Author: Jonas Bilinkevicius
I'm trying to retrieve clipboard data in RTF tokens, and I can't figure out how to
do it. Here's the scenario: 1. Create rich text in (eg) WordPad, including bold,
italics etc., 2. Copy that to the clipboard., 3. Paste it into a TRichEdit. The
pasted data includes all the formatting info. However, if I try to get that data
off the clipboard myself, using Clipboard.AsText, or getting a handle to it with
CF_TEXT, what I get is plain text, minus the formatting. It seems to me there
should be a way to get RTF tokens from the clipboard, but neither the Delphi docs
nor the MS documentation lists any format that could include rich text. There is no
such format as CF_RICHTEXT. Anybody know how I can do this? Am I completely wrong
to assume that RTF tokens are even being passed through the clipboard in the above
scenario?
Answer:
1 uses
2 RichEdit;
3
4 function GetRawRTFFromClipboard: string;
5 var
6 H: THandle;
7 TextPtr: PChar;
8 CurrentFormat: Integer;
9 NameLen: DWord;
10 NameStr: string;
11 begin
12 Result := '';
13 ClipBoard.Open;
14 try
15 CurrentFormat := EnumClipboardFormats(0);
16 while CurrentFormat <> 0 do
17 begin
18 NameLen := 1024;
19 SetLength(NameStr, NameLen);
20 NameLen := GetClipboardFormatName(CurrentFormat, PChar(NameStr), NameLen);
21 SetLength(NameStr, NameLen);
22 if CompareText(NameStr, CF_RTF) = 0 then
23 Break;
24 CurrentFormat := EnumClipboardFormats(CurrentFormat);
25 end;
26 if CurrentFormat = 0 then
27 raise Exception.Create('Data on clipboard is not RTF');
28 H := Clipboard.GetAsHandle(CurrentFormat);
29 TextPtr := GlobalLock(H);
30 Result := StrPas(TextPtr);
31 GlobalUnlock(H);
32 finally
33 Clipboard.Close;
34 end;
35 end;
|