vc中怎么使用SendMessage自定义消息函数

SendMessage的基本结构如下:
SendMessage(
    HWND hWnd,  //消息传递的目标窗口或线程的句柄。
    UINT Msg, //消息类别(这里可以是一些系统消息,也可以是自己定义,下文具体介绍,)
    WPARAM wParam, //参数1 (WPARAM 其实是与UINT是同种类型的,
  //在vc编译器中右键有个“转到WPARAM的定义”的选项可以查看。
    LPARAM lParam); //参数2
其中一些参数的由来如下:
//typedef unsigned int UINT;
//typedef UINT WPARAM;
//typedef LONG LPARAM;
//typedef LONG LRESULT;
例如可以用以下语句:
::SendMessage(this->m_hWnd, WM_MY_DOSOME, (WPARAM) 0, (LPARAM) 0);
这里我发送的消息是本窗体接收的,所以句柄用:this->m_hWnd
这里的消息类别WM_MY_DOSOME就是我自定义的,
在接收消息的窗体或线程所在的头文件里:
#define WM_MY_DOSOME WM_USER+1 // do something
当然你还可以定义更多如:
#define WM_DOOTHER WM_USER+2 // do other

表示要做一些事情。
到这里,可能大家还是对消息类别有点模糊,不要担心,我下面很快就讲到。
我们发了一个消息出去,那么接收方要能识别这个消息是干什么,就是通过消息类别来区分,
并且开始去做这个消息对应要处理的事情。如下:
一:编写一个事情:
我们在接收窗体里定义一个这样的事情(过程),
afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
{
 MessageBox("收到消息了,我要开始做一些事情了。","收到",MB_OK);
 //可以运用iParam1,iParam2 来做一些事情。
 return 0;
}
这个事情有3点大家要注意,非常重要:
1:使用了afx_msg,并且要将afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
改写到头文件的 
//{{AFX_MSG
//。。。改写到这里,颜色会变成灰的。这一点非常重要。
//}}AFX_MSG
2:参数有2个,WPARAM iParam1,LPARAM iParam2,哪怕没有东西传进来也要写,不然会吃苦头的,vc里不会提醒你少写了一个,
但一些莫名奇妙的事情会发生。
3:类型用 LRESULT,完了要return 0;
二:让接收方知道什么时候做这个事情:
我们在
//{{AFX_MSG_MAP
//。。。这里写上
ON_MESSAGE(WM_MY_DOSOME,DoSomeThing)
//如果还有其他消息就再写一个
ON_MESSAGE(WM_DOOTHER,DoOther)
//}}AFX_MSG_MAP
到这里,当你用SendMessage,发了一个WM_MY_DOSOME类型的消息过来的时候,
接收方就会去做DoSomeThing(WPARAM iParam1,LPARAM iParam2)
发了一个WM_DOOTHER类型的消息过来的时候,
接收方就会去做DoOther(WPARAM iParam1,LPARAM iParam2)当然,这里DoOther我还没有定义,
这样就是一个完整的消息发送与接受过程,我没有详细讲参数,iParam1,因为还没有用到很复杂的情况,

在头文件里
#define  WM_MYMSG  WM_USER+5  // 自定义一个消息

afx_msg 
void  OnMyMessage(WPARAM wParam, LPARAM lParam);  // 自定义消息的处理函数声明

在.cpp文件里
ON_MESSAGE(WM_MYMSG, OnMyMessage)
// 利用ON_MESSAGE()宏在自定义消息与其处理函数间建立映射关系

void  CModelessDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
// 从lParam中取出CString对象的指针,并将字符串内容在IDC_MSGEDIT中显示出来
{

    CString 
*str;
    str
=(CString *)lParam;

    SetDlgItemText(IDC_EDIT,
*str);
}



按下按钮发送消息
void  CModelessDlg::OnMsgBTN() 
{
    CString str
= "自定义消息被触发了!";

    SendMessage(WM_MYMSG, 
0, (LPARAM) &str);
 
//给ModelessDlg自己发一个自定义的消息

}



 附上MSDN上关于SendMsg的说明:

The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread's message queue and returns immediately.

LRESULT SendMessage(
  HWND hWnd,      // handle of destination window
  UINT Msg,       // message to send
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

Parameters
hWnd
Handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
Msg
Specifies the message to be sent.
wParam
Specifies additional message-specific information.
lParam
Specifies additional message-specific information.
Return Values

The return value specifies the result of the message processing and depends on the message sent.

 

Remarks

Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message.

Windows CE: Windows CE does not support all the messages the desktop Windows platforms support. Before using SendMessage, check to make sure the message you are sending is supported.

 

QuickInfo

  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 1.0 or later.
  Header: Declared in winuser.h.
  Import Library: Use user32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值