The use of the Clipboard in Delphi [5]:setashandle, Getashandle

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.