一个Demo初识MFC

如何通过完全代码的形式来建立窗口?下面是一个简单的程序。

新建Win32控制台空项目

设置项目属性如下:



添加文件 编写代码

头文件 TestAll.h

class CMyApp:public CWinApp
{
	public:
		virtual BOOL InitInstance();//虚函数
};

class CMainWindow:public CFrameWnd
{
	public:
		CMainWindow();
	protected:
		afx_msg void OnPaint();
		DECLARE_MESSAGE_MAP();//声明消息映射
};

源文件 TestAll.cpp

// TestAll.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <afxwin.h>
#include "TestAll.h"

CMyApp myApp;

BOOL CMyApp::InitInstance()
{
	 m_pMainWnd = new CMainWindow;

	 m_pMainWnd->ShowWindow(m_nCmdShow);
	 m_pMainWnd->UpdateWindow();
	 return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()       //消息映射

CMainWindow::CMainWindow() //构造函数初始化
{
    Create(NULL,_T("我的第一个MFC应用程序"));//创建窗体
}

void CMainWindow::OnPaint()
{  
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);
	dc.DrawText(_T("Hello MFC"),-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}

运行结果



代码解析见下一篇——《从Windows API 到 MFC浅谈》


猜你喜欢

转载自blog.csdn.net/zhuxipan1990/article/details/51346941