Mirror

How to trap changes of the clipboard content (Views: 101)


Problem/Question/Abstract:

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.

unit Unit1;

interface

uses
  Windows, Messages, Forms, Classes, Controls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FNextClipboardViewer: HWND;
    procedure WMChangeCBChain(var Msg: TWMChangeCBChain); message WM_CHANGECBCHAIN;
    procedure WMDrawClipboard(var Msg: TWMDrawClipboard); message WM_DRAWCLIPBOARD;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  { Initialize variable }
  FNextClipboardViewer := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FNextClipboardViewer <> 0 then
    MessageBox(0, 'This window is already registered!', nil, 0)
  else
    { Add to clipboard chain }
    FNextClipboardViewer := SetClipboardViewer(Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  { Remove from clipboard chain }
  ChangeClipboardChain(Handle, FNextClipboardViewer);
  FNextClipboardViewer := 0;
end;

procedure TForm1.WMChangeCBChain(var Msg: TWMChangeCBChain);
begin
  inherited;
  { mark message as done }
  Msg.Result := 0;
  { the chain has changed }
  if Msg.Remove = FNextClipboardViewer then
    { The next window in the clipboard viewer chain had been removed. We recreate it. }
    FNextClipboardViewer := Msg.Next
  else
    { Inform the next window in the clipboard viewer chain }
    SendMessage(FNextClipboardViewer, WM_CHANGECBCHAIN, Msg.Remove, Msg.Next);
end;

procedure TForm1.WMDrawClipboard(var Msg: TWMDrawClipboard);
begin
  inherited;
  { Clipboard content has changed }
  try
    MessageBox(0, 'Clipboard content has changed!', 'Clipboard Viewer', MB_ICONINFORMATION);
  finally
    { Inform the next window in the clipboard viewer chain }
    SendMessage(FNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if FNextClipboardViewer <> 0 then
  begin
    { Remove from clipboard chain }
    ChangeClipboardChain(Handle, FNextClipboardViewer);
    FNextClipboardViewer := 0;
  end;
end;

end.

<< Back to main page