Three types of Windows messages

Source: Internet
Author: User

Three types of Windows messages
1. Standard message
Macro name-to-message message-handler function (name is preset by system)
On_wm_charwm_cahronchar
On_wm_closewm_closeonclose
On_wm_createwm_createoncreate
On_wm_destroywm_destroyondestroy
On_wm_lbuttondownwm_lbuttondownonlbuttondown
On_wm_lbuttonupwm_lbuttonuponlbuttonup
On_wm_mousemovewm_mousemoveonmousemove
On_wm_paintwm_paintonpaint
...

such as: Keyboard messages
Keyboard messages are one of the most frequently used messages in an application.
When the user uses the keyboard to manipulate the application, the keyboard message is sent.
In general, there are 3 main keyboard messages in Visual C + +.

WM_CHAR: The message's handler function OnChar ().
Wm_keydown: The user presses a non-system key.
Wm_keyup: Generated when a non-system key is released.

such as: Mouse messages
Similar to keyboard messages, mouse messages are one of the most frequently used messages in Windows applications.
In general, there are several main mouse messages in Windows applications.

Wm_mousemove: The user moves the mouse over the window or moves through the window.
Wm_lbuttondown: The user presses the left button.
Wm_lbuttonup: The user releases the left button.
Wm_lbuttondbclick: User left-click.
Wm_rbuttondown: The user presses the right button.
Wm_rbuttonup: The user releases the right button.
Wm_rbuttondbclick: User Double-click right button.

Such as: Window message
A window message is a message that is sent when certain actions are made to the Windows Application's window.
In Visual C + + 6.0, the following 6 window messages are included.

Wm_create: Window is created.
Wm_destroy: Window is destroyed.
Wm_close: Window is closed.
Wm_move: The window has moved.
Wm_size: The window has changed.
WM_PAINT: The window has been redrawn.

Such as: Focus message
The focus message is when an object in the window gets focus and loses focus.
Corresponds to the action triggered by the window or object. In general, there are only the following two focus messages.

Wm_setfocus: Window gets focus.
Wm_killfocus: Window loses focus.
When a window changes from inactive to an active state with input focus,
It will receive wm_setfocus messages,
And when a window loses its input focus becomes inactive, it receives a wm_killfocus message.

such as: Timer message
Windows timer is a periodic message generation device,
When the user sets a timer resource for a window, the system will press
The specified interval sends a timer message to the window, in this message
Can handle things that need to be dealt with regularly.
。 There is only one type of Timer message: Wm_timer, whose response function is OnTimer.
For the operation of the timer, it is usually used in conjunction with the function SetTimer ().
Before responding to the OnTimer function, you should add a timer to trigger it.
If the timer is not established, the system does not trigger the OnTimer function itself,
The purpose of the SetTimer () function is to set the timer.

2. Command message
The general mapping rules for Command messages (WM_COMMAND) are:
On_command (<id>,<memberFxn>)
For example:
On_command (Idm_about,onabout)

3. Control notification message
[Notification message] The mapping mechanism (generated by control components, such as bn_xxx) is divided into several types of macros
(Because the control components are already divided into several types), the following examples represent
Control component Macro Name message handler function
Buttonon_bn_clicked (<id>,<memberFxn>) memberfxn
COMBOBOXON_CBN_DBLCLK (<id>,<memberFxn>) memberfxn
Editon_en_setfocus (<id>,<menberFxn>) memberfxn
LISTBOXON_LBN_DBLCLK (<id>,<memberFxn>) memberfxn

Each message processing function should be in the form of afx_msg void

Message map and message response:
MFC uses the message map mechanism to replace the switch-case structure in the C + + language to process messages.

The MFC Message-mapping mechanism includes a set of message-map macros. A message map macro puts a Windows message and its message at the
The function is linked together.

In the implementation source file for the class
Begin_message_map (Derived Class,base Class)
And
End_message_map () macro to define the message map.

The use of a message map is declared at the end of the class definition with a declare_message_map () macro.

Begin_message_map (Theclass, BaseClass)
{{Afx_msg_map (Theclass)
On_ ... MFC pre-defined message map macros
On_message (MESSAGE, MEMBERFXN)
User-defined message map macros
. . . . . .
}}afx_msg_map
End_message_map ()
Attention:
The special annotation "//{{afx_msg_map" is the tag that the ClassWizard Class Wizard uses to maintain message-map macros.
Users do not delete annotations to easily modify the code within the annotations.

4. User-defined message
1. First define the message in the class implementation file.
#define Wm_mymessage wm_user+100

2. Declare the message handler function in the class.
afx_msg LRESULT onmymessage (WPARAM wparam,lparam LPARAM)

3. Add a mapping entry in the message map table of the class.
On_message (Wm_mymessage, OnMyMessage)

4. Implement the message handler function on the class.

5. Send a message using the SendMessage function or PostMessage
SendMessage (wm_mymessage,0l,0l);

Three types of Windows messages

Related Article

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.