您的位置:首页 > 编程语言 > C语言/C++

[面试] C/C++ —— MFC

2016-03-27 10:25 453 查看
MFC 源程序所在路径:

D:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC


看不见的 WinMain

当我们在编译链接的时候,链接器将WinMain函数链接进程序包中的,WinMain 成为我们程序的一部分。

#define _tWinMain WinMain


_tWinMain 所在的文件是 APPMODUL.cpp

真实的启动顺序为:

唯一的全局的 C**App theApp; ⇒ 其构造函数处 ⇒ _tWinMain

CWinApp* AfxGetApp();

C++ 规定,全局对象的构建将比程序入口点(在 DOS 环境为 main,在 Windows 环境为 WinMain)更早。

// MFC.cpp
extern CMyApp theApp;
CWinApp* AfxGetApp()
{
return theApp.m_pCurrentApp;
}

// my.cpp
CMyApp theApp;
int main(int, char**)
{
CWinApp* pApp = AfxGetApp();
...
return 0;
};


MFC 程序的手法:main 函数调用全局函数 AfxGetApp 以取得 theApp 的对象指针。

CWinApp

其源码所在文件为:APPCORE.cpp

MFC 执行流程

其中窗口的处理流程为:

设计窗口类:

已有MFC设计完成,只差一个注册的问题

注册完成之后:

创建窗口

显示窗口

更新窗口

消息循环

完整流程如下:

(1)全局对象

CDIPDemoApp theApp;


(2)全局对象的构造

CDIPDemoApp::CDIPDemoApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}


(3)_tWinMain

extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}


(4)AfxWinMain 完成一部分初始化的工作

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
if (!pThread->InitInstance())


(5)InitInstance

BOOL CDIPDemoApp::InitInstance()
{
...
}


(6)注册窗口

BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)
{
}
BOOL AFXAPI AfxRegisterClass(WNDCLASS* lpWndClass)
{
}


(7)创建窗口

BOOL CFrameWnd::Create(LPCTSTR lpszClassName,
...
{
会去调用 CWnd::CreateEx

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID,
LPVOID lpParam /* = NULL */)
{


(8)窗口创建完毕结束,回到 BOOL CDIPDemoApp::InitInstance(),完成窗口的显示和更新

BOOL CDIPDemoApp::InitInstance()
{
...
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
}


CWinThread(CWnd* m_pMainWnd) ==> CWinApp ==> CMyApp

m_pMainWnd 用于指向CMainFrame框架窗口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: