如何捕获窗体所有控件发生的事件
1个回答
推荐于2016-05-09
展开全部
用HOOK应该可以实现,下面是代码供参考
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
WM_TestMessage = WM_USER + 2000;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
HookHandle: HHOOK;
function TestHookProc(Code: Integer; WParam: Longint;Msg:Longint): Longint;stdcall;
begin
if (Code = HC_ACTION) then
if PMsg(Msg)^.Message = WM_TestMessage then
begin
showMessage('已经截获该消息');
end;
Result := CallNextHookEx(HookHandle, Code, WParam, Longint(@Msg));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
HookHandle:=SetWindowsHookEx(WH_GETMESSAGE,TestHookProc,0,GetCurrentThreadID);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(self.Handle,WM_TestMessage,0,0);
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
WM_TestMessage = WM_USER + 2000;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
HookHandle: HHOOK;
function TestHookProc(Code: Integer; WParam: Longint;Msg:Longint): Longint;stdcall;
begin
if (Code = HC_ACTION) then
if PMsg(Msg)^.Message = WM_TestMessage then
begin
showMessage('已经截获该消息');
end;
Result := CallNextHookEx(HookHandle, Code, WParam, Longint(@Msg));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
HookHandle:=SetWindowsHookEx(WH_GETMESSAGE,TestHookProc,0,GetCurrentThreadID);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(self.Handle,WM_TestMessage,0,0);
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询