使用 FWindowHandle := AllocateHWnd(WndProc);获得一个句柄。
下面是我写的一个监视剪贴板变动的非可视控件,还没测试过,不知道对不对:
unit ZeroClipBoardSpy;
{
Web Site:http://myzeroworld.yeah.net
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TZeroClipBoardSpy = class(TComponent)
private
FWindowHandle:THandle;
//next clipboard viewer handle
FNextViewerHandle:THandle;
fOnChange: TNotifyEvent;
fStarted:boolean;
//main procedure
procedure WndProc(var Msg: TMessage);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent);override;
destructor Destroy; override;
procedure StartSpy;
procedure StopSpy;
{ Public declarations }
published
property OnChange:TNotifyEvent read fOnChange write fOnChange;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Zero Component', [TZeroClipBoardSpy]);
end;
{ TZeroClipBoardSpy }
constructor TZeroClipBoardSpy.Create(AOwner: TComponent);
begin
inherited;
FWindowHandle := AllocateHWnd(WndProc);
FNextViewerHandle:=0;
fStarted:=false;
end;
destructor TZeroClipBoardSpy.Destroy;
begin
inherited;
//call stop spy on destory
StopSpy;
//release handle
DeallocateHWnd(FWindowHandle);
end;
procedure TZeroClipBoardSpy.StartSpy;
begin
if not(fStarted) then
begin
fNextViewerHandle := SetClipboardViewer(fWindowHandle);
fStarted:=true;
end;
end;
procedure TZeroClipBoardSpy.StopSpy;
begin
if fStarted then
begin
ChangeClipboardChain(fWindowHandle, fNextViewerHandle);
fStarted:=false;
end;
end;
procedure TZeroClipBoardSpy.WndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_DRAWCLIPBOARD: //clipboard change
try
try
if assigned(fOnChange) then
fOnChange(Self);
finally
msg.Result := SendMessage(WM_DRAWCLIPBOARD, FNextViewerHandle, 0, 0);
end;
except
Application.HandleException(Self);
end;
WM_CHANGECBCHAIN://process change chain message
try
if THandle(msg.wParam) = FNextViewerHandle then
begin
FNextViewerHandle := msg.lParam;
msg.Result := 0;
end
else
msg.Result := SendMessage(FNextViewerHandle, WM_CHANGECBCHAIN,msg.wParam, msg.lParam);
except
Application.HandleException(Self);
end;
else
with Msg do
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;{Case}
end;
end.