MFC 程序的运行和启动过程和 hello 例程本质上是一致的,依次执行下面几个步骤,这 些步骤在 MFC 程序运行的时候都是自动完成的,编程被屏蔽在外面,这里通过一个简单的 hello 例程看到了这些步骤,从而对整个启动和运行过程有一个透彻的理解。 o 应用程序对象产生(theApp ),配置内存,设定初值。 o 调用 AfxWinMain() 函数,执行 AfxWinInit() 函数,后者又调用 AfxInitThread() 函数,把 消息队列尽量加大到 96 。 o AfxWinMain() 函数执行 InitApplication()函数。 o AfxWinMain() 函数执行 InitInstance()函数,它是 CWinApp 的虚函数,必须重载并修改 它。 o InitInstance()函数创建了一个 CMyFrameWnd 对象。 o CFrameWnd 构造函数调用 Create()函数,产生主窗口,在生成窗口前,Create()函数注 册有关窗口类。 o 回到 InitInstance()函数中继续执行 ShowWindow()函数显示窗口。 o 执行 UpdateWindow() 函数,发出 WM_PAINT 消息。 o 回到 AfxWinMain() 函数,执行 Run() 函数,进入消息循环。 2。3 应用程序类及其主要成员函数 每个应用程序从类 CWinApp 派生出自己的应用程序类,并定义一个全局的对象。该应 用程序类包含了 Windows 下 MFC 应用程序的初始化、运行和结束过程。基于框架建立的应 用程序必须有一个(且只能有一个 )从 CWinApp 派生的类的对象。在创建的 HelloMFC 程序 中,从 CWinApp 中派生出了 CHelloMFCApp 类,定义了一个全局对象 theApp ,CHelloMFCApp 类在 HelloMFC。cpp 文件中定义。CWinApp 类在 MFC 体系中的层次如图 2…15 所示。 图 2…15 CWinApp 类层次表 ·18 · …………………………………………………………Page 28…………………………………………………………… 第 2 章 应用程序基本框架 CWinApp 所包含的数据成员主要用于存放和控制应用程序本身的信息,主要数据成员如 下。 o m_pszAppName :指定了应用程序名称。 o m_hInstance :标识了应用程序的当前实例。 o m_lpCmdLine :指示一个以 NULL 结尾的字符串,指向应用程序的命令行。 o m_nCmdShow :指示最初如何显示窗口。 o m_pszExeName :应用程序的模块名称。 o m_pszHelpFilePath :应用程序的帮助文件路径。 o m_pszProfileName :应用程序对应的。ini 文件名。 在应用程序中经常需要用到一些全局函数来得到应用程序及实例的一些信息,主要的和 应用类相关的全局函数如下: o AfxGetApp :返回指向应用程序的 CWinApp 对象的指针。 o AfxGetInstanceHandle :返回应用程序实例的句柄。 o AfxGetResourceHandle :返回应用程序资源的句柄。。 o AfxGetAppName :返回应用程序的名称。 以上全局函数可以在应用程序代码中的任何位置调用,如调用 AfxGetApp 函数可获得指 向 CWinApp 类实体的指针 。一旦拥有了这个指针,就可以轻松访问 CWinApp 类的任何公有 成员变量。 CWinApp 的主要成员函数控制着应用程序的初始化、运行和中止,这在应用程序设计中 起着关键的作用,下面就对 CWinApp 的主要成员函数加以介绍。 2。3。1 InitInstance() 函数 InitInstance()函数用于初始化实例。Windows 下对同一个应用程序可以运行多个实例。例 如,如果已经在运行资源管理器,用户还可以再一次或多次启动资源管理器,这样,系统中 就有两个或多个资源管理器在运行,这些运行着的资源管理器就是资源管理器应用程序的多 个运行实例。当每次启动某个应用程序的一个实例时,WinMain() 函数都要调用 InitInstance() 函数。 InitInstance()函数主要完成设置注册数据库、载入标准设置(最近打开文件列表等)、注 册文档模板等工作,其中注册文档模板过程中隐含地创建了主窗口。接着,处理命令行参数、 显示窗口,然后返回、进入消息循环。在 CHelloMFCApp 应用程序类中重载了 CWinApp 的 成员函数 InitInstance ,HelloMFC 程序中调用 InitInstance()函数的代码如下: BOOL CHelloMFCApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable; you should remove from the following ·19 · …………………………………………………………Page 29…………………………………………………………… Visual C++ 6。0 程序设计从入门到精通 // the specific initialization routines you do not need。 #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // 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 pany 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(CHelloMFCDoc); RUNTIME_CLASS(CMainFrame); // main SDI frame window RUNTIME_CLASS(CHelloMFCView)); AddDocTemplate(pDocTemplate); // Parse mand line for standard shell mands; DDE; file open CmandLineInfo cmdInfo; ParsemandLine(cmdInfo); // Dispatch mands specified on the mand line if (!ProcessShellmand(cmdInfo)) return FALSE; // The one and only window has been initialized; so show and update it。