The shear plate class Tclipboard is defined in the CLIPBRD unit, before using the uses CLIPBRD;
uses Clipbrd;
procedure TForm1.Button1Click(Sender: TObject);
var
clip: TClipboard;
begin
clip := TClipboard.Create; {建立}
clip.AsText := Self.Text; {把窗体标题放入剪切板}
ShowMessage(clip.AsText); {从剪切板读取, 返回结果是: Form1}
{因为剪切板是全局的, 此时可以在其他地方粘贴一试}
clip.Free; {释放}
end;
According to Delphi to provide us with the convenience, the above example can be simplified to:
uses Clipbrd;
procedure TForm1.Button1Click(Sender: TObject);
begin
Clipboard.AsText := Text;
ShowMessage(Clipboard.AsText); {Form1}
end;
What is this Clipboard? Is it a type variable like screen?
The answer is NO! Clipboard is just a function, is a parameterless function, is defined in the CLIPBRD unit of a global function, it returns a tclipboard type of variable, when I see the source code of this function, really feel and learn a trick, very sophisticated ideas.
In addition to using the Tclipboard.astext property, we can also use Settextbuf to put text into the Clipboard and use Gettextbuf to read out the text in the Clipboard.
uses Clipbrd;
{使用 SetTextBuf}
procedure TForm1.Button1Click(Sender: TObject);
begin
Clipboard.SetTextBuf(PChar(Text)); {按参数类型要求, 需要转换一下}
ShowMessage(Clipboard.AsText); {Form1}
end;
{使用 GetTextBuf 就和使用 API 差不多, 需要给个缓冲区}
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;
{如果不给缓冲区, 那你自己得申请并释放内存}
procedure TForm1.Button3Click(Sender: TObject);
var
pc: PChar;
begin
Clipboard.AsText := Text;
GetMem(pc, 256); {申请内存}
Clipboard.GetTextBuf(pc, 256);
ShowMessage(pc); {Form1}
FreeMem(pc); {释放内存}
end;