Author: Jonas Bilinkevicius
I need to ensure that when my TRichEdit copies text to the clipboard, it is copied
in a certain font, colour and size. My problem is that my TRichEdit is defaulted to
one font and the users are not given the ability to change it. But I want it to
pasted into Word (for example) in another font.
Answer:
You can of course compose a rich text file in code and copy that into the clipboard
using the standard rich edit clipboard format, but it's a lot of work. A somewhat
simpler approach may be to take the rich text as it is in the control (stream to a
TMemoryStream, load into a String) and then modify the \fonttbl tag in the file.
1 procedure TForm1.Button3Click(Sender: TObject);
2 var
3 S: string;
4 ss: TStringstream;
5 ms: TMemoryStream;
6 begin
7 ms := TMemoryStream.Create;
8 try
9 richedit1.Lines.SaveToStream(ms);
10 SetString(S, Pchar(ms.Memory), ms.size);
11 finally
12 ms.free
13 end;
14 memo1.text := S; {view raw rtf in TMemo to see font table}
15 S := Stringreplace(S, 'Times New Roman', 'Verdana', []);
16 ss := TStringstream.Create(S);
17 try
18 richedit1.Lines.LoadFromStream(ss);
19 finally
20 ss.free
21 end;
22 end;
23
24 //To get the new text into the clipboard proceed as below:
25
26 uses
27 Richedit, Clipbrd;
28
29 {$R *.dfm}
30
31 procedure CopyStreamToClipboard(fmt: Cardinal; S: TStream);
32 var
33 hMem: THandle;
34 pMem: Pointer;
35 begin
36 {Rewind stream position to start}
37 S.Position := 0;
38 {Allocate a global memory block the size of the stream data}
39 hMem := GlobalAlloc(GHND or GMEM_DDESHARE, S.Size);
40 if hMem <> 0 then
41 begin
42 {Succeeded, lock the memory handle to get a pointer to the memory}
43 pMem := GlobalLock(hMem);
44 if pMem <> nil then
45 begin
46 {Succeeded, now read the stream contents into the memory the pointer points
47 at}
48 try
49 S.read(pMem^, S.Size);
50 {Rewind stream again, caller may be confused if the stream position is
51 left at the end}
52 S.Position := 0;
53 finally
54 {Unlock the memory block}
55 GlobalUnlock(hMem);
56 end;
57 {Open clipboard and put the block into it. The way the Delphi clipboard
58 object is written this will clear the clipboard first.
59 Make sure the clipboard is closed even in case of an exception. If left open
60 it would become unusable for other apps.}
61 Clipboard.Open;
62 try
63 Clipboard.SetAsHandle(fmt, hMem);
64 finally
65 Clipboard.Close;
66 end;
67 end
68 else
69 begin
70 {Could not lock the memory block, so free it again and raise an out of
71 memory exception}
72 GlobalFree(hMem);
73 OutOfMemoryError;
74 end;
75 end
76 else
77 {Failed to allocate the memory block, raise exception}
78 OutOfMemoryError;
79 end;
80
81 var
82 CF_RTF: Word = 0; {set in Initialization section}
83
84 procedure TForm1.Button3Click(Sender: TObject);
85 var
86 S: string;
87 ss: TStringstream;
88 ms: TMemoryStream;
89 begin
90 ms := TMemoryStream.Create;
91 try
92 richedit1.Lines.SaveToStream(ms);
93 SetString(S, Pchar(ms.Memory), ms.size);
94 finally
95 ms.free
96 end;
97 S := Stringreplace(S, 'Times New Roman', 'Verdana', []);
98 ss := TStringstream.Create(S);
99 try
100 // richedit1.Lines.LoadFromStream(ss);
101 CopyStreamToClipboard(CF_RTF, ss);
102 finally
103 ss.free
104 end;
105 end;
106
107 initialization
108 CF_RTF := RegisterClipboardFormat(Richedit.CF_RTF);
109 end.
|