MFC CWinApp Class成员变量及成员函数

一,概要说明

           CWinApp派生于CWinThread类。在头文件afxwin.h中定义,在用户定义的类CUserApp theApp;初始化时优先于Main函数执行CWinApp构造函数。MFC库提供下列全局函数来访问CWinApp对象和其它全局信息:

  1. AfxGetApp 获得CWinApp对象指针。
  2. AfxGetInstanceHandle 获得当前应用程序实例的句柄。
  3. AfxGetResourceHandle 获得应用程序资源句柄。
  4. AfxGetAppName 获得一个字符串指针,该字符串包含应用程序名称。也可以用CWinApp->m_pszExeName。

二,类图中的重要位置

            MFC CWinApp Class成员变量及成员函数_第1张图片


三,CWinApp成员函数及成员变量说明

    私有成员及受保护的成员对外不可见,不以说明。

class CWinApp : public CWinThread
{
    DECLARE_DYNAMIC(CWinApp)
public:

// Constructor
    /* explicit */ CWinApp(LPCTSTR lpszAppName = NULL);     // app name defaults to EXE name

// Attributes
    // Startup args (do not change)

    // This module's hInstance.
    HINSTANCE m_hInstance;
    进程句柄,用于加载程序内资源等功能。
    与Windows传送给WinMain的参数hInstance一致。
    m_hInstance数据成员是Windows应用程序当前实例的句柄
    它是全局函数AfxGetInstanceHandle的返回值
    
    
    // Pointer to the command-line.
    LPTSTR m_lpCmdLine;
    
    ///与Windows传送给WinMain的参数lpCmdLine一致。
    ///用m_lpCmdLine来访问当应用程序开始时用户输入的任何命令行参数。
    ///m_lpCmdLine是LPSTR型的公共变量。
    一般在CMyApp::InitInstance中处理命令
    /*
    BOOL CMyApp::InitInstance()
    {
       if (m_lpCmdLine[0] == '/0')
            ...........
    }
    */
    

    // Initial state of the application's window; normally,
    // this is an argument to ShowWindow().
    int m_nCmdShow;

    // Running args (can be changed in InitInstance)

    // Human-redable name of the application. Normally set in
    // constructor or retreived from AFX_IDS_APP_TITLE.
    LPCTSTR m_pszAppName;
    ///(a、ExeName中获取,b、AFX_IDS_APP_TITLE中获取,c、CWinApp构造函数)


    free((void*)m_pszAppName);

    m_pszAppName=_tcsdup(_T("d://somedir//myapp.exe"));

    /// 
    /// Application User Model ID.
    LPCTSTR m_pszAppID;

    // Name of registry key for this application. See
    // SetRegistryKey() member function.
    LPCTSTR m_pszRegistryKey;
    用来检测储存应用程序描述设置的全部注册键。通常它是只读态的。

    // Pointer to CDocManager used to manage document templates
    // for this application instance.
    CDocManager* m_pDocManager;

    // Support for Shift+F1 help mode.

    // TRUE if we're in SHIFT+F1 mode.
    BOOL m_bHelpMode;

public:
    // set in constructor to override default

    // Executable name (no spaces).
    LPCTSTR m_pszExeName;
    ///可用m_pszExeName来得到应用程序名称

    // Default based on this module's path.
    LPCTSTR m_pszHelpFilePath;
    ///指针,指向包含应用程序的帮助文件的路径的字符串。

    // Default based on this application's name.
    LPCTSTR m_pszProfileName;
    ///const char*型的公共变量,应用程序的.INI文件名。

    // Sets and initializes usage of HtmlHelp instead of WinHelp.
    void EnableHtmlHelp();

    // Sets and initializes usage of HtmlHelp instead of WinHelp.
    void SetHelpMode( AFX_HELP_TYPE eHelpType );
    AFX_HELP_TYPE GetHelpMode();

    // help mode used by the app
    AFX_HELP_TYPE m_eHelpType;

public:
    // Loads a cursor resource.
    HCURSOR LoadCursor(LPCTSTR lpszResourceName) const;
    HCURSOR LoadCursor(UINT nIDResource) const;
    ///指向包含光标资源名称的以null结尾的字符串指针

    // Loads a stock cursor resource; for for IDC_* values.
    HCURSOR LoadStandardCursor(LPCTSTR lpszCursorName) const;

    // Loads an OEM cursor; for all OCR_* values.
    HCURSOR LoadOEMCursor(UINT nIDCursor) const;

    // Loads an icon resource.
    HICON LoadIcon(LPCTSTR lpszResourceName) const;
    HICON LoadIcon(UINT nIDResource) const;

