《明明白白看MFC之程序框架(二)》
2004-04-05 23:18
316 查看
二、MFC应用程序结构
借助向导,可以快速的生成一个SDI的Windows应用程序。在使用向导生成应用程序后,会发现有好几个文件, 其主要代码如下:
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
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CHelloWorldDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CHelloWorldView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BOOL CWinApp::InitInstance()
{
return TRUE;
}
MFC应用程序之“Hello World”
咋一眼看上去,好像这个程序无从下手分析,甚至连程序的入口点都找不到。其实,上面的程序还是经过整理后才有如此模样,刚刚入门的我就花了不少功夫才整理出上面的运行脉络,已经较清晰了^_^
在程序的开始处,首先定义了一个全局变量theApp,他代表了整个程序的存在,然后程序开始进入入口点。但是,这里的入口点不再是C的main()或者是SDK中的WinMain()了,起而代之的是AfxWinMain(),如果不去深究 为什么,那么就当成是第一次到C语言中的main()函数一样,只要知道程序从这里开始执行就可以了!实际上,在AfxWinMain()中为了驱动程序的执行,调用了好几个函数,执行路径为:入口点----〉AfxGetThread()------〉AfxGetApp()-------àAfxWinInit()-------àpApp->InitApplication()-----àpThread->InitInstance()------àpThread->Run()。可以看到,程序一样有一个执行的线索可循,但是,相对于SDK来说,如今已经面目全非了,过去的那种清晰的程序结构在这些程序中也有吗?答案是肯定的,只不过他们的具体实现在MFC中都进行了包装而已。
程序由AfxWinMain()开始运行后,首先调用了AfxGetApp()来获取应用程序的对象指针pApp,然后通过这个指针调用有关的成员函数来完成初始化和启动工作,最后就调用了Run()函数,在这里,Run()函数就是代表了SDK中的消息循环。 (待续)
借助向导,可以快速的生成一个SDI的Windows应用程序。在使用向导生成应用程序后,会发现有好几个文件, 其主要代码如下:
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
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CHelloWorldDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CHelloWorldView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BOOL CWinApp::InitInstance()
{
return TRUE;
}
MFC应用程序之“Hello World”
咋一眼看上去,好像这个程序无从下手分析,甚至连程序的入口点都找不到。其实,上面的程序还是经过整理后才有如此模样,刚刚入门的我就花了不少功夫才整理出上面的运行脉络,已经较清晰了^_^
在程序的开始处,首先定义了一个全局变量theApp,他代表了整个程序的存在,然后程序开始进入入口点。但是,这里的入口点不再是C的main()或者是SDK中的WinMain()了,起而代之的是AfxWinMain(),如果不去深究 为什么,那么就当成是第一次到C语言中的main()函数一样,只要知道程序从这里开始执行就可以了!实际上,在AfxWinMain()中为了驱动程序的执行,调用了好几个函数,执行路径为:入口点----〉AfxGetThread()------〉AfxGetApp()-------àAfxWinInit()-------àpApp->InitApplication()-----àpThread->InitInstance()------àpThread->Run()。可以看到,程序一样有一个执行的线索可循,但是,相对于SDK来说,如今已经面目全非了,过去的那种清晰的程序结构在这些程序中也有吗?答案是肯定的,只不过他们的具体实现在MFC中都进行了包装而已。
程序由AfxWinMain()开始运行后,首先调用了AfxGetApp()来获取应用程序的对象指针pApp,然后通过这个指针调用有关的成员函数来完成初始化和启动工作,最后就调用了Run()函数,在这里,Run()函数就是代表了SDK中的消息循环。 (待续)
相关文章推荐
- 《明明白白看MFC之程序框架(三)》
- 明明白白看MFC之程序框架
- 明明白白看MFC之程序框架
- 明明白白看MFC之程序框架
- 《明明白白看MFC之程序框架(三)》
- 明明白白看MFC之程序框架(三)
- 明明白白看MFC之程序框架(二)
- 明明白白看MFC之程序框架(一)
- 明明白白看MFC之程序框架(一)
- 《明明白白看MFC之程序框架(一)》
- 孙鑫vc++ 第九课 笔记 修改MFC AppWizard向导生成的框架程序的外观和大小
- MFC框架程序剖析
- MFC框架程序剖析笔记(上篇)
- MFC多文档视图界面(MDI)搭建图像处理框架程序总结
- lesson3:MFC 框架程序初解
- MFC单文档程序中搭建OpenGL框架 .
- MFC--非模式对话框程序(mfc框架)
- MFC框架程序----实现十一
- 一个最简单的MFC程序框架剖析
- MFC--单文档程序(框架)