(轉)MFC中得到各個類的指針/句柄 ID的總結

http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.htmlhtml

通常咱們使用的框架是VC提供的Wizard生成的 MFC App Wizard(exe)框架,不管是多文檔仍是單文檔,都存在 指針獲取和操做問題。
下面這節內容主要是通常的框架,而後再講多線程中的指針使用。使用到的類須要包含響應的頭 文件。首先通常得到本類(視,文檔,對話框都支持)實例指針 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 獲得指針
B1.如何在本身的類和「應用程序類」中得到「文檔類」的句柄?
SDI AfxGetMainWnd() -> GetActiveView() -> GetDocument() 獲得指針
MDI AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument() 獲得指針
B3.如何在「框架類」中得到「文檔類」句柄?
SDI GetActiveView() -> GetDocument() 獲得指針,MDI MDIGetActive() -> GetActiveView() -> GetDocument() 從 CMainFrame 獲得指針,GetActiveView() -> GetDocument() 從 CChildFrame 獲得指針
B4.如何在「視圖類」中得到「文檔類」句柄?
GetDocument()
C1.如何在「文檔類」中得到「視圖類」句柄?
GetView(),調用 GetFirstViewPosition 和 GetNextView 函數獲得指針。
C2.如何在本身的類和「應用程序類」中得到「視圖類」句柄?
SDI GetActiveView 獲得指針
MDI MDIGetActive() -> GetActiveView() 從 CMainFrame 獲得指針,GetActiveView 從 CChildFrame 獲得指針

最後提醒你們,在提取到各個句柄以後,由於初次提取的都是標準類句柄,因此,在使用時要注意將標準句柄轉換成本身的類的句柄。
如:
AfxGetApp();//獲得的是WinApp類的句柄,
因此操做前記得轉換成本身定義的類的句柄。
如:
((CMyApp*)AfxGetApp())->XXXX();//這的xxxx()就是你定義的類中間的成員。

另外,附上 MSDN 關於應用程序信息和管理的各個函數:
When you write an application, you create a single CWinApp-derived object. At times, you may want to get information about this object from outside the CWinApp-derived object.
The Microsoft Foundation Class Library provides the following global functions to 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-OLE application, or the in-place frame window of a server application.

AfxGetResourceHandle
Returns an HINSTANCE to the source of the application's default resources. Use this 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 a DLL function.

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

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

AfxSetResourceHandle
Sets the HINSTANCE handle where the default resources of the application are loaded.

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 CWinApp initialization of a GUI-based application, to initialize MFC. Must be called directly for console applications using MFC.
 
 
 
 
ID--HANDLE--HWND三者之間的互相轉換
id->句柄(由ID獲得句柄)-----------hWnd = ::GetDlgItem(hParentWnd,id); id->指針(由ID獲得指針)-----------CWnd::GetDlgItem(); 句柄->id(由句柄獲得ID)-----------id = GetWindowLong(hWnd,GWL_ID); 句柄->指針(由句柄獲得指針)--------CWnd *pWnd=CWnd::FromHandle(hWnd); 指針->ID(由指針獲得ID)----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID); GetDlgCtrlID(); 指針->句柄(由 指針獲得句柄)--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd; 
相關文章
相關標籤/搜索