unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,
Clipbrd, jpeg; //Clipboard使用
type
TForm1 = class(TForm)
Image1: TImage;
ListBox1: TListBox;
Image2: TImage;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
//处理WM_DRAWCLIPBOARD消息过程
procedure WMDrawClipBoard(var AMessage:TMessage);message WM_DRAWCLIPBOARD;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
//观察链中下一个窗口句柄
NextClipHwnd:HWND;
{ TForm1 }
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//从观察链中删除本观察窗口
ChangeClipboardChain(Handle, NextClipHwnd);
//将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
SendMessage(NextClipHwnd, WM_CHANGECBCHAIN, Handle, NextClipHwnd);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//获得观察链中下一个窗口句柄
NextClipHwnd := SetClipBoardViewer(Handle);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if ListBox1.Items.Count >= 0 then
if ListBox1.Items.Strings[ListBox1.ItemIndex] ='Bitmap' then
begin
Clipboard.Open;
Clipboard.Clear;
Clipboard.SetAsHandle(CF_BITMAP, Image1.Picture.Bitmap.Handle);
Clipboard.Close;
end
else
begin
Clipboard.Open;
ClipBoard.Clear;
Clipboard.AsText := ListBox1.Items.Strings[ListBox1.ItemIndex];
ClipBoard.Close;
end;
end;
procedure TForm1.WMDrawClipBoard(var AMessage: TMessage);
begin
//将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
if NextClipHwnd <> 0 then
SendMessage(NextClipHwnd, AMessage.Msg, AMessage.WParam, AMessage.LParam);
//处理剪贴板中内容
if Clipboard.HasFormat(CF_TEXT) or Clipboard.HasFormat(CF_OEMTEXT) then
begin
//查询剪贴板中特定格式的数据内容
ListBox1.Items.Add(Clipboard.asText + '...');
end;
if Clipboard.HasFormat(CF_BITMAP) then
begin
ListBox1.Items.Add('Bitmap');
Image1.Picture.Bitmap.LoadFromClipboardFormat(CF_BITMAP, clipboard.GetAsHandle(CF_BITMAP), 0);
end;
end;
end.