• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            面對現實,超越自己
            逆水行舟,不進則退
            posts - 269,comments - 32,trackbacks - 0

            最近有些人在問MFC編程一些要點,有一些句柄的獲取、指針的獲取是常見的問題,本文將對這些問題做以解釋,參考了前人的筆錄(見reference),希望能夠幫助大家更方便地進行MFC程序開發。

               

               一般我們使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架,無論是多文檔還是單文檔,都存在指針和句柄獲取和操作問題。本文中將針對各類句柄的獲得、指針的獲得以及MFC中常見應用進行闡述并舉例。


            本文內容索引:

            =========================================================

            MFC中獲取常見類句柄<視圖類,文檔類,框架類,應用程序類>

            MFC中獲取窗口句柄及相關函數 

            MFC獲取控件句柄

            MFC各類中獲取類指針詳解

             MSDN關于應用程序信息和管理的各個函數

            ==========================================================

            MFC中獲取常見類句柄<視圖類,文檔類,框架類,應用程序類>

            本節為VC中常用的文檔類,視圖類,框架類,應用程序類,自定義類中獲取其它四個類的方法: 
            GET App 
               AfxGetInstanceHandle() 
               AfxGetApp() 
            GET Frame->View->Document 
                SDI   AfxGetMainWnd() -> GetActiveView() -> GetDocument() 
                MDI   AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument() 
            GET Menu 
                 CMenu *pMenu=AfxGetApp()->m_pMainWnd->GetMenu(); 
            GET ToolBar,StatusBar 
                  (CMainFrame *)GetParent()->m_wndToolBar; 
                  (CMainFrame *)GetParent()->m_wndStatusBar; 
                  CStatusBar * pStatusBa=(CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR); 
                  CToolBar * pToolBar=(CtoolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR); 
            Get View from Document 
                  GetFirstViewPosition 和 GetNextView 函數得到指針。 


            MFC中獲取窗口句柄及相關函數 

            首先,窗口句柄,在窗口類中直接使用成員變量m_hWnd,在窗口外最常見是用AfxGetMainWnd (獲取主窗口指針,其成員變量m_hWnd為主窗口句柄):

            HWND hWnd = AfxGetMainWnd()->m_hWnd;


            與其相關的函數說明如下,這些函數對于獲取窗口句柄非常有用:

            GetTopWindow
            函數功能:該函數檢查與特定父窗口相聯的子窗口z序(Z序:垂直屏幕的方向,即疊放次序),并返回在z序頂部的子窗口的句柄。
            函數原型:HWND GetTopWindow(HWND hWnd);
            參數:
              hWnd:被查序的父窗口的句柄。如果該參數為NULL,函數返回Z序頂部的窗口句柄。
            返回值:
                如果函數成功,返回值為在Z序頂部的子窗口句柄。如果指定的窗口無子窗口,返回值為NULL。

            GetForegroundWindow
            函數功能:該函數返回當前系統的前臺窗口的窗口句柄。
            函數原型:HWND GetForegroundWindow(VOID)  
            返回值:函數返回前臺窗回的句柄。

            ☆☆  GetActiveWindow     獲取當前窗口句柄

            函數功能:該函數可以獲得與調用該方法的線程的消息隊列相關的活動窗口的窗口句柄(就是取得當前進程的活動窗口的窗口句柄)。
            函數原型:HWND GetActiveWindow(VOID)
            返回值:返回值是與調用線程的消息隊列相關的活動窗口的句柄。否則,返回值為NULL。


            GetSafeHwnd
            函數功能:獲取某個窗口對象(CWnd的派生對象)指針的句柄(HWND)時,最安全的方法是使用GetSafeHwnd()函數。
            通過下面的例子來看其理由:
              CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器

            1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器  
            2.   HWND hwnd = pwnd->m_hwnd; //得到它的HWND  

                   HWND hwnd = pwnd->m_hwnd; //得到它的HWND

              這樣的代碼當開始得到的pwnd為空的時候就會出現一個“General protection error”,并關閉應用程序,因為一般不能對一個NULL指針訪問其成員,如果用下面的代碼:  
              
            1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器  
            2.   HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND  

              就不會出現問題,因為盡管當pwnd是NULL時,GetSafeHwnd仍然可以用,只是返回NULL


            IsWindowVisible
            函數功能:該函數獲得給定窗口的可視狀態。
            函數原型:BOOL IsWindowVisible(HWND hWnd);
            參數;
              hWnd:被測試窗口的句柄。
            返回值:
                如果指定的窗口及其父窗口具有WS_VISIBLE風格,返回值為非零;如果指定的窗口及其父窗口不具有WS_VISIBLE風格,返回值為零。由于返回值表明了窗口是否具有Ws_VISIBLE風格,因此,即使該窗口被其他窗口遮蓋,函數返回值也為非零。
            備注:
                窗口的可視狀態由WS_VISIBLE位指示。當設置了WS_VISIBLE位,窗口就可顯示,而且只要窗口具有WS_VISIBLE風格,任何畫在窗口的信息都將被顯示。


            IsWindow:
            函數功能:該函數確定給定的窗口句柄是否標示一個已存在的窗口。 
            函數原型:BOOL IsWindow(HWND hWnd);
            參數:
                hWnd:被測試窗口的句柄。
            返回值:
                如果窗口句柄標識了一個已存在的窗口,返回值為TURE;如果窗口句柄未標識一個已存在窗口,返回值為FALSE。

            FindWindow:
            HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName );
            參數:
            lpClassName

              指向一個以null結尾的、用來指定類名的字符串或一個可以確定類名字符串的原子。如果這個參數是一個原子,那么它必須是一個在調用此函數前已經通過GlobalAddAtom函數創建好的全局原子。這個原子(一個16bit的值),必須被放置在lpClassName的低位字節中,lpClassName的高位字節置零。


            lpWindowName
              指向一個以null結尾的、用來指定窗口名(即窗口標題)的字符串。如果此參數為NULL,則匹配所有窗口名。
            返回值:
            如果函數執行成功,則返回值是擁有指定窗口類名或窗口名的窗口的句柄。

              如果函數執行失敗,則返回值為 NULL 。可以通過調用GetLastError函數獲得更加詳細的錯誤信息。


            來說個應用,窗口標題的改變,我們可以通過SetWindowText來實現:

            注:如果窗口本身屬性是不顯示標題的,這個函數的調用不會影響窗口屬性。

            1. //Set title for application’s main frame window .  
            2. AfxGetMainWnd ( ) -> SetWindowText (_T("Application title") )  
            3. //Set title for View’s MDI child frame window .  
            4. GetParentFrame ( ) -> SetWindowText ("_T ("MDI Child Frame new title") )  
            5. //Set title for dialog’s push button control.  
            6. GetDigitem (IDC_BUTTON) -> SetWindowText (_T ("Button new title ") )  


            MFC獲取控件句柄

            SDI中的控件句柄獲取:

            1. CWnd   *pWnd   =   GetDlgItem(ID_***); // 取得控件的指針  
            2. HWND hwnd = pWnd->GetSafeHwnd();  // 取得控件的句柄  

            
            
            取得CDC的指針是CDC* pdc = pwnd->GetWindowDC();



            MFC各類中獲取類指針詳解

            使用到的類需要包含響應的頭文件。首先一般獲得本類(視,文檔,對話框都支持)實例指針 this,用this的目的,主要可以通過類中的函數向其他類或者函數中發指針,以便于在非本類中操作和使用本類中的功能。這其中的關鍵在于理解 m_pMainWnd,AfxGetApp(),AfxGetMainWnd()的意義!

            1)在View中獲得Doc指針
            CYouSDIDoc *pDoc=GetDocument();一個視只能有一個文檔。

            2) 在App中獲得MainFrame指針
            CWinApp 中的 m_pMainWnd變量就是MainFrame的指針,也可以: CMainFrame *pMain =(CMainFrame*)AfxGetMainWnd();

            3) 在View中獲得MainFrame指針
            CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;

            4) 獲得View(已建立)指針
            CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
            CyouView *pView=(CyouView *)pMain->GetActiveView();

            5) 獲得當前文檔指針
            CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();

            6) 獲得狀態欄與工具欄指針
            CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);
            CToolBar * pToolBar=(CtoolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

            7) 如果框架中加入工具欄和狀態欄變量還可以這樣
            (CMainFrame *)GetParent()->m_wndToolBar;
            (CMainFrame *)GetParent()->m_wndStatusBar;

            8) 在Mainframe獲得菜單指針
            CMenu *pMenu=m_pMainWnd->GetMenu();

            9) 在任何類中獲得應用程序類
            AfxGetInstanceHandle 得到句柄,AfxGetApp得到指針

            最后提醒大家,在提取到各個句柄之后,因為初次提取的都是標準類句柄,所以,在使用時要注意將標準句柄轉換成自己的類的句柄。
            如:
            AfxGetApp();//得到的是WinApp類的句柄,
            所以操作前記得轉換成自己定義的類的句柄。
            如:
            ((CMyApp*)AfxGetApp())->XXXX();//這的xxxx()就是你定義的類中間的成員。

            MSDN關于應用程序信息和管理的各個函數
            When you write an application, you create a single CWinApp-derived object. Attimes, you may want to get information about this object from outside theCWinApp-derived object.
            The Microsoft Foundation Class Library provides the following global functionsto help you accomplish these tasks:
            Application Information and Management Functions
            AfxFreeLibrary
            Decrements the reference count of the loaded dynamic-link library (DLL) module;when the reference count reaches zero, the module is unmapped.

            AfxGetApp
            Returns a pointer to the application's single CWinApp object.

            AfxGetAppName
            Returns a string containing the application's name.

            AfxGetInstanceHandle
            Returns an HINSTANCE representing this instance of the application.

            AfxGetMainWnd
            Returns a pointer to the current "main" window of a non-OLEapplication, or the in-place frame window of a server application.

            AfxGetResourceHandle
            Returns an HINSTANCE to the source of the application's default resources. Usethis to access the application's resources directly.

            AfxInitRichEdit
            Initializes the version 1.0 rich edit control for the application.

            AfxInitRichEdit2
            Initializes the version 2.0 and later rich edit control for the application.

            AfxLoadLibrary
            Maps a DLL module and returns a handle that can be used to get the address of aDLL function.

            AfxRegisterWndClass
            Registers a Windows window class to supplement those registered automaticallyby MFC.

            AfxSocketInit
            Called in a CWinApp::InitInstance override to initialize Windows Sockets.

            AfxSetResourceHandle
            Sets the HINSTANCE handle where the default resources of the application areloaded.

            AfxRegisterClass
            Registers a window class in a DLL that uses MFC.

            AfxBeginThread
            Creates a new thread.

            AfxEndThread
            Terminates the current thread.

            AfxGetThread
            Retrieves a pointer to the current CWinThread object.

            AfxWinInit
            Called by the MFC-supplied WinMain function, as part of the CWinAppinitialization of a GUI-based application, to initialize MFC. Must be calleddirectly for console applications using MFC.

            本文轉自:http://blog.csdn.net/tianmo2010/article/details/7843332


            其他鏈接:http://www.cnblogs.com/carekee/articles/1835979.html

            1、獲取應用程序指針

              CMyApp* pApp=(CMyApp*)AfxGetApp();

            2、獲取主框架指針

              CWinApp 中的公有成員變量 m_pMainWnd 就是主框架的指針

              CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);

              或者

              CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

            3、獲取菜單指針

              CMenu* pMenu = AfxGetMainWnd()->GetMenu();

            4、獲取工具欄、狀態欄指針

              主框架中可以直接使用m_wndToolBar、m_wndStatusBar

              其他:

              CToolBar* pToolBar = (CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

              CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

            5、獲取控件指針

              先用 GetDlgItem() 再轉換,如:

              CButton* pButton = (CButton*)GetDlgItem(IDC_MYBUTTON);

            6、獲取文檔、視圖指針

            SDI:

            CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

            CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();

            CYourView* pView = (CYourView*)pMainFrame->GetActiveView();

            MDI:

            CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

            CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();

            CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument();

            CYourView* pView = (CYourView*)pChildFrame->GetActiveView();

            7、文檔、視圖

            從視圖獲取文檔指針:

            CYourDoc* pDoc = GetDocument();

            從文檔獲取視圖指針:

            利用成員函數 GetFirstViewPosition() 和 GetNextView() 遍歷

            virtual POSITION GetFirstViewPosition() const;

            virtual CView* GetNextView(POSITION& rPosition) const;

            SDI:

            CYourView* pView;

            POSITION pos = GetFirstViewPosition();

            pView = GetNextView(pos);

            MDI:

            定義函數

            CView* CYourDoc::GetView(CRuntimeClass* pClass)

            {

                CView* pView;

                POSITION pos=GetFirstViewPosition();

                while(pos!=NULL)

                {

                    pView=GetNextView(pos);

                     if(!pView->IsKindOf(pClass))

                         break;

                }

                if(!pView->IsKindOf(pClass))

                {

                    AfxMessageBox("Connt Locate the View.");

                   return NULL;

                }

                return pView;

            }

            使用如下:

            CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

            8、文檔模版、文檔

            從文檔獲取文檔模版指針:

            CDocTemplate* GetDocTemplate() const;

            從文檔模版獲取文檔指針:

            viaual POSITION GetFirstDocPosition( ) const = 0;

            visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

            9、獲取分割視圖中各個視圖的指針

            主框架中定義:CSplitterWnd m_wndSplitter;

            定義兩個View類:CView1、CView2

            框架類中重載:

            BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)

            {

                VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成兩行一列

                VERIFY(m_splitter.CreateView(0,0,RUNTIME_CLASS(CView1),CSize(100,100),pContext));

                VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext));

                return TRUE;

            }

            獲取分割視圖指針

            CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);

            CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

            10、通過鼠標獲得子窗口指針

            CWnd* ChildWindowFromPoint(POINT point) const;

            CWnd* ChildWindowFromPoint(POINT point,UINT nFlags) const;

            用于確定包含指定點的子窗口

            如果指定點在客戶區之外,函數返回NULL;

            如果指定點在客戶區內,但是不屬于任何一個子窗口,函數返回該CWnd的指針;

            如果有多個子窗口包含指定點,則返回第一個子窗口的指針。

            還要注意的是,該函數返回的是一個偽窗口指針,不能將它保存起來供以后使用。

            對于第二個參數nFlags有幾個含義:

            CWP_ALL             file://不忽略任何子窗口

            CWP_SKIPNIVSIBLE    file://忽略不可見子窗口

            CWP_SKIPDISABLED    file://忽略禁止的子窗口

            CWP_SKIPRANSPARENT file://忽略透明子窗口

            在獲取視類的指針時,需要在需要獲取類的cpp文件前面加入#include "***View.h",這樣編譯時會報錯,解決方法是在視類的cpp文件前面加入#include "***Doc.h"

            posted on 2012-09-03 17:41 王海光 閱讀(1204) 評論(0)  編輯 收藏 引用 所屬分類: MFC
            中文字幕久久波多野结衣av| 91久久精品91久久性色| 天天爽天天狠久久久综合麻豆| 国产精品免费福利久久| 少妇熟女久久综合网色欲| 久久91精品综合国产首页| 88久久精品无码一区二区毛片 | 久久国产精品久久国产精品| 色欲综合久久躁天天躁蜜桃| 亚洲第一极品精品无码久久| 久久久久久九九99精品| 久久精品国产久精国产| 国产精品gz久久久| 亚洲精品无码久久久| 国产亚洲精品久久久久秋霞| 人妻精品久久无码专区精东影业| 无码人妻久久久一区二区三区 | 一本色道久久88精品综合 | 国产高清国内精品福利99久久| 国产精品九九久久免费视频| 久久国产精品偷99| 久久国产色av免费看| 99久久99这里只有免费费精品| 国产精品一区二区久久不卡| 久久99精品久久久久久| 精品久久久久久久久久中文字幕| 亚洲精品乱码久久久久久蜜桃| 性高湖久久久久久久久| 99久久精品免费看国产一区二区三区 | 国产伊人久久| 少妇久久久久久久久久| 精品国产91久久久久久久a| 99久久做夜夜爱天天做精品| 久久精品免费观看| 久久精品国产清自在天天线 | 久久99热这里只有精品国产| 大美女久久久久久j久久| 99久久精品免费看国产一区二区三区 | 久久久久久精品免费免费自慰| 伊人热人久久中文字幕| 久久久久亚洲AV片无码下载蜜桃|