消息映射机制 说明
1 声明宏 写到头文件中
2 分界宏 写到cpp源文件中
3 找消息宏 写到分界宏
4把函数原型写到 头文件中
5函数的实现写到cpp源文件
项目创建参考本人上一次博客
头文件
#include "afxwin.h" /*必须引用*/
class test:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class TestFrame :public CFrameWnd
{
public:
TestFrame();
//声明宏,提供消息映射机制
DECLARE_MESSAGE_MAP();
afx_msg void OnLButtonDown(UINT, CPoint);//鼠标点击
afx_msg void OnChar(UINT,UINT,UINT); //键盘按下
afx_msg void OnPaint();//绘图
};
源文件:
#include "test.h"
test _test;// 全局应用程序对象,有且仅有一个
BOOL test::InitInstance()
{
//创建窗口
TestFrame* frame = new TestFrame;
//显示
frame->ShowWindow(SW_SHOWNORMAL);
//更新
frame->UpdateWindow();
m_pMainWnd = frame;////保存指向应用程序的主窗口的指针
return true;
}
//这里分界宏
BEGIN_MESSAGE_MAP(TestFrame, CFrameWnd)
ON_WM_LBUTTONDOWN()//鼠标左键按下
ON_WM_CHAR() //键盘按下
ON_WM_PAINT()//绘图
END_MESSAGE_MAP();
TestFrame::TestFrame()
{
Create(NULL, TEXT("ONE"));
}
//鼠标左键实现
void TestFrame::OnLButtonDown(UINT, CPoint point)
{
/*TCHAR buf[1024];
wsprintf(buf, TEXT("x=%d,y=%d"), point.x, point.y);
MessageBox(buf);*/
CString str;
str.Format(TEXT("x=%d ....y=%d", point.x, point.y));
MessageBox(str);
}
//鼠标按下实现
void TestFrame::OnChar(UINT key, UINT, UINT) {
CString str;
str.Format(TEXT("按下了%c键"), key);
MessageBox(str);
}
//画图实现
void TestFrame::OnPaint()
{
CPaintDC dc(this);
dc.TextOutW(100, 100, TEXT("这里"));
}
结果: