UnitMain;Interfaceusessysutils, Wintypes, Winprocs, Messages, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTmainform=class(tform) Sendbtn:tbutton; Postbtn:tbutton; procedureSendbtnclick (Sender:tobject); procedurePostbtnclick (Sender:tobject); procedureformcreate (Sender:tobject); procedureFormdestroy (Sender:tobject); PrivateOldwndproc:pointer; Wndprocptr:pointer; procedureWndmethod (varmsg:tmessage); procedureHandleappmessage (varmsg:tmsg;varHandled:boolean); End;varMainform:tmainform;Implementation{$R *. DFM}usesSCWNDPRC;procedureTmainform.sendbtnclick (sender:tobject);beginSendMessage (Application.handle, Wm_user,0,0);End;procedureTmainform.postbtnclick (sender:tobject);beginPostMessage (Application.handle, Wm_user,0,0);End;procedureTmainform.handleappmessage (varmsg:tmsg;varHandled:boolean);begin ifMsg.message = Wm_user ThenShowMessage (Format ('Message seen by onmessage! Value is: $%x', [msg.message]));End;procedureTmainform.wndmethod (varmsg:tmessage);begin ifMsg.msg = Wm_user Then //second processing (new process function)ShowMessage (Format ('Message seen by wndmethod! Value is: $%x', [msg.msg])); withMsg DoResult:= CallWindowProc (Oldwndproc, Application.handle, MSG, WParam, LParam);//third place processing (old procedure function)End;proceduretmainform.formcreate (sender:tobject);beginApplication.onmessage:= Handleappmessage;//the first place to deal with (before onmessage this off)Wndprocptr: = Makeobjectinstance (Wndmethod);//Make window proc {Set window procedure of application window.}Oldwndproc:=Pointer (SetWindowLong (Application.handle, GWL_WNDPROC, Integer (wndprocptr)));End;procedureTmainform.formdestroy (sender:tobject);begin {Restore Old window procedure for application window}SetWindowLong (Application.handle, GWL_WNDPROC, Longint (Oldwndproc)); {Free our user-created window procedure}freeobjectinstance (wndprocptr);End;End.UnitSCWNDPRC;InterfaceusesForms, Messages;ImplementationusesWindows, Sysutils, Dialogs;varWproc:pointer;functionNewwndproc (Handle:hwnd; MSG, WParam, lparam:longint): Longint; stdcall;{This is a Win32 api-level window procedure. It handles the messages}{received by the application window.}begin ifMSG = Wm_user Then {If It's our user-defined message and then alert the user.}ShowMessage (Format ('Message seen by wndproc! Value is: $%x', [MSG]); {Pass message on to old window procedure}Result:=CallWindowProc (Wproc, Handle, MSG, WParam, LParam);End;initialization {Set window procedure of application window.}Wproc:=Pointer (SetWindowLong (Application.handle, GWL_WNDPROC, Integer (@NewWndProc)));End.
Summary of messages sent to Application.handle:
1. Pass the application.onmessage first.
2. Over-the-new process functions this close
3. Can also continue to pass to the old procedure function
Where SendMessage is sent to the message without a message pump, so call the procedure function directly (execute the new procedure function first, then continue to pass to the old)
The process of executing (intercepting) messages three times to the Application.handle message