Delphi's message processing

zhaozj2021-02-16  45

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.

Message in Delphi

The message is a notification issued by Windows that 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 in this:

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 the MSG is a constant value distinguished from other messages, these constant values ​​can be predefined in the Windows unit, or they can be the 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 the keyboard message record:

TWMKEY = Packed Record

Msg: cardinal;

Charcode: Word;

Unused: Word;

Keydata: longint;

Result: longint;

Messages related to keyboards, such as: WM_KeyDown, WM_KEYUP, WM_CHAR, WM_SYSKEYDOWN WM_SYSKEYUP, WM_SYSKEYUP, WM_SYSCHAR records are also defined as TWMKEY. There is the following statement in the message.pas file:

TWMCHAR = twmkey; twmkeydown =

Twmkey; twmkeyup = twmkey; twmsys

-Keydown = twmkey; twmsyskeyup =

TWMKEY; TWMSYSCHAR = twmkey;

Message transmission

Message processing is to define how the application responds to Windows messages. 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. Open your message.pas file, after the standard Windows is the declaration of the notification message:

Const

{$ Externalsym bn_clicked}

Bn_clicked = 0;

{$ Externalsym bn_paint}

BN_PAINT = 1;

{$ Extealsym bn_hilite}

BN_HILITE = 2;

The above is the notification message of the button, indicating 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 Delphi messages:

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. Its statement is as follows:

Function Tcontrol.Perform (msg: cardinal; wparam, lparam: longint): Longint

2. Windows API Functions SendMessage () and PostMessage (). Its statement 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; stdcall

The PostMessage function adds messages 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.

VCL message processing mechanism

There is a statement Application.Run 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. Response Application.onMessage event must be a TMESSAGEVENT type whose statement is as follows: Type TMESSAGEVENT = Procedure (var Msg: TMSG; VAR HANDED: Boolean) of object;

The TMSG is a message record defined in Windows, which we can declare this:

Procedure ONMYMESSAGE (VAR MSG: TMSG; VAR HANDED: BOOLEAN);

Then give this method to the Application.onMessage event:

Application.onMessage: = OnMyMessage;

The OnMessage event will capture all messages sent to the application, which is a very busy event, so set breakpoints during processing onMessage events, is unwise.

The method of the VCL object is used to receive the message is MAINWNDPROC. It is a static method defined in the TwinControl class and cannot be overloaded. It does not directly process the message. When the message leaves the mainwndProc, the message is passed to the object's WndProc method, and the WndProc method is a virtual method defined in the TControl class, which is called the Dispatch method. Dispatch looks for the corresponding processing method according to the incoming Message. If it is not found, continue to the parent class to find the message processing method, until the found, if you can't find the defaulthandler. The DEFAULTHANDLER method performs the last process of the message and then passes the message to the Windows DefWindowProc function or other default window process.

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

New Post(0)