上一篇 | 下一篇

明明白白看MFC之程序框架(二)

发布: 2008-6-26 14:22 | 作者: admin | 来源: | 查看: 32次

二.MFC应用程序结构

  在《明明白白看MFC之程序框架(一)》中我分析了一个经典的应用程序的结构,现在可是要进入主题"MFC应用程序结构"了.应用程序有好多种,为了能够更清楚地与前面的文章形成对比,我们在这里看一个SDI的应用程序,当然例子还是经典的Hello World了。在使用向导生成应用程序后,会发现有好几个文件,首先我们不管有哪些文件,按照程序执行得主线抽取主要的源程序分析一下再说(因为 MFC 生成的应用程序不是很方便阅读,所以在这里我将他们重新编辑了)。

CHelloWorldApp theApp;

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPTSTR lpCmdLine, int nCmdShow)

{

CWinThread* pThread = AfxGetThread();

CWinApp* pApp = AfxGetApp();

// AFX internal initialization

if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))

goto InitFailure;

// App global initializations (rare)

if (pApp != NULL && !pApp->InitApplication())

goto InitFailure;

// Perform specific initializations

if (!pThread->InitInstance())

{

if (pThread->m_pMainWnd != NULL)

{

TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");

pThread->m_pMainWnd->DestroyWindow();

}

nReturnCode = pThread->ExitInstance();

goto InitFailure;

}

nReturnCode = pThread->Run();

InitFailure:

……………………

AfxWinTerm();

return nReturnCode;

}

BOOL CWinApp::InitApplication()

{

if (CDocManager::pStaticDocManager != NULL)

{

if (m_pDocManager == NULL)

m_pDocManager = CDocManager::pStaticDocManager;

CDocManager::pStaticDocManager = NULL;

}

if (m_pDocManager != NULL)

m_pDocManager->AddDocTemplate(NULL);

else

CDocManager::bStaticInit = FALSE;

return TRUE;

}

BOOL CHelloWorldApp::InitInstance()

{

AfxEnableControlContainer();

………………………………

// Change the registry key under which our settings are stored.

// TODO: You should modify this string to be something appropriate

字号: | 推荐给好友

31/3123>

评分:0

我来说两句

网络推荐