一个处理剪贴板的简单例子,只是将剪贴板的文字内容显示为窗口标题。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
Clipbrd;//加入clipbrd单元
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
NextClipHwnd:HWND;//观察链中下一个窗口句柄
procedure WMDrawClipBoard
(var AMessage:TMessage);
message WM_DRAWCLIPBOARD;
//处理WM_DRAWCLIPBOARD消息过程
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.WMDrawClip
Board(var AMessage: TMessage);
begin
//将WM_DRAWCLIPBOARD
消息传递到下一个观察链中的窗口
SendMessage(NextClipHwnd,AMessage.
Msg,AMessage.WParam,AMessage.LParam);
//查询剪贴板中特定格式的数据内容
if (Clipboard.HasFormat(CF_TEXT) or
Clipboard.HasFormat(CF_OEMTEXT)) then
begin
//处理剪贴板中内容
Caption:=Clipboard.asText;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//获得观察链中下一个窗口句柄
NextClipHwnd:=SetClipBoardViewer(Handle);
end;
procedure TForm1.FormClose
(Sender: TObject; var Action: TCloseAction);
begin
//从观察链中删除本观察窗口
ChangeClipboardChain(Handle,NextClipHwnd);
//将WM_DRAWCLIPBOARD
消息传递到下一个观察链中的窗口
SendMessage(NextClipHwnd,WM_
CHANGECBCHAIN,Handle,NextClipHwnd);
end;
end.