This example demonstrates the process of placing a component (tedit) on the clipboard and taking it out (put it on a tpanel.
The method for placing a clipboard is a process: setcomponent (component to be placed );
The method to retrieve is a function: getcomponent (specifying the owner and specifying the parent window): the handle of the component returned by the function.
Before extracting the clipboard, you 'd better determine whether it is a component in the current clipboard: hasformat (cf_component );
You must register the component class to be retrieved before removing the class, for example, registerclasses ([tedit]);
Preparation: Add tedit, tpanel, and three buttons to the form.
In this example:
unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; edit1: tedit; Panel1: tpanel; Procedure handle (Sender: tobject); Procedure handle (Sender: tobject); Procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses clipbrd; var OBJ: tcomponent; {used to accept the returned value of getcomponent} procedure tform1.button1click (Sender: tobject); begin clipboard. setcomponent (edit1); tbutton (sender ). caption: = 'copy'; end; Procedure tform1.button2click (Sender: tobject); begin registerclasses ([tedit]); If clipboard. hasformat (cf_component) Then OBJ: = clipboard. getcomponent (nil, Panel1); tbutton (sender ). caption: = 'pause'; end; Procedure tform1.button3click (Sender: tobject); begin if assigned (OBJ) Then obj. free; tbutton (sender ). caption: = 'delete'; end.
Generally, you should place the registerclasses (); Process in advance (at least avoid repeated execution), for example, in the form1.oncreate event;
It seems that everyone is used to advance to initialization.ProgramModify as follows:
unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; edit1: tedit; Panel1: tpanel; Procedure handle (Sender: tobject); Procedure handle (Sender: tobject); Procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses clipbrd; var OBJ: tcomponent; Procedure tform1.button1click (Sender: tobject); begin clipboard. setcomponent (edit1); tbutton (sender ). caption: = 'copy'; end; Procedure tform1.button2click (Sender: tobject); begin if clipboard. hasformat (cf_component) Then OBJ: = clipboard. getcomponent (nil, Panel1); tbutton (sender ). caption: = 'pause'; end; Procedure tform1.button3click (Sender: tobject); begin if assigned (OBJ) Then obj. free; tbutton (sender ). caption: = 'delete'; end; Initialization registerclasses ([tedit]); end.
In addition, the format of the clipboard has not been discussed in detail. Here we have a cf_component.
The format constants of more than a dozen clipboard types have been defined in windows, such as cf_bitmap and cf_text;
However, the cf_component here is customized by Delphi. We can guess that when necessary, we can also customize the format in the clipboard.