The biggest feature of Windows is its graphical operation interface, which is based on its message processing mechanism. If you do not understand the WINDOWS Message Processing Mechanism, you cannot fully understand windows programming. Unfortunately manyProgramI have only heard about Windows messages, but I do not know much about their usage and the internal implementation principles. This article attempts to disclose the Windows message mechanism I understand step by step. It can be said that mastering this part of knowledge is a powerful weapon in Windows programming. Using it flexibly will greatly improve our programming capabilities.
1. Message Overview
How does a Windows form appear on the screen? As we all know, it is implemented through API plotting. The Windows operating system provides a series of API functions to draw the interface, such:
Draw text using drawtext
Draw a border using drawedge
Drawicon draw icon
Bitblt
Rectangle
...
The complex program interface is implemented through this function.
So when will these functions be called? Obviously, we need a control center for "issuing orders". We also need a command communication mechanism to instantly deliver commands to the destination. This control center is a power source, like a heart, which continuously sends blood everywhere. This command transmission mechanism is a Windows message mechanism. A Windows message is like the blood in the body. It is the messenger of command communication.
Windows message control center is generally a three-tier structure with the Windows Kernel at the top. The Windows Kernel maintains a message queue. The second-level control center obtains messages under its jurisdiction from the message queue and then processes them. Some messages are directly processed, some of them also need to be sent to the next level form (window) or control ). The second-level control center is generally the application objects of various Windows applications. The third-level control center is a Windows form object. Each form has a default form process, which processes various received messages.
A message is sent to an application in a fixed structure. The structure is as follows:
Public type msg
Hwnd as long
Message as long
Wparam as long
Lparam as long
Time as long
Pt as pointapi
End type
Hwnd is the form handle, and message is a message constant used to indicate the type of the message. wparam and lparam are both 32-bit additional information, it depends on the message type. Time is the time when the message is sent, and PT is the cursor position when the message is sent.
Windows OS includes the following messages:
1. Standard Windows messages:
This type of message starts with WM.
2. Notification Message
Notification messages are messages for standard Windows controls. These controls include buttons, ComboBox, Textbox, ListBox, listview, Treeview, and toolbar), menu, etc. Each type of message starts with a different string.
3. Custom messages
Programmers can also customize messages.
Ii. Windows handle
Not every control can receive messages, forward messages, and draw itself. Only a control with a handle can do this. Controls with handles are essentially a window. They can exist independently and can be used as containers of other controls. controls without handles, such as labels, cannot exist independently, it can only be used as a child control of the window control. It cannot draw itself, but can only be drawn by the parent form.
The handle is essentially a 32-bit value automatically maintained by the system. This value is unique at any time of the operating system. However, after the form indicated by the handle is released, the handle is released, which may be used by other forms. That is to say, the value of the handle is dynamic, and it is only a unique identifier. The operating system uses the handle to identify and find the objects it represents.
However, not all handles are form handles. in windows, there are many other types of handles, such as the HDC handle, paint brush handle, and paint brush handle, application handle (hinstance. Such a handle cannot receive messages. No matter which handle is used, it is the unique identifier of an object in the system. This article only discusses the form handle.
So why does the handle make the window so unique? It is actually because of the message. With a handle, the form can receive messages, so it knows when to draw itself, draw the child control, and know when to click the part of the window, and then proceed accordingly. A handle is like a person's ID card. With it, You can engage in various social activities. Otherwise, you will either be a hacker invisible to the society or be behind others, prove your existence by others.
Iii. message transmission
1. Get a message from the message queue:
You can use the peekmessage or getmessage function to retrieve messages from the Windows message queue. Message Queues stored in windows are grouped by threads, that is, each thread has its own message queue.
2. Send messages
The following two functions are used to send messages to a specified form: sendmessage and postmessage. The difference between the two functions is that the postmessage function only adds a message to the thread message queue. if the message is successfully added, true is returned. Otherwise, false is returned, whether the message is processed, or whether the message is processed, I don't know. Sendmessage is somewhat different. Instead of adding a message to a queue, sendmessage directly translates the message and calls the message for processing until the message is processed. Therefore, if we want to send a message to be executed immediately, we should call sendmessage.
Another point is that because the message sent by sendmessage is not added to the message queue, the message sent by sendmessage cannot be obtained through peekmessage or getmessage.
In addition, some messages won't succeed with postmessage, such as wm_settext. Therefore, not all messages can use postmessage.
There are other message sending API functions, such as postthreadmessage, sendmessagecallback, sendmessagetimeout, and sendpolicymessage.
Iv. Message loop and Form Process
Message loops are the root cause for the persistence of applications. If the application exits cyclically, the application ends.
Let's take a look at the encapsulated message loop in Delphi:
Step 1: run the program)
Application. initialize; // Initialization
Application. createform (tform1, form1); // create the main form
Application. Run; // start running and prepare for message loop
If you do not create a main form, the application can also exist and run.
Step 2: Start to call the handlemessage)
Procedure tapplication. Run;
Begin
Frunning: = true;
Try
Addexitproc (doneapplication );
If fmainform <> nil then
Begin
Case cmdshow
Sw_showminnoactive: fmainform. fwindowstate: = wsminimized;
Sw_showmaximized: mainform. windowstate: = wsmaximized;
End;
If fshowmainform then
If fmainform. fwindowstate = wsminimized then
Minimize else
Fmainform. Visible: = true;
Repeat // note: the cycle starts.
Try
Handlemessage;
Except
Handleexception (Self );
End;
Until terminated; // The condition for loop end
End;
Finally
Frunning: = false;
End;
End;
Step 3: process messages in a message loop.
Procedure tapplication. handlemessage;
VaR
MSG: tmsg;
Begin
If not processmessage (MSG) Then idle (MSG );
End;
Function tapplication. processmessage (var msg: tmsg): Boolean;
VaR
Handled: Boolean;
Begin
Result: = false;
If peekmessage (MSG, 0, 0, 0, pm_remove) then
Begin
Result: = true;
If MSG. Message <> wm_quit then
Begin
Handled: = false;
If assigned (fonmessage) Then fonmessage (MSG, handled );
If not ishintmsg (MSG) and not handled and not ismdimsg (MSG) and
Not iskeymsg (MSG) and not isdlgmsg (MSG) then
Begin
Translatemessage (MSG );
Dispatchmessage (MSG );
End;
End
Else
Fterminate: = true;
End;
End;
The form process is actually a callback function. The so-called callback function is actually a function called by a Windows operating system or an external program. Callback functions generally have a specified parameter format, which is passed to the caller in address mode. The window is called by the Windows operating system. When creating a window, you need to input the callback function address when allocating the form handle. Why can't we see this callback function during programming? This is because our programming tool has generated a default form process for us. The only thing we need to do in this process is to judge different message types and then make different processing. For example, an event can be generated for keyboard or mouse input.
5. Messages and events
Events are essentially message encapsulation and are a useful tool provided by the IDE programming environment to simplify programming. This encapsulation is implemented in the form process. Each ide encapsulates many Windows messages, such:
Event
Message
Onactivate
Wm_activate
Onclick
Wm_xbuttondown
Oncreate
Wm_create
Ondblclick
Wm_xbuttondblclick
Onkeydown
Wm_keydown
Onkeypress
Wm_char
Onkeyup
Win_keyup
Onpaint
Wm_paint
Onresize
Wm_size
Ontimer
Wm_timer
After understanding this, We can encapsulate our own events.
Based on the above introduction, I believe you have understood the Windows message mechanism. Through message programming in windows, we can not only implement many common functions, but also implement functions not provided by many ide class libraries. In addition, we can intercept messages through message hooks, change its default processing functions to break through the restrictions of platform or software functions and greatly extend the functions of the program. We can also modify the default form process to respond to messages as required; or customize messages to implement instant communication between programs. Through more in-depth study, we will also be exposed to more basic Windows-related information about the windows messaging mechanism, you will find yourself not far from the "master.