VS2008->新建->vsual c++->常规->空项目
添加个头文件,代码如下
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMyFrame:public CFrameWnd
{
public:
CMyFrame();
protected:
afx_msg void OnLButtonDown(UINT NfLAGS,CPoint point);
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
添加个源文件,代码如下
#include <afxwin.h>
#include "MyApp.h"
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMyFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return true;
}
BEGIN_MESSAGE_MAP(CMyFrame,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()
CMyFrame::CMyFrame()
{
Create(NULL,"MYAPP Application");
}
void CMyFrame::OnLButtonDown(UINT nFlags,CPoint point)
{
TRACE("Entering MyApp - %lx,%d,%d\n",(long)nFlags,point.x,point.y);
}
void CMyFrame::OnPaint()
{
CPaintDC dc(this);
dc.TextOutA(0,0,"Hello,world");
}
项目-》属性-》配置属性-》常规-》项目默认值-》MFC的使用-》在共享 DLL 中使用 MFC 同样在配置属性中-》连接器-》高级-》入口点-》WinMainCRTStartup
然后项目可正常运行 下面挑一部分内容解释一下这个程序
程序运行先构造一个CWinApp派生出来的类的对象(MFC框架决定的) 就是这个对象:CMyApp theApp; MFC内部有一个WinMain函数 这个函数是程序的入口点,函数执行过程中发现了CMyApp 的对象,即执行它的InitInstance函数 此函数负责主窗口的构造和显示工作 我们可以看到在InitInstance程序中创建了一个继承自CFrameWnd的CMyFrame类 这就是代表窗口的类 OnLButtonDown是鼠标左键按下的事件 此处事件做的工作是在调试窗口输出鼠标的坐标 OnPaint是窗口的重绘事件 就解释到这里