MFC文档视图结构框架分析

2019-03-09 22:08

MFC文档视图结构框架分析 1:程序的“导火索”---theApp

CmyApp theApp;

在声明对象的同时,调用其构造函数。按C++的语法,首先要调用其基类Cwinapp的构造函数. 这个文件主要用于应用程序的一些初始化操作。

class CWinApp : public CWinThread {

DECLARE_DYNAMIC(CWinApp) public: // Constructor

CWinApp(LPCTSTR lpszAppName = NULL); ???? }

CWinApp::CWinApp(LPCTSTR lpszAppName) {

if (lpszAppName != NULL)

m_pszAppName = _tcsdup(lpszAppName); else

m_pszAppName = NULL; // initialize CWinThread state

AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE(); AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread; ASSERT(AfxGetThread() == NULL);

pThreadState->m_pCurrentWinThread = this; ASSERT(AfxGetThread() == this); m_hThread = ::GetCurrentThread(); m_nThreadID = ::GetCurrentThreadId();

// initialize CWinApp state

ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please pModuleState->m_pCurrentWinApp = this; ASSERT(AfxGetApp() == this);

// in non-running state until WinMain m_hInstance = NULL; m_hLangResourceDLL = NULL; m_pszHelpFilePath = NULL; m_pszProfileName = NULL; m_pszRegistryKey = NULL; m_pszExeName = NULL; m_pRecentFileList = NULL; m_pDocManager = NULL;

m_atomApp = m_atomSystemTopic = NULL; m_lpCmdLine = NULL; m_pCmdInfo = NULL;

// initialize wait cursor state m_nWaitCursorCount = 0; m_hcurWaitCursorRestore = NULL; // initialize current printer state m_hDevMode = NULL; m_hDevNames = NULL;

m_nNumPreviewPages = 0; // not specified (defaults to 1) // initialize DAO state

m_lpfnDaoTerm = NULL; // will be set if AfxDaoInit called // other initialization m_bHelpMode = FALSE; m_eHelpType = afxWinHelp;

m_nSafetyPoolSize = 512; // default size }

2:theApp之后的隐藏代码,由他控制整个程序的流程。

_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {

// call shared/exported WinMain

return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); }

其中有宏定义:#define _tWinMain wWinMain

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {

ASSERT(hPrevInstance == NULL); int nReturnCode = -1;

CWinThread* pThread = AfxGetThread();;// CWinApp是从CWinThread派生的, CWinApp* pApp = AfxGetApp(); //实质上就是pThread==pApp // 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()) //注意多态性 virtual BOOL InitInstance();

//又因为pThread==pApp,所以调用pApp-> InitInstance()

{

if (pThread->m_pMainWnd != NULL) {

TRACE(traceAppMsg, 0, \n\

pThread->m_pMainWnd->DestroyWindow(); }

nReturnCode = pThread->ExitInstance(); goto InitFailure; }

nReturnCode = pThread->Run(); //控制消息循环 InitFailure: #ifdef _DEBUG

// Check for missing AfxLockTempMap calls

if (AfxGetModuleThreadState()->m_nTempMapLock != 0) {

TRACE(traceAppMsg, 0, \\\n\

AfxGetModuleThreadState()->m_nTempMapLock); }

AfxLockTempMaps(); AfxUnlockTempMaps(-1); #endif

AfxWinTerm(); return nReturnCode; }

由上面的程序可以看到几个很重要的函数

(1)AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)) goto InitFailure; (2) pApp->InitApplication())

(3) pThread->InitInstance() (4) pThread->Run()

其中1,2 也是完成程序的一些初始化工作,4 主要是为了处理消息,3呢,很关键,我们运行时看到的窗口就是从这里产生。下面一一介绍

3:程序自动产生的InitInstance()函数

以下是自动生成的InitInstance()源程序: BOOL CmyApp::InitInstance() {

// 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControls()。否则,将无法创建窗口。 InitCommonControls(); CWinApp::InitInstance(); // 初始化 OLE 库 if (!AfxOleInit()) {

AfxMessageBox(IDP_OLE_INIT_FAILED); return FALSE; }

AfxEnableControlContainer(); // 标准初始化

// 如果未使用这些功能并希望减小 // 最终可执行文件的大小,则应移除下列 // 不需要的特定初始化例程 // 更改用于存储设置的注册表项 // TODO: 应适当修改该字符串, // 例如修改为公司或组织名

SetRegistryKey(_T(\应用程序向导生成的本地应用程序\


MFC文档视图结构框架分析.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017会计硕士考研备考研习:英语词汇强化练习题(四)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: