VC Use SendMessage Custom message function

Source: Internet
Author: User
Tags thread

This article mainly introduces the VC in the use of SendMessage custom message functions of the relevant information, the need for friends can refer to the

The basic structure of the SendMessage is as follows:

?

1 2 3 4 5 6 SendMessage (HWND hwnd,//Message delivery target window or thread handle.) UINT MSG,//message categories (This can be some system messages, also can be their own definition, described below,) WPARAM WPARAM,//Parameter 1 (WPARAM is actually the same type as UINT,////////////////////////////// Options are available for viewing. LPARAM LPARAM); Parameter 2

Some of these parameters are as follows:

typedef unsigned int UINT;

typedef UINT WPARAM;

typedef LONG LPARAM;

typedef LONG LRESULT;

For example, you can use the following statement:

?

1 :: SendMessage (This->m_hwnd, Wm_my_dosome, (WPARAM) 0, (LPARAM) 0);

The message I sent here is received by this form, so the handle is: This->m_hwnd

The message category Wm_my_dosome here is my custom,

In the header file of the form or thread where the message is received:

?

1 #define Wm_my_dosome wm_user+1//do something

Of course you can also define more such as:

?

1 #define Wm_doother wm_user+2//Do other

Say something to do.

Here, maybe everyone is still a little vague about the message category, don't worry, I'll talk about it soon.

We sent out a message, so the receiver had to be able to identify what the message was for, or to differentiate it by the message category,

and start to do the information that corresponds to the things to be dealt with. As follows:

One: Write one thing:

We define one such thing (process) in the receiving form,

?

1 2 3 4 5 6 afx_msg lresult dosomething (WPARAM iparam1,lparam iParam2) {MessageBox ("received the message, I'm going to start doing something." "," received ", MB_OK); You can use IPARAM1,IPARAM2 to do something. return 0; }

This thing has 3 points to pay attention to, very important:

1: afx_msg is used, and afx_msg lresult dosomething (WPARAM iparam1,lparam)

Overwrite the header file

{{afx_msg

。。。 Rewrite it here, the color will turn gray. This is very important.

}}afx_msg

2: There are 2 parameters, WPARAM Iparam1,lparam iParam2, even if there is no thing to write, or will suffer, the VC will not remind you to write a less,

But some inexplicable and wonderful things will happen.

3: Type with LRESULT, finished to return 0;

Second: Let the receiver know when to do this thing:

We are

?

1 2 3 4 5 6 {{afx_msg_map//... Here's a on_message (wm_my_dosome,dosomething)///If there's another message, write another on_message (wm_doother,doother)//}}afx_msg_map

Here, when you use SendMessage to send out a wm_my_dosome type of message over,

The receiver is going to do dosomething (WPARAM iparam1,lparam iParam2)

Sent a wm_doother type of news over the time,

The receiver will do the DoOther (WPARAM iparam1,lparam iParam2) Of course, here doother I have not defined,

This is a complete message send and receive process, I did not detail the parameters, iParam1, because it has not been used in a very complicated situation,

We can improve together if we have a heart.

In the header file.

Copy code code as follows:

#define WM_MYMSG wm_user+5//Customize a message

afx_msg void OnMyMessage (WPARAM WPARAM, LPARAM LPARAM); Custom message processing function declaration

In the. cpp file

On_message (wm_mymsg, OnMyMessage)

Using the On_message () macro to establish a mapping relationship between a custom message and its processing function

void Cmodelessdlg::onmymessage (WPARAM WPARAM, LPARAM LPARAM)

Remove the CString object's pointer from the lparam and display the string contents in the Idc_msgedit

The above mentioned is the entire content of this article, I hope you can enjoy.

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.