第一个MFC程序及它的运行流程
步骤
1.创建空项目并配置属性
属性配置如下,在项目属性中改为以下两项。
2.书写代码
MyApp.h头文件
#pragma once
#define _WIN32_WINNT 0x0502 //不写这个会有警告
#include <afxwin.h>
class MyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};
class MyMainWindow :public CFrameWnd
{
public:
MyMainWindow(); //主窗口
};
MyApp代码
#include "MyApp.h"
BOOL MyApp::InitInstance()
{
this->m_pMainWnd = new MyMainWindow();
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
this->m_pMainWnd->UpdateWindow();
return true;
}
MyMainWindow::MyMainWindow()
{
Create(NULL, TEXT("第一个MFC程序"));
}
helloMFC.cpp
#include "MyApp.h"
MyApp app;
3.使用的继承
最重要的两个MFC类:
CwinApp
CFrameWnd
MyApp继承的CwinApp类,MyMainWindow继承CFrameWnd。
4.MFC程序运行步骤
1.CwinApp中的全局变量定义
2.调用CwinApp构造函数
3.进入Winmain函数(_tWinMain为宏,值为WinMain)
4.(1) 注册窗口类并创建窗口
AfxEndDeferRegisterClass()(相当于SDK里面的RegisterClass()函数)
CFrameWnd::Create()