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

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

               

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


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

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

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

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

            MFC獲取控件句柄

            MFC各類中獲取類指針詳解

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

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

            MFC中獲取常見(jiàn)類句柄<視圖類,文檔類,框架類,應(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,在窗口外最常見(jiàn)是用AfxGetMainWnd (獲取主窗口指針,其成員變量m_hWnd為主窗口句柄):

            HWND hWnd = AfxGetMainWnd()->m_hWnd;


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

            GetTopWindow
            函數(shù)功能:該函數(shù)檢查與特定父窗口相聯(lián)的子窗口z序(Z序:垂直屏幕的方向,即疊放次序),并返回在z序頂部的子窗口的句柄。
            函數(shù)原型:HWND GetTopWindow(HWND hWnd);
            參數(shù):
              hWnd:被查序的父窗口的句柄。如果該參數(shù)為NULL,函數(shù)返回Z序頂部的窗口句柄。
            返回值:
                如果函數(shù)成功,返回值為在Z序頂部的子窗口句柄。如果指定的窗口無(wú)子窗口,返回值為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è)窗口對(duì)象(CWnd的派生對(duì)象)指針的句柄(HWND)時(shí),最安全的方法是使用GetSafeHwnd()函數(shù)。
            通過(guò)下面的例子來(lái)看其理由:
              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)開(kāi)始得到的pwnd為空的時(shí)候就會(huì)出現(xiàn)一個(gè)“General protection error”,并關(guān)閉應(yīng)用程序,因?yàn)橐话悴荒軐?duì)一個(gè)NULL指針訪問(wèn)其成員,如果用下面的代碼:  
              
            1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器  
            2.   HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND  

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


            IsWindowVisible
            函數(shù)功能:該函數(shù)獲得給定窗口的可視狀態(tài)。
            函數(shù)原型:BOOL IsWindowVisible(HWND hWnd);
            參數(shù);
              hWnd:被測(cè)試窗口的句柄。
            返回值:
                如果指定的窗口及其父窗口具有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)格,任何畫(huà)在窗口的信息都將被顯示。


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

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

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


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

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


            來(lái)說(shuō)個(gè)應(yīng)用,窗口標(biāo)題的改變,我們可以通過(guò)SetWindowText來(lái)實(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中的控件句柄獲?。?/span>

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

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



            MFC各類中獲取類指針詳解

            使用到的類需要包含響應(yīng)的頭文件。首先一般獲得本類(視,文檔,對(duì)話框都支持)實(shí)例指針 this,用this的目的,主要可以通過(guò)類中的函數(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、通過(guò)鼠標(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è)偽窗口指針,不能將它保存起來(lái)供以后使用。

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

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

            CWP_SKIPNIVSIBLE    file://忽略不可見(jiàn)子窗口

            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 王海光 閱讀(1224) 評(píng)論(0)  編輯 收藏 引用 所屬分類: MFC
            99久久免费只有精品国产| 无码超乳爆乳中文字幕久久| 国产精品热久久无码av| 午夜精品久久久内射近拍高清| 欧美日韩精品久久免费| 久久精品中文字幕久久| 久久人妻无码中文字幕| 久久精品国产半推半就| 成人综合久久精品色婷婷| 国产午夜福利精品久久2021| 性高朝久久久久久久久久| 国产精品久久影院| 国内精品伊人久久久久777| 久久99精品久久久久久秒播 | 久久精品国产99久久丝袜| 亚洲精品NV久久久久久久久久| 99久久精品日本一区二区免费 | 97香蕉久久夜色精品国产| 久久精品国产亚洲欧美| 老色鬼久久亚洲AV综合| 久久综合色老色| 精品久久久久国产免费| 99久久99久久精品国产| 精品国际久久久久999波多野| 精品多毛少妇人妻AV免费久久| 日韩va亚洲va欧美va久久| 国内精品久久久久久不卡影院 | 亚洲精品美女久久久久99| 波多野结衣久久一区二区| 欧美性猛交xxxx免费看久久久| 国产成人精品久久亚洲| 国产成人久久久精品二区三区| 国产精品9999久久久久| 久久精品国产亚洲av麻豆小说 | 国产激情久久久久影院| 久久香蕉一级毛片| 欧美精品一区二区精品久久| 青青热久久综合网伊人| 国产精品美女久久久久av爽| 久久精品国产精品亚洲人人| 久久久久国产精品麻豆AR影院|