Use of the Clipboard in Delphi [1]: Astext, Settextbuf, Gettextbuf

Source: Internet
Author: User

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;

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.