WatchClipboard

クリップボードの変更を監視するようなアプリケーションのためのコード。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Forms;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FClipNextHandle: THandle;
    procedure WMDrawClipboard(var Msg: TWMDrawClipboard);
      message WM_DRAWCLIPBOARD;
    procedure WMChangeCBChain(var Msg: TWMChangeCBChain);
      message WM_CHANGECBCHAIN;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMDrawClipboard(var Msg: TWMDrawClipboard);
begin
  inherited;
  //クリップボードが更新されるたびの処理をここに書く
  if FClipNextHandle <> 0 then
    SendMessage(FClipNextHandle, WM_DRAWCLIPBOARD, 0, 0);
end;

procedure TForm1.WMChangeCBChain(var Msg: TWMChangeCBChain);
begin
  if Msg.Remove = FClipNextHandle then
    FClipNextHandle := Msg.Next;
  if FClipNextHandle <> 0 then
    SendMessage(FClipNextHandle, WM_CHANGECBCHAIN, Msg.Remove, Msg.Next);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FClipNextHandle := SetClipboardViewer(Handle);
  if (FClipNextHandle = 0) and (GetLastError <> 0) then Abort;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChangeClipboardChain(Handle, FClipNextHandle);
end;

end.

Return index page