Author: Jonas Bilinkevicius
I want to write raw bytes to the clipboard, but the only functions the TClipboard
component offers are SetText, SetAsHandle and SetComponent. Well, I need to set
these bytes as a certain format (566 to be exact), so that rules out SetText.
SetAsHandle didn't seem to work when I tried it, and I have no idea how
SetComponent would do anything for me. The help files included with Delphi (6)
weren't overly helpful in explaining how to use this class in any way, other than
copying text and pictures.
Answer:
Here is an example:
1 uses2 Clipbrd;
3 const4 MyFormatName = 'My Junk Clipboard Format';
5 MySource: PChar = 'hello';
6 var7 MyFormat: Word;
8 MySize: Integer;
9 MyMemory: THandle;
10 MyBuffer: Pointer;
11 begin12 MyFormat := RegisterClipboardFormat(MyFormatName);
13 {Determine the size of the data to be copied. 14 StrLen does not count the null terminator.15 So add one byte to be sure it will be pasted correctly as PChar.}16 MySize := StrLen(MySource) + 1;
17 {Allocate memory for passing data to the clipboard.}18 MyMemory := GlobalAlloc(GMEM_MOVEABLE, MySize);
19 try20 {Copy data to the memory}21 MyBuffer := GlobalLock(MyMemory);
22 try23 Move(MySource^, MyBuffer^, MySize);
24 finally25 GlobalUnlock(MyMemory);
26 end;
27 {Call TClipboard.SetAsHandle}28 Clipboard.SetAsHandle(MyFormat, MyMemory);
29 except30 GlobalFree(MyMemory);
31 raise;
32 end;
33 end;