Understand Windows Message (in Delphi) -

xiaoxiao2021-04-10  492

Understand Windows Message (in Delphi) - -

1. What is a message?

2. How to work in Windows message system

3. Delphi Message System

4. Message Process

5. Send your own message

6. User custom message

1. What is a message message, that is, a notification issued by Windows, the message itself is passed to the application as a record, and this record contains the type of message and other information. For example, for messages generated by clicking on the mouse, this record contains the coordinates when clicking the mouse. This record type is called TM SG, which is declared in the Windows unit: Type TMSG = Packed RecordhWnd: hwnd; // Window handle Message: uint; // message constant identifier, can be predefined in the Windows unit, It is also possible to be a customized constant WPARAM: WPARAM; // 32 bit message, is usually a constant value related to the message, or the handle of a window or control. LPARAM: LPARAM; / / 32-bit messages Specific additional information, usually a pointer to data in memory. Because WPARAM, LPARAM and POIIters are 32-bit, they can be converted between them. Time: DWORD; // Message Time PT: TPOINT; // Message Create Mouse Location End; 2. Windows Message System Work: Windows Message System consists of 3 parts: a. message queue. Wi N D O W S can maintain a message queue for all applications. The application must get a message from the message queue and assign it to a window. B. Message loop. The message is retrieved from the message queue through this loop mechanism, and then assisted it to the appropriate window, then continue to retrieve the next message from the message queue, and then assign it to the appropriate window. C. Window process. Each window has a window process to receive a message passed to the window, and its task is to get the message and respond to it. The window process is a callback function; after processing a message, it usually returns a value to Wi N D O W s. Note: The callback function is a function in the program, which is called by Windows or external modules. A message is generated from a window response, where there is 5 steps: 1) an event has occurred in the system. 2) Windows translates this event into a message and put it in the message queue. 3) The application receives this message from the message queue and stores it in the TMSG record. 4) The application passes the message to a window process of an appropriate window. 5) The window process responds to this message and processes.

3. Delphi Message System: The message loop has been encapsulated into the VCL's Forms unit, and Delphi also maps the information in the Windows TMSG record to TMESSAGE record: Type TMESSAGE = Recordmsg: cardinal; Case Integer of 0: (wparam: longint; LParam: longint; result: longint); 1: (WParamlo: Word; WPARAMHI: Word; lParamlo: Word; lParamhi: Word; Resultlo: Word; Resulthi: Word; End; except for General TMESSAGE records, Delphi is per Windows The message defines a special message record so you don't have to decompose information from the WPARAM domain and the LPARAM domain 4. Message Processing: In a standard Windows application, the message is handled by the window process. In Delphi, Each message has a respective process. The process for message processing must meet the following three conditions: A This process must be a method in an object. B The process must have a var parameter, the variable is TMESSAGE or other special Message record .C Declaring this process, you must use the Message indicator, followed by the constant value of the message to be processed. Below is a code that declares a process of processes the WM_Paint message: Procedure WmPaint (var Msg: tMessage); message wm_paint; Note that this convention is used to name the process for message processing: The process name is consistent with the identifier of the message, but don't write all over, don't undertake it. Assign the Result domain: When handling some Windows messages, Windows hopes to return A value .Tapplication's OnMessage event: To handle Windows messages, you can also use the TAPPLICATION's onMessage event. After the processing of responding to the onMessage event, you will trigger the onMessage event as long as you retrieve a message from the message queue .. Response The process of Application.onMessage event must be a TMESSAGEVENT type 5. Send your own message: Just like the Windows Send message, you can also send a message between the window and the control. Delphi provides several applications within an application. Message: call perform () (this method does not depend on Windows API), and call Sendme SSAGE (), postmessage () API function. Perform (): VCL's Perform () method uses all TControl's derived objects. Upperform can send messages to any window and control, just know the instance of this window and control. Upperform needs to pass three parameters: message identifier, WPARAM and LPARAM: Function Tcontrol.Perform (msg: cardinal; wparam, lparam: longint): longint; After calling the Perform message, it will wait until the message is handled after it will return .sendMessage () and postMessage () method: pass the window The handle Send message SendMessage () sends the message to the window, and returns postMessage () after the message is processed, just sends the message to the message queue, then returns Function SendMessage (hwnd: hwnd; msg: uint; wparam: wparam; lParam : Lparam: LRESULT; stdcall; function postmessage (hwnd: hwnd; msg: uint; wparam: wparam; lparam: lparam): bool; stdcall;

转载请注明原文地址:https://www.9cbs.com/read-133395.html

New Post(0)