第一个MFC程序代码

#include <afxwin.h>

class CMyApp :public CWinApp {
public:
	virtual BOOL InitInstance();
};

class CMyWnd :public CFrameWnd {
public:
	CMyWnd();
protected:
	afx_msg void OnPaint();
	DECLARE_MESSAGE_MAP();
};

BOOL CMyApp::InitInstance() {
	m_pMainWnd = new CMyWnd();//run方法会检测此指针,如果为空会退出。
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

CMyWnd::CMyWnd() {
	Create(NULL, "主窗口");
}
void CMyWnd::OnPaint() {
	CPaintDC dc(this);
	CRect rc;
	GetClientRect(&rc);
	dc.DrawText("hello,MFC! suifeng@2018/8/11", &rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()
CMyApp myApp;//构造函数中 填充了某个全局变量,如果不定义这个对象,会出现空指针异常

猜你喜欢

转载自blog.csdn.net/suifengTYZ/article/details/82532237