首先新建一个win32的工程,新建一个***.h的头文件和一个***.cpp文件
一、在头文件里面包含
#include<afswin.h>
class Myapp:public CWinApp //应用程序类
{
public:
virtual BOOL InitInstance(); //程序入口
};
class MyFrame:public CFrameWnd //框架类
{
public:
MyFrame();
};
二、在cpp文件包含
#include"MFC.h"
Myapp app;
BOOL Myapp::InitInstance()
{
MyFrame *frame=new MyFrame;
frame->ShowWindow(SW_SHOWNORMAL);
frame->UpdateWindow();
m_pMainWnd=frame;//指向主窗口的指针 这一句必须要 不然会闪退
return TRUE;
}
MyFrame::MyFrame()
{
Create(0,TEXT("This is My MFC!"));
}
cpp文件
#include"MFC.h"
Myapp app;
BOOL Myapp::InitInstance()
{
MyFrame *frame=new MyFrame;
frame->ShowWindow(SW_SHOWNORMAL);
frame->UpdateWindow();
m_pMainWnd=frame;//指向主窗口的指针
return TRUE;
}
// 实现宏
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
ON_WM_LBUTTONDOWN() //鼠标左键
ON_WM_CHAR()//键盘
ON_WM_PAINT()//绘画
END_MESSAGE_MAP()
MyFrame::MyFrame()
{
Create(0,TEXT("This is My MFC!"));
}
void MyFrame::OnLButtonDown(UINT,CPoint point) //单击鼠标的实现
{
TCHAR buf[1024];
wsprintf(buf,TEXT("你现在所单击的位置为[%d,%d]"),point.x,point.y);
MessageBox(buf);
/* 第二种实现方法
CString str;
str.Format(TEXT("你现在所单击的位置为[%d,%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);
dc.TextOutW(901,454,TEXT("Martin")); //绘字
}
头文件
#include<afxwin.h>
class Myapp:public CWinApp //应用程序类
{
public:
virtual BOOL InitInstance(); //程序入口
};
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();
};