• <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>
            面對現(xiàn)實(shí),超越自己
            逆水行舟,不進(jìn)則退
            posts - 269,comments - 32,trackbacks - 0

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

               

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


            本文內(nèi)容索引:

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

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

            MFC中獲取窗口句柄及相關(guān)函數(shù) 

            MFC獲取控件句柄

            MFC各類中獲取類指針詳解

             MSDN關(guān)于應(yīng)用程序信息和管理的各個(gè)函數(shù)

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

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

            本節(jié)為VC中常用的文檔類,視圖類,框架類,應(yīng)用程序類,自定義類中獲取其它四個(gè)類的方法: 
            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 函數(shù)得到指針。 


            MFC中獲取窗口句柄及相關(guān)函數(shù) 

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

            HWND hWnd = AfxGetMainWnd()->m_hWnd;


            與其相關(guān)的函數(shù)說明如下,這些函數(shù)對于獲取窗口句柄非常有用:

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

            GetForegroundWindow
            函數(shù)功能:該函數(shù)返回當(dāng)前系統(tǒng)的前臺(tái)窗口的窗口句柄。
            函數(shù)原型:HWND GetForegroundWindow(VOID)  
            返回值:函數(shù)返回前臺(tái)窗回的句柄。

            ☆☆  GetActiveWindow     獲取當(dāng)前窗口句柄

            函數(shù)功能:該函數(shù)可以獲得與調(diào)用該方法的線程的消息隊(duì)列相關(guān)的活動(dòng)窗口的窗口句柄(就是取得當(dāng)前進(jìn)程的活動(dòng)窗口的窗口句柄)。
            函數(shù)原型:HWND GetActiveWindow(VOID)
            返回值:返回值是與調(diào)用線程的消息隊(duì)列相關(guān)的活動(dòng)窗口的句柄。否則,返回值為NULL。


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

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

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

              這樣的代碼當(dāng)開始得到的pwnd為空的時(shí)候就會(huì)出現(xiàn)一個(gè)“General protection error”,并關(guān)閉應(yīng)用程序,因?yàn)橐话悴荒軐σ粋€(gè)NULL指針訪問其成員,如果用下面的代碼:  
              
            1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器  
            2.   HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND  

              就不會(huì)出現(xiàn)問題,因?yàn)楸M管當(dāng)pwnd是NULL時(shí),GetSafeHwnd仍然可以用,只是返回NULL


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


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

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

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


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

              如果函數(shù)執(zhí)行失敗,則返回值為 NULL 。可以通過調(diào)用GetLastError函數(shù)獲得更加詳細(xì)的錯(cuò)誤信息。


            來說個(gè)應(yīng)用,窗口標(biāo)題的改變,我們可以通過SetWindowText來實(shí)現(xiàn):

            注:如果窗口本身屬性是不顯示標(biāo)題的,這個(gè)函數(shù)的調(diào)用不會(huì)影響窗口屬性。

            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各類中獲取類指針詳解

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

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

            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) 獲得當(dāng)前文檔指針
            CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();

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

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

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

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

            最后提醒大家,在提取到各個(gè)句柄之后,因?yàn)槌醮翁崛〉亩际菢?biāo)準(zhǔn)類句柄,所以,在使用時(shí)要注意將標(biāo)準(zhǔn)句柄轉(zhuǎn)換成自己的類的句柄。
            如:
            AfxGetApp();//得到的是WinApp類的句柄,
            所以操作前記得轉(zhuǎn)換成自己定義的類的句柄。
            如:
            ((CMyApp*)AfxGetApp())->XXXX();//這的xxxx()就是你定義的類中間的成員。

            MSDN關(guān)于應(yīng)用程序信息和管理的各個(gè)函數(shù)
            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.

            本文轉(zhuǎn)自:http://blog.csdn.net/tianmo2010/article/details/7843332


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

            1、獲取應(yīng)用程序指針

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

            2、獲取主框架指針

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

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

              或者

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

            3、獲取菜單指針

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

            4、獲取工具欄、狀態(tài)欄指針

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

              其他:

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

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

            5、獲取控件指針

              先用 GetDlgItem() 再轉(zhuǎn)換,如:

              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();

            從文檔獲取視圖指針:

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

            virtual POSITION GetFirstViewPosition() const;

            virtual CView* GetNextView(POSITION& rPosition) const;

            SDI:

            CYourView* pView;

            POSITION pos = GetFirstViewPosition();

            pView = GetNextView(pos);

            MDI:

            定義函數(shù)

            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、獲取分割視圖中各個(gè)視圖的指針

            主框架中定義:CSplitterWnd m_wndSplitter;

            定義兩個(gè)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、通過鼠標(biāo)獲得子窗口指針

            CWnd* ChildWindowFromPoint(POINT point) const;

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

            用于確定包含指定點(diǎn)的子窗口

            如果指定點(diǎn)在客戶區(qū)之外,函數(shù)返回NULL;

            如果指定點(diǎn)在客戶區(qū)內(nèi),但是不屬于任何一個(gè)子窗口,函數(shù)返回該CWnd的指針;

            如果有多個(gè)子窗口包含指定點(diǎn),則返回第一個(gè)子窗口的指針。

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

            對于第二個(gè)參數(shù)nFlags有幾個(gè)含義:

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

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

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

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

            在獲取視類的指針時(shí),需要在需要獲取類的cpp文件前面加入#include "***View.h",這樣編譯時(shí)會(huì)報(bào)錯(cuò),解決方法是在視類的cpp文件前面加入#include "***Doc.h"

            posted on 2012-09-03 17:41 王海光 閱讀(1222) 評論(0)  編輯 收藏 引用 所屬分類: MFC
            亚洲av日韩精品久久久久久a| 99999久久久久久亚洲| 精品久久久久久无码中文字幕| 996久久国产精品线观看| 国产ww久久久久久久久久| 亚洲人成无码网站久久99热国产| 久久久久久伊人高潮影院| 精品午夜久久福利大片| 久久精品亚洲欧美日韩久久| 久久人与动人物a级毛片| 99久久国产综合精品五月天喷水 | 久久久www免费人成精品| 久久精品亚洲中文字幕无码麻豆 | 伊人伊成久久人综合网777| 久久99久久99精品免视看动漫| 久久久久亚洲av成人无码电影| 久久精品日日躁夜夜躁欧美| 精品久久久久久国产三级| 久久久久久亚洲精品成人| 婷婷久久综合九色综合绿巨人| 久久香蕉国产线看观看乱码| 97久久国产综合精品女不卡| 国产精品热久久毛片| 国产精品一久久香蕉国产线看观看 | 99热都是精品久久久久久| 一本色道久久99一综合| 午夜精品久久久久| 日日狠狠久久偷偷色综合免费 | 久久天天躁狠狠躁夜夜2020一| 国产精品99久久不卡| 久久99精品综合国产首页| 色综合久久中文字幕无码| 18禁黄久久久AAA片| 亚洲欧美精品一区久久中文字幕| 久久精品国产亚洲av瑜伽| 久久97久久97精品免视看| 品成人欧美大片久久国产欧美| 日韩精品国产自在久久现线拍| 99久久无码一区人妻| 精品久久久久久无码中文野结衣 | 久久线看观看精品香蕉国产|