    // Loads an icon resource; for stock IDI_ values.
    HICON LoadStandardIcon(LPCTSTR lpszIconName) const;

    // Loads an OEM icon resource; for all OIC_* values.
    HICON LoadOEMIcon(UINT nIDIcon) const;

    // Retrieve an integer value from INI file or registry.
    virtual UINT GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
    ///从应用程序的.INI文件获取一个条目。
    
    
    // Sets an integer value to INI file or registry.
    virtual BOOL WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);

    // Retrieve a string value from INI file or registry.
    virtual CString GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL);

    // Sets a string value to INI file or registry.
    virtual BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue);

    // Retrieve an arbitrary binary value from INI file or registry.
    virtual BOOL GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE* ppData, UINT* pBytes);

    // Sets an arbitrary binary value to INI file or registry.
    virtual BOOL WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes);

    // Override in derived class.
    virtual void InitLibId();

    // Register
    virtual BOOL Register();

    // Unregisters everything this app was known to register.
    virtual BOOL Unregister();

    /// 
    /// Deletes the subkeys and values of the specified key recursively.
    ///  
    /// If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a nonzero error code defined in Winerror.h
    /// A handle to an open registry key.
    /// The name of the key to be deleted.
    /// Pointer to CAtlTransactionManager object
    LONG DelRegTree(HKEY hParentKey, const CString& strKeyName, CAtlTransactionManager* pTM = NULL);

// Running Operations - to be done on a running application
    // Dealing with document templates
    void AddDocTemplate(CDocTemplate* pTemplate);
    POSITION GetFirstDocTemplatePosition() const;
    CDocTemplate* GetNextDocTemplate(POSITION& pos) const;

    // Open named file, trying to match a regsitered
    // document template to it.
    virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName);
    virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU);

    // Add a string to the recent file list. Remove oldest string,
    // if no space left.
    virtual void AddToRecentFileList(LPCTSTR lpszPathName);

    // Printer DC Setup routine, 'struct tagPD' is a PRINTDLG structure.
    void SelectPrinter(HANDLE hDevNames, HANDLE hDevMode,
        BOOL bFreeOld = TRUE);

    // Create a DC for the system default printer.
    BOOL CreatePrinterDC(CDC& dc);


    BOOL GetPrinterDeviceDefaults(PRINTDLG* pPrintDlg);

    // Run this app as an embedded object.
    BOOL RunEmbedded();

    // Run this app as an OLE automation server.
    BOOL RunAutomated();

    // Parse the command line for stock options and commands.
    void ParseCommandLine(CCommandLineInfo& rCmdInfo);

    // React to a shell-issued command line directive.
    BOOL ProcessShellCommand(CCommandLineInfo& rCmdInfo);

// Overridables

    // Hooks for your initialization code
    virtual BOOL InitApplication();

    // exiting
    virtual BOOL SaveAllModified(); // save before exit
    void HideApplication();
    ///关闭所有文档前隐藏应用程序。
    
    void CloseAllDocuments(BOOL bEndSession); // close documents before exiting
    ///关闭所有打开的文档。

    // Advanced: to override message boxes and other hooks
    virtual int DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt);
    为应用程序实现AfxMessageBox。
    
    virtual void DoWaitCursor(int nCode); // 0 => restore, 1=> begin, -1=> end
    ///等待”光标的开关。
    
    
    // Advanced: process async DDE request
    virtual BOOL OnDDECommand(_In_z_ LPTSTR lpszCommand);

    // Advanced: Help support
    virtual void WinHelp(DWORD_PTR dwData, UINT nCmd = HELP_CONTEXT);
    virtual void HtmlHelp(DWORD_PTR dwData, UINT nCmd = 0x000F);
    virtual void WinHelpInternal(DWORD_PTR dwData, UINT nCmd = HELP_CONTEXT);

    // Restart Manager support
    virtual HRESULT RegisterWithRestartManager(BOOL bRegisterRecoveryCallback, const CString &strRestartIdentifier);
    virtual HRESULT RegisterWithRestartManager(LPCWSTR pwzCommandLineArgs, DWORD dwRestartFlags, APPLICATION_RECOVERY_CALLBACK pRecoveryCallback, LPVOID lpvParam, DWORD dwPingInterval, DWORD dwCallbackFlags);
    virtual DWORD ApplicationRecoveryCallback(LPVOID lpvParam);

    virtual BOOL SupportsRestartManager() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_SUPPORT_RESTART; }
    virtual BOOL SupportsApplicationRecovery() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_SUPPORT_RECOVERY; }
    virtual BOOL SupportsAutosaveAtRestart() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_AUTOSAVE_AT_RESTART; }
    virtual BOOL SupportsAutosaveAtInterval() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_AUTOSAVE_AT_INTERVAL; }
    virtual BOOL ReopenPreviousFilesAtRestart() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_REOPEN_PREVIOUS_FILES; }
    virtual BOOL RestoreAutosavedFilesAtRestart() const { return m_dwRestartManagerSupportFlags & AFX_RESTART_MANAGER_RESTORE_AUTOSAVED_FILES; }

    virtual DWORD GetApplicationRestartFlags() { return 0; }  // Flags for RegisterApplicationRestart: default is none of RESTART_NO_CRASH/RESTART_NO_HANG/RESTART_NO_PATCH/RESTART_NO_REBOOT
    virtual LPVOID GetApplicationRecoveryParameter() { return NULL; }  // Parameter to be passed along to RegisterApplicationRecoveryCallback


