If you want to store your own format in the Clipboard, you need to use the Setashandle, Getashandle two methods.
Setashandle (the format ID for the Clipboard, the memory handle of the data); It's a bit of a hassle to look at the two parameters of this method.
The custom Clipboard format uses the RegisterClipboardFormat function; The second parameter is a memory handle instead of a memory address, a function that allocates memory and returns a handle for the moment I know only GlobalAlloc, globalrealloc two functions that use the GMEM_DDESHARE flag when allocating memory for the Clipboard.
The Getashandle (format ID for the Clipboard) method returns a handle to the memory in which the data resides.
Get the memory address from the memory handle and use the GlobalLock function.
This example customizes the structure Tmyrec and specifies the corresponding clipboard format cf_my.
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
TForm1 = Class (Tform)
Button1:tbutton;
Button2:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses CLIPBRD;
Type
Tmyrec = Record
NAME:STRING[8];
Age:byte;
End
Var
Cf_my:word;
Procedure Tform1.formcreate (Sender:tobject);
Begin
Cf_my: = RegisterClipboardFormat (' my Format ');
End
Procedure Tform1.button1click (Sender:tobject);
Var
PREC: ^tmyrec;
Data:thandle;
Begin
Data: = GlobalAlloc (Gmem_ddeshare, SizeOf (Tmyrec));
PRec: = GlobalLock (Data);
Prec.name: = ' John ';
Prec.age: = 99;
GlobalUnlock (Data);
Clipboard.setashandle (Cf_my, Data);
End
Procedure Tform1.button2click (Sender:tobject);
Var
PREC: ^tmyrec;
Data:thandle;
Begin
If not Clipboard.hasformat (cf_my) then Exit;
Data: = Clipboard.getashandle (cf_my);
PRec: = GlobalLock (Data);
Showmessagefmt ('%s%d years old ', [Prec.name, Prec.age]); {John 99 years old}
GlobalUnlock (Data);
End
End.
This example forgot the GlobalFree.