User defined messages
Here is the code:
//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);
Why doesn't the program call the handler?
u开发者_运维百科pd: WinXP, MS VS 2003
Maybe you are calling SendMessage() from a method not of MyClass, but of MyMainFrame, for example, so you are sending the message to the wrong window. If that is the case, simply add the member variable:
m_myView.SendMessage(WM_USER+100,0,0);
Also you did forget the MyClass:: from:
LRESULT MyClass::OnMyMess(WPARAM, LPARAM)
{return 0};
First,
LRESULT OnMyMess(WPARAM, LPARAM)
{return 0;}
should be
LRESULT MyClass::OnMyMess(WPARAM, LPARAM)
{return 0;}
but I guess it's just a typo.
Second, SendMessage
should work as expected, only if it's in the MyClass that you're calling it; otherwise, you should specify the window to which you want to send the message.
Try:
//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);
精彩评论