实际上我们的程序有一个隐藏的窗口,就是Application对象,我们所有的消息循环都是Application来完成的,因此,我们只要在Application之前处理消息的话,就可以达到控制本程序所有消息的目的了!
下面的示例工程文件演示了如何做到这一点:
program Project1;
uses
Forms, messages, wintypes, winprocs,
Unit1 in 'UNIT1.PAS' {Form1};
{$R *.RES}
var
OldWndProc: TFarProc;
function NewWndProc(hWndAppl: HWnd; Msg, wParam: Word;
lParam: Longint): Longint; export;
begin
NewWndProc := 0; { Default WndProc return value }
{ * * * Handle messages here; The message number is in Msg * * * }
NewWndProc := CallWindowProc(OldWndProc, hWndAppl, Msg,
wParam, lParam);
end;
begin
Application.CreateForm(TForm1, Form1);
OldWndProc := TFarProc(GetWindowLong(Application.Handle,
GWL_WNDPROC));
SetWindowLong(Application.Handle, GWL_WNDPROC,
longint(@NewWndProc));
Application.Run;
end.