《明明白白看MFC之程序框架(二)》
来源:互联网 发布:淘宝周南中学地址 编辑:程序博客网 时间:2024/04/30 05:04
二、 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中的消息循环。 (待续)
- 《明明白白看MFC之程序框架(二)》
- 明明白白看MFC之程序框架(二)
- 明明白白看MFC之程序框架
- 明明白白看MFC之程序框架(一)
- 明明白白看MFC之程序框架
- 明明白白看MFC之程序框架
- 《明明白白看MFC之程序框架(一)》
- 《明明白白看MFC之程序框架(三)》
- 明明白白看MFC之程序框架(三)
- 明明白白看MFC之程序框架(一)
- 《明明白白看MFC之程序框架(三)》
- MFC之程序框架
- MFC之程序框架
- 【MVC框架系列】(一)——Struts,让我把你看得明明白白
- MFC学习笔记之二----------MFC框架程序及消息映射
- MFC学习笔记之二----------MFC框架程序及消息映射
- MFC框架剖析(二)
- Windows编程之MFC向导程序框架
- 对于HOOK函数的一点认识 iBreathe(收藏)
- DataGrid传统分页方式
- 让Real Player从你的电脑走开
- 矩阵类模板
- 《明明白白看MFC之程序框架(一)》
- 《明明白白看MFC之程序框架(二)》
- 《明明白白看MFC之程序框架(三)》
- ADO 数据库连接
- 如何在一个应用程序中打开其他的应用程序
- VB与VC通信初探(一)
- VB与VC通信初探(二)
- 内存分配——realloc()函数的使用
- VB动态调用外部函数的方法
- 一年工作的总结