Use clipboard [5]: setashandle, getashandle-custom format

Source: Internet
Author: User
If you want to store your own format in the clipboard, you need to use the setashandle and getashandle methods.

Setashandle (the format ID used for the clipboard, the memory handle of the data); the two parameters of this method are a bit troublesome.
You must use the registerclipboardformat function to customize the clipboard format. The second parameter is the memory handle rather than the memory address. Currently, I only know the functions globalalloc and globalrealloc that can allocate memory and return the handle, the gmem_ddeshare flag must also be used to allocate memory for the clipboard.

The getashandle (format ID used for clipboard) method returns the handle of the memory where the data is stored.

Obtain the memory address through the memory handle and use the globallock function.

In this example, the structure tmyrec is customized and the corresponding clipboard format cf_my is specified.

Unit unit1; interfaceuses 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: = 'zhang san'; 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', [prec. name, prec. age]); {Zhang San 99 years old} globalunlock (data); end.
 
   
 // This example forgets 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.