使用剪下板[3]: SetComponent、GetComponent

來源:互聯網
上載者:User
本例示範把一個組件(TEdit)放入剪下板, 又取出(放到一個 TPanel 上)的過程.

放入剪下板的方法是個過程: SetComponent(要放入的組件);
取出的方法是個函數: GetComponent(指定屬主, 指定父視窗): 函數返回取出的組件的控制代碼.

取出以前, 最好要判斷一下當前剪下板中是不是個組件: HasFormat(CF_COMPONENT);

取出以前還必須要註冊要取出的組件類, 譬如: RegisterClasses([TEdit]);

準備工作: 在表單上添加 TEdit、TPanel 和三個按鈕.
本例:


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 Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}uses Clipbrd;var obj: TComponent; {用於接受 GetComponent 的傳回值}procedure TForm1.Button1Click(Sender: TObject);begin  Clipboard.SetComponent(Edit1);  TButton(Sender).Caption := '複製';end;procedure TForm1.Button2Click(Sender: TObject);begin  RegisterClasses([TEdit]);  if Clipboard.HasFormat(CF_COMPONENT) then    obj := Clipboard.GetComponent(nil, Panel1);  TButton(Sender).Caption := '粘貼';end;procedure TForm1.Button3Click(Sender: TObject);begin  if Assigned(obj) then obj.Free;  TButton(Sender).Caption := '刪除';end;end.

一般情況下, 應該把 RegisterClasses(); 過程提前放置(起碼可以避免反覆執行), 譬如在 Form1.OnCreate 事件中;
大家好像都習慣再提前到: initialization. 程式修改如下:

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 Button1Click(Sender: TObject);    procedure Button2Click(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 := '複製';end;procedure TForm1.Button2Click(Sender: TObject);begin  if Clipboard.HasFormat(CF_COMPONENT) then    obj := Clipboard.GetComponent(nil, Panel1);  TButton(Sender).Caption := '粘貼';end;procedure TForm1.Button3Click(Sender: TObject);begin  if Assigned(obj) then obj.Free;  TButton(Sender).Caption := '刪除';end;initialization  RegisterClasses([TEdit]);end.

另外, 關於剪下板中格式的問題還沒有詳談, 這裡有來了一個 CF_COMPONENT.

Windows 系統已經定義了十幾種剪下板的格式常數, 譬如: CF_BITMAP、CF_TEXT 等等;
不過這裡的 CF_COMPONENT 是 Delphi 自訂的, 可以猜測: 在需要的時候, 我們也可以自訂剪下板中的格式.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.