用户定义的消息

时间:2011-07-28 12:57:24

标签: c++ mfc sendmessage

以下是代码:

//h file
class MyClass: public CView
{
public:
    afx_msg LRESULT OnMyMess(WPARAM, LPARAM);
}

//cpp file
BEGIN_MESSAGE_MAP(MyClass, CView)
    ON_MESSAGE(WM_USER+100, OnMyMess)
END_MESSAGE_MAP()

LRESULT OnMyMess(WPARAM, LPARAM)
{return 0};


//Somewhere in the programm
SendMessage(WM_USER+100, 0 ,0);

为什么程序不调用处理程序?

upd:WinXP,MS VS 2003

3 个答案:

答案 0 :(得分:3)

例如,您可能从非MyClass的方法调用SendMessage(),而是调用MyMainFrame,因此您将消息发送到错误的窗口。 如果是这种情况,只需添加成员变量:

m_myView.SendMessage(WM_USER+100,0,0);

你也忘记了 MyClass ::

LRESULT MyClass::OnMyMess(WPARAM, LPARAM)
{return 0};

答案 1 :(得分:2)

首先,

LRESULT OnMyMess(WPARAM, LPARAM)
{return 0;} 

应该是

LRESULT MyClass::OnMyMess(WPARAM, LPARAM)
{return 0;}

但我猜这只是一个错字。

其次,SendMessage应该按预期工作,只有当它在您调用它的MyClass中时才会工作;否则,您应指定要向其发送消息的窗口。

答案 2 :(得分:0)

尝试:

//h file
#define CUSTOM_MESSAGE WM_USER + 100

class MyClass: public CView
{
public:
    afx_msg LRESULT OnMyMess(WPARAM, LPARAM);
}

//cpp file
BEGIN_MESSAGE_MAP(MyClass, CView)
    ON_MESSAGE(CUSTOM_MESSAGE, OnMyMess)
END_MESSAGE_MAP()

LRESULT OnMyMess(WPARAM, LPARAM)
{return 0};


//Somewhere in the programm
SendMessage(CUSTOM_MESSAGE, 0 ,0);