MFC中獲得各個類的句柄的總結

 

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章