MFC学习笔记之消息映射

声明宏写到.h中,分界宏写到.cpp中


.h文件

#include<afxwin.h>
//mfc头文件

//应用程序类CWinApp,派生类(子类)
class MyApp : public CWinApp
{
public:
//基类的虚函数,派生类只是重写
//MFC程序的入口地址
virtual BOOL InitInstance();
};
//框架类CFrameWnd派生类(子类)
class MyFrame : public CFrameWnd
{
public:
MyFrame();//构造函数
//声明宏,声明消息映射,必须在类声明中,
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT,CPoint);
afx_msg void OnCHAR(UINT,UINT,UINT);
afx_msg void OnPaint();

};

.cpp文件:

#include"mfc.h"

MyApp app;

BOOL MyApp::InitInstance()
{
MyFrame * frame = new MyFrame;

frame->ShowWindow(SW_SHOWNORMAL);
frame->UpdateWindow();
m_pMainWnd = frame;
return TRUE;
}
//分界宏,其中有两个参数,the class ,base class(类和基类)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)

ON_WM_LBUTTONDOWN()//在头文件中要声明

ON_WM_CHAR()//键盘消息

ON_WM_PAINT()


END_MESSAGE_MAP()

MyFrame::MyFrame()
{
Create(NULL,TEXT("mfc"));
}
//在导入头文件#include<mfc.h>打不开,换种方式#include"mfc.h"就打开了
void MyFrame :: OnLButtonDown(UINT,CPoint point)
{
/*
TCHAR buf[1024];
wsprintf(buf,TEXT("x = %d , y = %d"),point.x,point.y);
MessageBox(buf);
*/

//mfc中的字符串Cstring
CString str;
str.Format(TEXT("x=%d,,,,,,,,,y=%d"),point.x,point.y);
MessageBox(str);
}


void MyFrame :: OnCHAR(UINT key,UINT,UINT)
{
CString str;
str.Format(TEXT("按下了%c键"),key);
MessageBox(str);
}

void MyFrame :: OnPaint()
{

CPaintDC dc(this);//this指定绘图设备
dc.TextOutW(100,100,TEXT("为了部落"));//CDC里面找能够画图的图形
dc.Ellipse(10,10,100,100);
}

猜你喜欢

转载自www.cnblogs.com/sunflowers-lanqijiu/p/11785241.html
今日推荐