首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

类中消息映射的有关问题

2012-04-05 
类中消息映射的问题请大家帮我看看了,小妹很困惑啊写好了一个有消息映射的程序,经过测试,可以实现.现在把

类中消息映射的问题
请大家帮我看看了,小妹很困惑啊
写好了一个有消息映射的程序,经过测试,可以实现.

现在把它要封装成一个类,结果消息映射就不能触发函数了

写了一个继承与CWnd的类Modem/

Modem.h
//{{AFX_MSG(Modem)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg LRESULT OnCommNotify1(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

Modem.cpp
BEGIN_MESSAGE_MAP(Modem, CWnd)
//{{AFX_MSG_MAP(Modem)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_MESSAGE(mymsg, OnCommNotify1)
//}}AFX_MSG_MAP

END_MESSAGE_MAP()

在线程中发送postmessage时就不能触发OnCommNotify1.

Modem::Modem()
{
 
  HWNDghWnd=AfxGetMainWnd()->GetSafeHwnd();  
}
在线程中:
PostMessage(pModem->ghWnd, WM_COMMNOTIFY, dwMask, 0);
编译时pModem->ghWnd为0.不能触发了.


[解决办法]
在线程中: 
PostMessage(pModem->m_hWnd, WM_COMMNOTIFY, dwMask, 0); 

另:
Modem.cpp 
BEGIN_MESSAGE_MAP(Modem, CWnd) 
//{{AFX_MSG_MAP(Modem) 
// NOTE - the ClassWizard will add and remove mapping macros here. 
ON_MESSAGE(WM_COMMNOTIFY, OnCommNotify1) 
//}}AFX_MSG_MAP 


[解决办法]
另外我还有一个不明白的地方,你既然已经得到了类Modem的对象指针了,何必还要通过向它发送消息来调用它的函数呢?直接调用不是更直接。
[解决办法]
很简单啊。。你把这个消息发送给主窗口。。不是这个类。。然后主窗口调用你要调用的函数就可以了。。

热点排行