From Delphi-stdio

Source: Internet
Author: User
Delphi is an object-oriented visual software development tool developed by Borland. Delphi focuses on the advantages of Visual C ++ and Visual Basic: easy to use, powerful functions, especially in interface design, database programming, network programming has its unique advantages.
Messages in Delphi

A message is a notification sent by windows, which tells the application that an event has occurred. In Delphi, Windows messages are encapsulated in VCL events in most cases. We only need to handle the corresponding VCL events, however, if we need to write our own controls, intercept or filter messages, we must thoroughly study the Win32 message processing mechanism.

In Delphi, messages are defined as tmessage records. Open the message. Pas file. We can see that tmessage is defined as follows:

Type

Tmessage = packed record

MSG: Cardinal;

Case INTEGER

0: (wparam: longint;

Lparam: longint;

Result: longint );

1: (wparamlo: word;

Wparamhi: word;

Lparamlo: word;

Lparamhi: word;

Resultlo: word;

Resulthi: Word );

End;

MSG is a constant value different from other messages. These values can be pre-defined constants in Windows units or custom constants. Wparam is a message-related constant value, or a handle of a window or control. Lparam is usually a pointer to data in the memory.

Result is the return value of message processing. Wparam, lparam, and result are both 32-bit. If you want to access low 16-bit or high 16-bit, you can use wparamlo, wparamhi, lparamlo, lparamhi, resultlo, and resulthi respectively.

In Delphi, in addition to common tmessage, a special message record is also defined for each windows. We can browse the message. Pas file. below is the message record of the keyboard:

Twmkey = packed record

MSG: Cardinal;

Charcode: word;

Unused: word;

Keydata: longint;

Result: longint;

Keyboard-related messages such as wm_keydown, wm_keyup, wm_char, wm_syskeydown wm_syskeyup, and wm_syschar are also defined as twmkey. The message. Pas file contains the following statements:

Twmchar = twmkey; twmkeydown =

Twmkey; twmkeyup = twmkey; twmsys

-Keydown = twmkey; twmsyskeyup =

Twmkey; twmsyschar = twmkey;

Message sending

Message Processing defines how an application responds to Windows messages. In Delphi, each message has its own processing process. It must be a method in an object and can only pass one tmessage or other special message records, after the method declaration, there must be a message command followed by a constant between 0 and 32767.

All the messages we mentioned earlier are standard Windows messages (wm_x). In addition, there are VCL Internal messages, notification messages, and user-defined messages.

A vcl internal message usually starts with "cM _" and is used to manage things inside the VCL. If you change a property value or other features of a component, You need to notify other components of the change through internal messages. For example, an active input focus message is sent to an activated or deactivated component to accept or discard the input focus.

In addition, there is a notification message. The child control in a window has some things and needs to notify the parent window, which is implemented through the notification message. It is only applicable to standard window controls, such as buttons, list boxes, and edit boxes. Open the message. Pas file, which is the notification message declaration after the standard windows:

Const

{$ Externalsym bn_clicked}

Bn_clicked = 0;

{$ Externalsym bn_paint}

Bn_paint = 1;

{$ Externalsym bn_hilite}

Bn_hilite = 2;

The above is the notification message of the button, indicating that the user has clicked the button, the button should be re-painted, and the user has highlighted the button.

You can also define the message, send the message to yourself, and write the message processing process. The message's constant value is wm_user + 100 to $ 7fff, which is reserved by windows for custom messages.

There are three methods to send a Delphi message:

1. tcontrol class perform object method. You can send messages to any form or control. You only need to know the instances of the form or control. The statement is as follows:

Function tcontrol. Perform (MSG: Cardinal; wparam, lparam: longint): longint

2. Windows API functions sendmessage () and postmessage (). The 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 a message to the Message Queue of the application. The message loop of the application extracts the registered message from the message queue and sends it to the corresponding window.

The sendmessage function can directly send messages to the window process beyond the message queue. Therefore, sendmessage is used when Windows needs to return values immediately, and postmessage is used when different applications need to process messages in sequence. In essence, perform is similar to sendmessage, and they are directly sent to the window process. The sendmessage and postmessage functions can send messages only by knowing the handle of the window. Therefore, they can send a message to a non-Delphi form, but the perform must know the instances of the form or control.

VCL Message Processing Mechanism

In the source code of the Delphi application, the statement application. Run is used to start the message loop and then call application. processmessage. This function searches for a message in the message queue of the application. When a message is retrieved in the message queue, the application. onmessage event is triggered. In this way, before Windows processes messages, it will respond to the onmessage event processing process, which is superior to any message processing and only receives registered messages, that is, the message sent by postmessage. The process for responding to the application. onmessage event must be tmessageevent. The declaration is as follows:

Type tmessageevent = procedure (var msg: tmsg; var handled: Boolean) of object;

Where tmsg is a message record defined in windows, we can declare it as follows:

Procedure onmymessage (var msg: tmsg; var handled: Boolean );

Then assign this method to the application. onmessage event:

Application. onmessage: = onmymessage;

The onmessage event captures all messages sent to the application. This is a very busy event. Therefore, it is unwise to set breakpoints for message processing during onmessage event processing.

The method used by the VCL object to receive messages 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 mainwndproc, the message is passed to the wndproc method of the object. The wndproc method is a virtual method defined in the tcontrol class, which calls the dispatch method. Dispatch finds the corresponding processing method based on the received message. If it cannot be found at the end, it proceeds to the parent class to find the message processing method until it is found. If it cannot be found, it calls defaulthandler. The defaulthandler method performs the final processing of the message and then transmits the message to the defwindowproc function of windows or other default window procedures.

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.