The process of executing (intercepting) messages three times to the Application.handle message

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.