转载:MFC 获得各类指针、句柄的方法
2019獨角獸企業重金招聘Python工程師標準>>>
<h5> </h5> <p>轉載: <p>最近有些人在問MFC編程一些要點,有一些句柄的獲取、指針的獲取是常見的問題,本文將對這些問題做以解釋,參考了前人的筆錄(見reference),希望能夠幫助大家更方便地進行MFC程序開發。 <p> 一般我們使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架,無論是多文檔還是單文檔,都存在指針和句柄獲取和操作問題。本文中將針對各類句柄的獲得、指針的獲得以及MFC中常見應用進行闡述并舉例。 <p>本文內容索引: <p>========================================================= <p>MFC中獲取常見類句柄<視圖類,文檔類,框架類,應用程序類> <p>MFC中獲取窗口句柄及相關函數 <p>MFC獲取控件句柄 <p>MFC各類中獲取類指針詳解 <p> MSDN關于應用程序信息和管理的各個函數 <p>========================================================== <p>MFC中獲取常見類句柄<視圖類,文檔類,框架類,應用程序類> <p>本節為VC中常用的文檔類,視圖類,框架類,應用程序類,自定義類中獲取其它四個類的方法:<br>GET App <br> AfxGetInstanceHandle() <br> AfxGetApp() <br>GET Frame->View->Document <br> SDI AfxGetMainWnd() -> GetActiveView() -> GetDocument() <br> MDI AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument() <br>GET Menu <br> CMenu *pMenu=AfxGetApp()->m_pMainWnd->GetMenu(); <br>GET ToolBar,StatusBar <br> (CMainFrame *)GetParent()->m_wndToolBar; <br> (CMainFrame *)GetParent()->m_wndStatusBar; <br> CStatusBar * pStatusBa=(CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR); <br> CToolBar * pToolBar=(CtoolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR); <br>Get View from Document <br> GetFirstViewPosition 和 GetNextView 函數得到指針。 <p>MFC中獲取窗口句柄及相關函數 <p>首先,窗口句柄,在窗口類中直接使用成員變量m_hWnd,在窗口外最常見是用AfxGetMainWnd (獲取主窗口指針,其成員變量m_hWnd為主窗口句柄): <p>HWND hWnd = AfxGetMainWnd()->m_hWnd; <p>與其相關的函數說明如下,這些函數對于獲取窗口句柄非常有用: <p>GetTopWindow<br>函數功能:該函數檢查與特定父窗口相聯的子窗口z序(Z序:垂直屏幕的方向,即疊放次序),并返回在z序頂部的子窗口的句柄。<br>函數原型:HWND GetTopWindow(HWND hWnd);<br>參數:<br> hWnd:被查序的父窗口的句柄。如果該參數為NULL,函數返回Z序頂部的窗口句柄。<br>返回值:<br> 如果函數成功,返回值為在Z序頂部的子窗口句柄。如果指定的窗口無子窗口,返回值為NULL。<br>GetForegroundWindow<br>函數功能:該函數返回當前系統的前臺窗口的窗口句柄。<br>函數原型:HWND GetForegroundWindow(VOID) <br>返回值:函數返回前臺窗回的句柄。<br>☆☆☆ GetActiveWindow 獲取當前窗口句柄 <p>函數功能:該函數可以獲得與調用該方法的線程的消息隊列相關的活動窗口的窗口句柄(就是取得當前進程的活動窗口的窗口句柄)。<br>函數原型:HWND GetActiveWindow(VOID)<br>返回值:返回值是與調用線程的消息隊列相關的活動窗口的句柄。否則,返回值為NULL。<br>GetSafeHwnd<br>函數功能:獲取某個窗口對象(CWnd的派生對象)指針的句柄(HWND)時,最安全的方法是使用GetSafeHwnd()函數。<br>通過下面的例子來看其理由:<br> CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器 <p><b>[cpp]</b> <a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">view plain</a><a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">copy</a> <ol> <li>CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器 <li>HWND hwnd = pwnd->m_hwnd; //得到它的HWND</li></ol> <p>HWND hwnd = pwnd->m_hwnd; //得到它的HWND <p> 這樣的代碼當開始得到的pwnd為空的時候就會出現一個“General protection error”,并關閉應用程序,因為一般不能對一個NULL指針訪問其成員,如果用下面的代碼: <p><b>[cpp]</b> <a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">view plain</a><a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">copy</a> <ol> <li>CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到資源管理器 <li>HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND</li></ol> <p>就不會出現問題,因為盡管當pwnd是NULL時,GetSafeHwnd仍然可以用,只是返回NULL<br>IsWindowVisible<br>函數功能:該函數獲得給定窗口的可視狀態。<br>函數原型:BOOL IsWindowVisible(HWND hWnd);<br>參數;<br> hWnd:被測試窗口的句柄。<br>返回值:<br> 如果指定的窗口及其父窗口具有WS_VISIBLE風格,返回值為非零;如果指定的窗口及其父窗口不具有WS_VISIBLE風格,返回值為零。由于返回值表明了窗口是否具有Ws_VISIBLE風格,因此,即使該窗口被其他窗口遮蓋,函數返回值也為非零。<br>備注:<br> 窗口的可視狀態由WS_VISIBLE位指示。當設置了WS_VISIBLE位,窗口就可顯示,而且只要窗口具有WS_VISIBLE風格,任何畫在窗口的信息都將被顯示。<br>IsWindow:<br>函數功能:該函數確定給定的窗口句柄是否標示一個已存在的窗口。 <br>函數原型:BOOL IsWindow(HWND hWnd);<br>參數:<br> hWnd:被測試窗口的句柄。<br>返回值:<br> 如果窗口句柄標識了一個已存在的窗口,返回值為TURE;如果窗口句柄未標識一個已存在窗口,返回值為FALSE。 <p>FindWindow:<br>HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName );<br>參數:<br>lpClassName <p> 指向一個以null結尾的、用來指定類名的字符串或一個可以確定類名字符串的原子。如果這個參數是一個原子,那么它必須是一個在調用此函數前已經通過GlobalAddAtom函數創建好的全局原子。這個原子(一個16bit的值),必須被放置在lpClassName的低位字節中,lpClassName的高位字節置零。 <p>lpWindowName<br> 指向一個以null結尾的、用來指定窗口名(即窗口標題)的字符串。如果此參數為NULL,則匹配所有窗口名。<br>返回值:<br>如果函數執行成功,則返回值是擁有指定窗口類名或窗口名的窗口的句柄。 <p> 如果函數執行失敗,則返回值為 NULL 。可以通過調用GetLastError函數獲得更加詳細的錯誤信息。 <p>來說個應用,窗口標題的改變,我們可以通過SetWindowText來實現: <p>注:如果窗口本身屬性是不顯示標題的,這個函數的調用不會影響窗口屬性。 <p><b>[cpp]</b> <a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">view plain</a><a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">copy</a> <ol> <li>//Set title for application’s main frame window . <li>AfxGetMainWnd ( ) -> SetWindowText (_T("Application title") ) <li>//Set title for View’s MDI child frame window . <li>GetParentFrame ( ) -> SetWindowText ("_T ("MDI Child Frame new title") ) <li>//Set title for dialog’s push button control. <li>GetDigitem (IDC_BUTTON) -> SetWindowText (_T ("Button new title ") ) </li></ol> <p>MFC獲取控件句柄 <p>SDI中的控件句柄獲取: <p><b>[cpp]</b> <a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">view plain</a><a href="http://blog.csdn.net/abcjennifer/article/details/7480019#">copy</a> <ol> <li>CWnd *pWnd = GetDlgItem(ID_***); // 取得控件的指針 <li>HWND hwnd = pWnd->GetSafeHwnd(); // 取得控件的句柄</li></ol><pre></pre><pre>取得CDC的指針是CDC* pdc = pwnd->GetWindowDC();</pre> <p>MFC各類中獲取類指針詳解 <p>使用到的類需要包含響應的頭文件。首先一般獲得本類(視,文檔,對話框都支持)實例指針 this,用this的目的,主要可以通過類中的函數向其他類或者函數中發指針,以便于在非本類中操作和使用本類中的功能。這其中的關鍵在于理解 m_pMainWnd,AfxGetApp(),AfxGetMainWnd()的意義!<br>1)在View中獲得Doc指針<br>CYouSDIDoc *pDoc=GetDocument();一個視只能有一個文檔。<br>2) 在App中獲得MainFrame指針<br>CWinApp 中的 m_pMainWnd變量就是MainFrame的指針,也可以: CMainFrame *pMain =(CMainFrame*)AfxGetMainWnd();<br>3) 在View中獲得MainFrame指針<br>CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;<br>4) 獲得View(已建立)指針<br>CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;<br>CyouView *pView=(CyouView *)pMain->GetActiveView();<br>5) 獲得當前文檔指針<br>CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();<br>6) 獲得狀態欄與工具欄指針<br>CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);<br>CToolBar * pToolBar=(CtoolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);<br>7) 如果框架中加入工具欄和狀態欄變量還可以這樣<br>(CMainFrame *)GetParent()->m_wndToolBar;<br>(CMainFrame *)GetParent()->m_wndStatusBar;<br>8) 在Mainframe獲得菜單指針<br>CMenu *pMenu=m_pMainWnd->GetMenu();<br>9) 在任何類中獲得應用程序類<br>AfxGetInstanceHandle 得到句柄,AfxGetApp得到指針<br>最后提醒大家,在提取到各個句柄之后,因為初次提取的都是標準類句柄,所以,在使用時要注意將標準句柄轉換成自己的類的句柄。<br>如:<br>AfxGetApp();//得到的是WinApp類的句柄,<br>所以操作前記得轉換成自己定義的類的句柄。<br>如:<br>((CMyApp*)AfxGetApp())->XXXX();//這的xxxx()就是你定義的類中間的成員。 <p>MSDN關于應用程序信息和管理的各個函數 <p>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.<br>The Microsoft Foundation Class Library provides the following global functionsto help you accomplish these tasks:<br>Application Information and Management Functions<br>AfxFreeLibrary<br>Decrements the reference count of the loaded dynamic-link library (DLL) module;when the reference count reaches zero, the module is unmapped.<br>AfxGetApp<br>Returns a pointer to the application's single CWinApp object.<br>AfxGetAppName<br>Returns a string containing the application's name.<br>AfxGetInstanceHandle<br>Returns an HINSTANCE representing this instance of the application.<br>AfxGetMainWnd<br>Returns a pointer to the current "main" window of a non-OLEapplication, or the in-place frame window of a server application.<br>AfxGetResourceHandle<br>Returns an HINSTANCE to the source of the application's default resources. Usethis to access the application's resources directly.<br>AfxInitRichEdit<br>Initializes the version 1.0 rich edit control for the application.<br>AfxInitRichEdit2<br>Initializes the version 2.0 and later rich edit control for the application.<br>AfxLoadLibrary<br>Maps a DLL module and returns a handle that can be used to get the address of aDLL function.<br>AfxRegisterWndClass<br>Registers a Windows window class to supplement those registered automaticallyby MFC.<br>AfxSocketInit<br>Called in a CWinApp::InitInstance override to initialize Windows Sockets.<br>AfxSetResourceHandle<br>Sets the HINSTANCE handle where the default resources of the application areloaded.<br>AfxRegisterClass<br>Registers a window class in a DLL that uses MFC.<br>AfxBeginThread<br>Creates a new thread.<br>AfxEndThread<br>Terminates the current thread.<br>AfxGetThread<br>Retrieves a pointer to the current CWinThread object.<br>AfxWinInit<br>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. <p>Reference: <p>1. <a href="http://www.cppblog.com/eday/archive/2006/11/18/15375.html">http://www.cppblog.com/eday/archive/2006/11/18/15375.html</a> <p>2. <a href="http://support.microsoft.com/KB/141003">http://support.microsoft.com/KB/141003</a> <p>3. <a href="http://blog.csdn.net/zhifeiya/article/details/7402667">http://blog.csdn.net/zhifeiya/article/details/7402667</a></p>
轉載于:https://my.oschina.net/EndlessText/blog/172259
總結
以上是生活随笔為你收集整理的转载:MFC 获得各类指针、句柄的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: env: zh_CN.GB2312::
- 下一篇: c 语言实现汇文, 瞎写