MFC通過對話框窗口句柄獲得對話框對象指針


MFC在很多的對話框操作中,我們經常要用到在一個對話框中調用另一個對話框的函數或變量.可以用如下方法來解決.
    HWND hWnd=::FindWindow(NULL,_T("Sphere"));      //得到對話框的句柄
   C***Dialog* pWnd= (C***Dialog*)FromHandle(hWnd); //由句柄得到對話框的對象指針
   pWnd->xxx( );                                             //調用C***Dialog中的函數xxx();

通過窗口類函數:CWnd *GetWindow獲得窗口指針,pWnd->m_hWnd(The handle of the Windows window attached to this CWnd)在::FromHandle(hWnd);

獲取主窗口句柄:       CWnd *wnd  = AfxGetMainWnd();
                     HWND hwnd = wnd->GetSafeHwnd();

設置控件爲窗口焦點:GetDlgItem(IDC_TREE1)->SetFocus();

獲取控件句柄:HWND hwndctrl = ::GetDlgItem(mainHwnd, IDC_TREE1);//獲取樹形控件的句柄IDC_TREE IDC_COMBO1

獲取當前最上層窗口的句柄: HWND  mainHwnd = ::GetForegroundWindow();//獲取當前topmost的窗口句柄
獲取當前窗口的焦點句柄:        HWND  currentFocus = ::GetFocus();

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

  

 VC中編程,最大的障礙和問題就是消息機制和指針獲取與操作。其實這些內容基本上是每本VC學習工具書上
必講的內容,而且通過MSDN很多問題都能解決。
面文字主要是個人在編程中指針使用的一些體會,說的不當的地方請指正。
一般我們使用的框架是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.

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