龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

MFC中自由使用自定义消息(4)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
第四步:在视类中添加自定义消息: 在头文件MessageView.h中添加消息映射 protected: //{{AFX_MSG(CMessageView) //}}AFX_MSG afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

  第四步:在视类中添加自定义消息:

  在头文件MessageView.h中添加消息映射

protected:
//{{AFX_MSG(CMessageView)
//}}AFX_MSG
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); //此行为添加代码
DECLARE_MESSAGE_MAP()
在视类文件MessageView.cpp中的消息映射中添加自定义消息映射
BEGIN_MESSAGE_MAP(CMessageView, CView)
//{{AFX_MSG_MAP(CMessageView)
//}}AFX_MSG_MAP
// Standard printing commands
ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage) //此行添加代码定义唯一消息
END_MESSAGE_MAP()

  添加相应的0消息处理函数

LRESULT CMessageView::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
CRect rect;
GetClientRect(&rect);
InvalidateRect(&rect);
test=!test;
return 0;
}

  在MessageView.h中添加布尔变量 public:BOOL test;

  在视类构造函数中初始化 test变量:test=FALSE;

  修改CMessageView::OnDraw()函数

void CMessageView::OnDraw(CDC* pDC)
{
CMessageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// 以下程序显示消息响应效果
if(test)
pDC->TextOut(0,0,"消息响应!");
}

  第五步:显示测试对话框

  在MainFrame类中包含对话框头文件:

#include "TestDialog.h";
OnTest()函数中添加代码
void CMainFrame::OnTest()
{
TestDialog dialog;
dialog.DoModal();
}

  运行程序,在测试菜单打开对话框,点击测试按钮即可看到结果。

精彩图集

赞助商链接