public: // public for implementation access
    CCommandLineInfo* m_pCmdInfo;

    ATOM m_atomApp, m_atomSystemTopic;   // for DDE open
    UINT m_nNumPreviewPages;        // number of default printed pages

    size_t  m_nSafetyPoolSize;      // ideal size

    void (AFXAPI* m_lpfnDaoTerm)();

    void DevModeChange(_In_z_ LPTSTR lpDeviceName);
    void SetCurrentHandles();

    // Finds number of opened CDocument items owned by templates
    // registered with the doc manager.
    int GetOpenDocumentCount();

    virtual CDataRecoveryHandler *GetDataRecoveryHandler();
    ///  
    /// Tells whether Windows 7 Taskbar interaction is enabled.
    ///  
    /// Returns TRUE if EnableTaskbarInteraction has been called and Operation System is 
    /// Windows 7 or higher.
    ///  Taskbar interaction means that MDI application displays the content of MDI children 
    /// in separate tabbed thumbnails that appear when mouse pointer is over application taskbar button.
    virtual BOOL IsTaskbarInteractionEnabled();

    /// 
    /// Enables Taskbar interaction
    ///  
    /// Returns TRUE if taskbar interaction can be enabled or disabled (e.g. this method was called before creation of main window).
    ///  
    /// This method must be called before creation of main window, otherwise it asserts and returns FALSE.
    ///  Specifies whether interaction with Windows 7 taskbar should be enabled (TRUE), or disabled (FALSE).
    BOOL EnableTaskbarInteraction(BOOL bEnable = TRUE);

    // helpers for standard commdlg dialogs
    BOOL DoPromptFileName(CString& fileName, UINT nIDSTitle,
            DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate);
    INT_PTR DoPrintDialog(CPrintDialog* pPD);

    void EnableModeless(BOOL bEnable); // to disable OLE in-place dialogs

    // overrides for implementation
    virtual BOOL InitInstance();
    ///当你从CWinApp 派生一个应用程序类时
    ///重载InitInstance成员函数来创建应用程序的主窗口对象。
    
    virtual int ExitInstance(); // return app exit code
    virtual BOOL RestartInstance(); // handle restart by Restart Manager
    virtual int Run();
    virtual BOOL OnIdle(LONG lCount); // return TRUE if more idle processing
    virtual LRESULT ProcessWndProcException(CException* e, const MSG* pMsg);
    virtual HINSTANCE LoadAppLangResourceDLL();

    // Helper for message boxes; can work when no CWinApp can be found
    static int ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt);
    static void DoEnableModeless(BOOL bEnable); // to disable OLE in-place dialogs

public:
    virtual ~CWinApp();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

    // helpers for registration

    /// 
    /// returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection.
    ///  
    /// Section key if the function succeeds, NULL - otherwise
    /// The name of the key to be obtained.
    /// Pointer to CAtlTransactionManager object
    HKEY GetSectionKey(LPCTSTR lpszSection, CAtlTransactionManager* pTM = NULL);

    /// 
    /// returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName.
    ///  
    /// Applixation key if the function succeeds, NULL - otherwise
    /// Pointer to CAtlTransactionManager object
    HKEY GetAppRegistryKey(CAtlTransactionManager* pTM = NULL);

protected:
    DECLARE_MESSAGE_MAP()
public :
    // System Policy Settings
    virtual BOOL LoadSysPolicies(); // Override to load policies other than the system policies that MFC loads.
    BOOL GetSysPolicyValue(DWORD dwPolicyID, BOOL *pbValue); // returns the policy's setting in the out parameter
};



你可能感兴趣的:(C/C++/MFC)