The clipboard class tclipboard is defined in the clipbrd unit. Uses clipbrd is required before use;
Uses clipbrd; Procedure tform1.button1click (Sender: tobject); var clip: tclipboard; begin clip: = tclipboard. create; {create} clip. astext: = self. text; {put the Form title into the clipboard} showmessage (clip. astext); {read from the clipboard. The returned result is: form1} {because the clipboard is global, You can paste it elsewhere} clip. free; {release} end;
According to the convenience provided by Delphi, the above example can be simplified:
Uses clipbrd; Procedure tform1.button1click (Sender: tobject); begin clipboard. astext: = text; showmessage (clipboard. astext); {form1} end;
What is this clipboard? Is it the same type variable as screen?
The answer is no! Clipboard is just a function. It is a non-argument function and a global function defined in the clipbrd unit. It returns a variable of the tclipboard type. When I see the source code of this function, I feel that I have learned another skill and a very delicate idea.
In addition to the tclipboard. astext attribute, we can also use settextbuf to put the text into the clipboard and use gettextbuf to read the text in the clipboard.
Uses clipbrd; {use settextbuf} procedure tform1.button1click (Sender: tobject); begin clipboard. settextbuf (pchar (text); {according to parameter type requirements, need to convert} showmessage (clipboard. astext); {form1} end; {using gettextbuf is similar to using APIs. You need to give a buffer} procedure tform1.button2click (Sender: tobject); var arr: array [0 .. 255] of char; begin clipboard. astext: = text; clipboard. gettextbuf (ARR, length (ARR); showmessage (ARR); {form1} end; {if no buffer zone is provided, you have to apply for and release the memory.} procedure tform1.button3click (Sender: tobject); var PC: pchar; begin clipboard. astext: = text; getmem (PC, 256); {applied memory} clipboard. gettextbuf (PC, 256); showmessage (PC); {form1} freemem (PC); {release memory} end;