Author: Tomas Rutkauskas
Is there a way to use an OnChange event for the clipboard? I want to avoid to check
for a change of the clipboard content every millisecond.
Answer:
An application can register itself in the clipboard viewer chain. The first window
in this chain always receives the messages. Every window is responsible to pass the
messages on to the next one.
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, Forms, Classes, Controls, StdCtrls;
7
8 type
9 TForm1 = class(TForm)
10 Button1: TButton;
11 Button2: TButton;
12 procedure Button1Click(Sender: TObject);
13 procedure Button2Click(Sender: TObject);
14 procedure FormCreate(Sender: TObject);
15 procedure FormDestroy(Sender: TObject);
16 private
17 FNextClipboardViewer: HWND;
18 procedure WMChangeCBChain(var Msg: TWMChangeCBChain); message WM_CHANGECBCHAIN;
19 procedure WMDrawClipboard(var Msg: TWMDrawClipboard); message WM_DRAWCLIPBOARD;
20 end;
21
22 var
23 Form1: TForm1;
24
25 implementation
26
27 {$R *.DFM}
28
29 procedure TForm1.FormCreate(Sender: TObject);
30 begin
31 { Initialize variable }
32 FNextClipboardViewer := 0;
33 end;
34
35 procedure TForm1.Button1Click(Sender: TObject);
36 begin
37 if FNextClipboardViewer <> 0 then
38 MessageBox(0, 'This window is already registered!', nil, 0)
39 else
40 { Add to clipboard chain }
41 FNextClipboardViewer := SetClipboardViewer(Handle);
42 end;
43
44 procedure TForm1.Button2Click(Sender: TObject);
45 begin
46 { Remove from clipboard chain }
47 ChangeClipboardChain(Handle, FNextClipboardViewer);
48 FNextClipboardViewer := 0;
49 end;
50
51 procedure TForm1.WMChangeCBChain(var Msg: TWMChangeCBChain);
52 begin
53 inherited;
54 { mark message as done }
55 Msg.Result := 0;
56 { the chain has changed }
57 if Msg.Remove = FNextClipboardViewer then
58 { The next window in the clipboard viewer chain had been removed. We recreate
59 it. }
60 FNextClipboardViewer := Msg.Next
61 else
62 { Inform the next window in the clipboard viewer chain }
63 SendMessage(FNextClipboardViewer, WM_CHANGECBCHAIN, Msg.Remove, Msg.Next);
64 end;
65
66 procedure TForm1.WMDrawClipboard(var Msg: TWMDrawClipboard);
67 begin
68 inherited;
69 { Clipboard content has changed }
70 try
71 MessageBox(0, 'Clipboard content has changed!', 'Clipboard Viewer',
72 MB_ICONINFORMATION);
73 finally
74 { Inform the next window in the clipboard viewer chain }
75 SendMessage(FNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
76 end;
77 end;
78
79 procedure TForm1.FormDestroy(Sender: TObject);
80 begin
81 if FNextClipboardViewer <> 0 then
82 begin
83 { Remove from clipboard chain }
84 ChangeClipboardChain(Handle, FNextClipboardViewer);
85 FNextClipboardViewer := 0;
86 end;
87 end;
88
89 end.
|