Delphi's message processing

xiaoxiao2021-03-06  50

Delphi is an object-oriented visual software development tool for Borland. Delphi focuses on the advantages of Visual C and Visual Basic: easy to get started, powerful, especially in interface design, database programming, and network programming more unique advantages. The message message in Delphi is a notification issued by Windows, which tells the application an event. In Delphi, in most cases, Windows messages are encapsulated in the event of VCL, we only need to handle the corresponding VCL event, but if we need to write your own control, intercept or filter messages must be deeply studied in depth, Win32 Message process mechanism. In Delphi, messages are defined in the way. Open your message.pas file, we can see that tMessage is defined: Type tMessage = Packed Record Msg: 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; where MSG is a constant value distinguished from other messages, these constant values ​​can be predefined in the Windows unit, also It can be a constant defined by the user. WPARAM is usually a constant value associated with a message or a handle of a window or control. LPARAM is usually a pointer to data in memory. Result is the return value of the message processing. WPARAM, LPARAM and RESULT are 32-bit, and if you want to access the low 16-bit or high 16 bits, WParamlo, WParamhi, LParamlo, LParamhi, Resultlo, and Resulthi can be used separately. In addition to common TMessage in Delphi, a special message record is defined for each Windows. We can browse Message.Pas files, below is a keyboard message record: twmkey = packed record msg: cardinal; charcode: word; unused: word; keydata: longint; result: longint; message related to keyboard such as: WM_KeyDown, WM_KEYUP, WM_CHAR, WM_SYSKEYDOWN WM_SYSKEYUP, the records of WM_SYSCHAR are also defined as TWMKEY. There Message.pas file in the following statement: TWMChar = TWMkey; TWMKeyDown = TWMkey; TWMKeyUp = TWMkey; TWMSys -KeyDown = TWMkey; TWMSysKeyUp = TWMkey; TWMSysChar = TWMkey; how to send a message processing is to define the application response message Windows Message . Each message in Delphi has its own processing, it must be a method in an object, and can only pass a TMessage or other special message record, and then have a message command after the method declaration, then one after another is 0 to 0 The constant between 32767. The news we mentioned earlier is a standard Windows message (WM_X), in addition to this, the VCL internal message, notification message, and user-defined messages. The VCL internal message is usually starting with "cm_" for managing things inside the VCL. If you change some of the other characteristics of a property or component, you need to notify other components by internal messages.

For example, an activation input focus message is to accept or discard the input focus to the activated or deactivated component. There is also a notification message, and the child control in one window has some things that need to notify the parent window, which is implemented by notification messages. It only applies to standard window controls, such as buttons, list boxes, edit boxes, and more. Message.pas open file, in the standard Windows message is a notification of the statement: const {$ EXTERNALSYM BN_CLICKED} BN_CLICKED = 0; {$ EXTERNALSYM BN_PAINT} BN_PAINT = 1; {$ EXTERNALSYM BN_HILITE} BN_HILITE = 2; upper button notification The message indicates that the user clicks the button, the button should be heavy, and the user highlights the button. Users can also define messages to send messages and write messages processing. The constant value of the message is WM_USER 100 to $ 7FFF, this range is why Windows is reserved for user-defined messages. There are three ways to send the Delphi message: 1. The Perform object method of the TControl class. You can send messages to any form or control, you only need to know the instance of the form or control. It is as follows: Function Tcontrol.Perform (Msg: Cardinal; WPARAM, LPARAM: LongInt): Longint 2. Windows API Functions SendMessage () and PostMessage (). It is as follows: Function SendMessage (hwnd: hwnd; msg: uint; wparam: wparam; lparam: lparam): LRESULT; stdcall; function sendMessage (hwnd: hwnd; msg: uint; wparam: wparam; lparam: lparam): LRESULT; The StdCall PostMessage function is added to the application's message queue. The application's message loop will extract the registered message from the message queue, and then send it to the corresponding window. The SendMessage function can be sent directly to the window procedure over the message queue. So use SendMessage when Windows needs to return values ​​immediately, use PostMessage when different applications are required to process messages in turn. PERFORM is similar to SendMessage in nature, and they send them directly to the window process. SendMessage, the PostMessage function only needs to know the handle of the window to send a message, so they can send a message to the non-Delphi form, but Perform must know the form of the form or control. The VCL message handling mechanism is in the source code of the Delphi application, and its role is to start the message loop, then call Application.ProcessMessage, which looks up a message in the application's message queue. When a message is retrieved in the message queue, the Application.onMessage event is triggered. This responds to the processing of the onMessage event before Windows itself, which is better than any message processing, and only the registered message is received, that is, the message sent by PostMessage described above.

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

New Post(0)