自创的对话框类向视图类发送自定义消息
更新时间:2023-10-29 21:56:01 阅读量: 综合文库 文档下载
- mfc对话框类推荐度:
- 相关推荐
VC中自定义消息的方法
以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView) 发送自定义消息为例,说明这两种不同方法的自定义消息的 消息传递的方法一:使用ON_MESSAGE
使用ON_MESSAGE响应消息,必须配合定义消息#define WM_MY_MESSAGE (WM_USER+100) 对于发送消息者-MyMessageDlg,
在其MyMessageDlg.h中,定义#define WM_MY_MESSAGE (WM_USER+100) 在其MyMessageDlg.cpp中要先添加:#i nclude \因为使用了CMainFrame*定义对象。 并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg() {
// TODO: Add your control notification handler code here
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针 CView * active = pMF->GetActiveView();//才能获取当前视类指针 if(active != NULL) //获取了当前视类指针才能发送消息
active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息 }
对于消息的接受者-MessageTestView,
在其MessageTestView.h中,也要定义#define WM_MY_MESSAGE (WM_USER+100) 并定义消息映射函数-OnMyMessage() protected:
//{{AFX_MSG(CMessageTestView)
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); //}}AFX_MSG
DECLARE_MESSAGE_MAP() 在其MessageTestView.cpp中, 先要声明响应消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView) //{{AFX_MSG_MAP(CMessageTestView)
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage) //}}AFX_MSG_MAP
再添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam) {
MessageBox(\return 0; }
消息传递的方法二:使用ON_REGISTERED_MESSAGE 使用ON_REGISTERED_MESSAGE注册消息,必须配合
static UINT WM_MY_MESSAGE=RegisterWindowMessage(\
对于消息的发送者-MyMessageDlg, 在其MyMessageDlg.h中,只要
定义static UINT WM_MY_MESSAGE=RegisterWindowMessage(\就可以了。
在其MyMessageDlg.cpp中要先添加:#i nclude \因为使用了CMainFrame*定义对象。 并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg() {
// TODO: Add your control notification handler code here
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针 CView * active = pMF->GetActiveView();//才能获取当前视类指针 if(active != NULL) //获取了当前视类指针才能发送消息
active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息 }
对于消息的接收者-MessageTestView, 在其MessageTestView.h中不要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage(\应该把这个定义放到MessageTestView.cpp中,要不会出现: redefinition 在其MessageTestView.h中只要定义消息映射函数 protected:
//{{AFX_MSG(CMessageTestView)
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); //}}AFX_MSG
DECLARE_MESSAGE_MAP()
在其MessageTestView.cpp中,先定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage(\接着注册消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView) //{{AFX_MSG_MAP(CMessageTestView)
ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage) //}}AFX_MSG_MAP
最后添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam) {
MessageBox(\return 0; }
----------------------------------------------------------------
比较两种方法,只是略有不同。但也要小心谨慎,以免出现接收不到消息的情况。 ------------------------------------------------------------------- 其他注意事项:
发送消息的-MyMessageDlg.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage(\
接受消息的-MessageTestView.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage(\
RegisterWindowMessage(\中\的内容是什么不重要,写什么都可以,但是 发送者与接受者必须是一样的内容,例如:\
转自:http://www.cppblog.com/xpzhou/archive/2007/04/18/22185.html
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 自创
- 视图
- 对话框
- 发送
- 定义
- 消息