11. Custom message in MFC

Source: Internet
Author: User

MFCCustom message

the message ing and loop mechanism are Windows BASIC Program running mode. VC ++ MFC there are many ready-made message handles. You can customize messages when you need to complete other tasks, I encountered some difficulties. In MFC classwizard User-defined messages cannot be added. Therefore, you must manually add the corresponding Code to the program, this allows you to process custom messages like other messages.

The procedure for customizing a message is as follows (for example ):

(1) CreateSingle DocumentOfMFC ApplicationProject name:Mymessage

(2) Custom message:

Step 1: Define a message

InResource. hAdd the following code:

//The recommended custom message must be at leastWm_user+ 100Because many new controls also need to be usedWm_userMessage.

# Define wm_my_message (wm_user+ 100)

Step 2: declare the Message Processing Function

SelectCmainframeClass to add a message processing function

InMainfrm. hFile, ClassCmainframeAnd declare the message processing function. The Code is as follows::

Protect:

Afx_msg lresult onmymessage (wparam, lparam );

Step 3: Implement Message Processing functions

InMainfrm. cppAdd the following code to the file:

Lresult cmainframe: onmymessage (wparam, lparam)

{

// Todo: add your message Handle Code

Return 0;

}

Step 4:CmainframeIn the message block of the class, useOn_messageMacro commands map messages to message processing functions

Begin_message_map (cmainframe, cframewnd)

On_wm_create ()

On_message (wm_my_message, onmymessage)

// On_registered_message (wm_my_message, onmymessage)

end_message_map ()

if you need a message that uniquely defines the entire system , call SDK Functions registerwindowmessage define a message :

InResource. hSet the code

# Define wm_my_message (wm_user+ 100)

Replace:

Static uint wm_my_message = registerwindowmessage (_ T ("user "));

And useOn_registered_messageMacro commands replaceOn_messageMacro commands,The remaining steps are the same as above.

note: if you still use the on_message macro command, compile you can use, but cannot respond to the message.

When custom messages are required,You can call a function in a function of the corresponding class.PostmessageOrSendmessageSend messagePosemessage (wm_my_message, O, O).

Registerwindowmessage Function

The registerwindowmessage function defines a new window message that is guaranteed to be unique throughout the system.

The message value can be used when sending or posting messages.

Syntax

Uint registerwindowmessage (lpctstr lpstring

);

Remarks

The registerwindowmessage function is typically used to register messages for communicating between two cooperating applications.

If two different applications register the same message string, the applications return the same message value.

the message remains registered until the session ends.

//If twoApplicationUse the sameStringRegisterMessage, They will wait until the sameMessageValue, that is, get the sameMessage

Reference

[1]Http://www.cnblogs.com/xulei/archive/2007/11/22/968170.html

[2]Http://www.cnblogs.com/pbreak/archive/2010/06/05/1752333.html

[3]Msdn

[ 4 ] sun Xin's book VC ++ in-depth"

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.