Windows custom message

Source: Internet
Author: User

Almost all tasks of Windows applications are based on message processing. Messages in windows are classified into common Windows messages, control notification messages, and commands. However, sometimes we need to define our own messages to notify the program of what happened. This is the custom message. Classwizard does not provide the function of adding custom messages. Therefore, to use custom messages, you must write code manually. Then, classwizard can process custom messages just like other messages. The procedure is as follows:

Step 1: Define a message. When a message is actually used to develop a Windows 95 application, Microsoft recommends that you customize the message at least wm_user + 100, because many new controls also use wm_user messages.

Step 2: Implement Message processing functions. This function uses wpram and lparam parameters and returns lpesult.

Lpesult cmainframe: onmymessage (wparam, lparam) {// todo: process user-defined message afxmessagebox ("process user-defined message"); Return 0 ;}

Step 3: Describe the message processing function in the afx_msg block of the Class header file:
Class cmainframe: Public cmdiframewnd {

...

// General message ing Function

Protected:

// {Afx_msg (cmainframe)

Afx_msg int oncreate (maid );

Afx_msg void ontimer (uint nidevent );

Afx_msg lresult onmymessage (wparam, lparam );

//} Afx_msg

Declare_message_map ()}

Step 4: Use the on_message macro command in the message block of the user class to map the message to the message processing function.
Begin_message_map (cmainframe, cmdiframewnd)

// {Afx_msg_map (cmainframe)

On_wm_create ()

On_wm_timer ()

On_message (wm_my_message, onmymessage)

//} Afx_msg_mapend_message_map ()

In this way, a user-defined message can be used. If you need a unique message in the system, you can call the SDK function registerwindowmessage and use the on_register_message macro command to replace the on_message macro command. The remaining steps are the same as above.

 

At the top of the. cpp File
# Define wm_mymessage wm_user+ 100
In the. h file
// {Afx_msg (cmydlg) // automatically generated
Afx_msg void onmymessage (); //
///} Afx_msg // automatically generated.
In the. cpp File
Begin_message_map (cmydlg, cdialog) // automatically generated
// {Afx_msg_map (cmydlg) // automatically generated
On_message (wm_mymessage, onmymessage) //
///} Afx_msg_map // automatically generated
End_message_map () // automatically generated

Then define the Function
Void cmydlg: onmymessage ()
{
Afxmessagebox ("this is my message ");
}

Finally, the message is sent. If you do not need the API
Sendmessage (wm_mymessage, 0, 0 );
If you use an API
Sendmessage (window handle, wm_mymessage, 0, 0)

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.