剪切板-监视剪贴板
相关资料:
http://user.qzone.qq.com/513187410/blog/1265981516
实例原码:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls,
8 Clipbrd, jpeg; //Clipboard使用
9
10 type
11 TForm1 = class(TForm)
12 Image1: TImage;
13 ListBox1: TListBox;
14 Image2: TImage;
15 procedure FormCreate(Sender: TObject);
16 procedure FormClose(Sender: TObject; var Action: TCloseAction);
17 procedure ListBox1Click(Sender: TObject);
18 private
19 { Private declarations }
20 //处理WM_DRAWCLIPBOARD消息过程
21 procedure WMDrawClipBoard(var AMessage:TMessage);message WM_DRAWCLIPBOARD;
22 public
23 { Public declarations }
24 end;
25
26 var
27 Form1: TForm1;
28
29 implementation
30 {$R *.dfm}
31
32 var
33 //观察链中下一个窗口句柄
34 NextClipHwnd:HWND;
35 { TForm1 }
36
37 procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
38 begin
39 //从观察链中删除本观察窗口
40 ChangeClipboardChain(Handle, NextClipHwnd);
41 //将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
42 SendMessage(NextClipHwnd, WM_CHANGECBCHAIN, Handle, NextClipHwnd);
43 end;
44
45 procedure TForm1.FormCreate(Sender: TObject);
46 begin
47 //获得观察链中下一个窗口句柄
48 NextClipHwnd := SetClipBoardViewer(Handle);
49 end;
50
51 procedure TForm1.ListBox1Click(Sender: TObject);
52 begin
53 if ListBox1.Items.Count >= 0 then
54 if ListBox1.Items.Strings[ListBox1.ItemIndex] ='Bitmap' then
55 begin
56 Clipboard.Open;
57 Clipboard.Clear;
58 Clipboard.SetAsHandle(CF_BITMAP, Image1.Picture.Bitmap.Handle);
59 Clipboard.Close;
60 end
61 else
62 begin
63 Clipboard.Open;
64 ClipBoard.Clear;
65 Clipboard.AsText := ListBox1.Items.Strings[ListBox1.ItemIndex];
66 ClipBoard.Close;
67 end;
68 end;
69
70 procedure TForm1.WMDrawClipBoard(var AMessage: TMessage);
71 begin
72 //将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
73 if NextClipHwnd <> 0 then
74 SendMessage(NextClipHwnd, AMessage.Msg, AMessage.WParam, AMessage.LParam);
75 //处理剪贴板中内容
76 if Clipboard.HasFormat(CF_TEXT) or Clipboard.HasFormat(CF_OEMTEXT) then
77 begin
78 //查询剪贴板中特定格式的数据内容
79 ListBox1.Items.Add(Clipboard.asText + '...');
80 end;
81 if Clipboard.HasFormat(CF_BITMAP) then
82 begin
83 ListBox1.Items.Add('Bitmap');
84 Image1.Picture.Bitmap.LoadFromClipboardFormat(CF_BITMAP, clipboard.GetAsHandle(CF_BITMAP), 0);
85 end;
86 end;
87
88 end.
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417075318%E5%8A%A0%E6%88%91QQ.jpg)
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417080925%E5%8A%A0%E6%88%91%E5%BE%AE%E4%BF%A1.jpg)