為了讓自己查找方便,所以撰寫了這篇文章,本文融會了許多前人所整理的資料,加上自己的一些想法,如有錯誤,請 Mail 或 留言通知。
MFC取得常用的handle<view , document , fram , app >
Get APP
AfxGetInstanceHandle () |
AfxGetApp () |
Get Frame->View->Document
SDI AfxGetMainWnd()->GetActiveView()->GetDocument() |
MDI AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument() |
Get Menu
CMenu * pMenu = AfxGetApp()->m_pMainWnd->GetMunu () ; |
Get Tool Bar , Status Bar
(CMainFram *) GetParent()->m_wndToolBar ; |
(CMainFram *) GetParent()->m_wndStatusBar ; |
CStatusBar * pStatusBa = (CStatusBar* )AfxGetMainWnd()->GetDescendantWindow ( AFX_IDW_STATUS_BAR ); |
CToolBar * pToolBar = ( CToolBar * ) AfxGetMainWnd()->GetDescendantWindow (AFX_IDW_TOOLBAR ) ; |
Get View from Document
Function ( GetFirstViewPosition ) & ( GetNextView ) can get pointer ; |
-----------------------------------------------------------------------------------------------------------------------------
MFC中取得Windows Handle的方法
GetTopWindow
Syntax
HWND WINAPI GetTopWindow( _In_opt_ HWND hWnd );
Parameters
- hWnd [in, optional]
-
Type: HWND
A handle to the parent window whose child windows are to be examined. If this parameter is NULL, the function returns a handle to the window at the top of the Z order.
Return value
Type:
Type: HWND
If the function succeeds, the return value is a handle to the child window at the top of the Z order. If the specified window has no child windows, the return value is NULL. To get extended error information, use the GetLastError function.
Source : https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms633514(v=vs.85).aspx
GetActiveWindow
功能: 簡單來說就是取得當前Window 的handle ! ( 這個很好用 )
Syntax
HWND WINAPI GetActiveWindow(void);
Parameters
This function has no parameters.
Return value
Type: HWND
The return value is the handle to the active window attached to the calling thread's message queue. Otherwise, the return value is NULL.
Remarks
To get the handle to the foreground window, you can use GetForegroundWindow.
To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.
GetForegroundWindow
功能 : This function will return foreground handle .
GetSafeHwnd
功能 : 這個函數其實就是像直接存取 h_wnd一樣,不過通常我都會使用這個比較安全。
HWND ThisWindowsHandle = GetSafeHwnd() ;
DBG("\n####### ThisWindowsHandle : 0x%p ####### \n" , ThisWindowsHandle ) ; //Print
FindWindow
Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.
To search child windows, beginning with a specified child window, use the FindWindowEx function.
Syntax
HWND WINAPI FindWindow( _In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowName );
Parameters
- lpClassName [in, optional]
-
Type: LPCTSTR
The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.
If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.
If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.
- lpWindowName [in, optional]
-
Type: LPCTSTR
The window name (the window's title). If this parameter is NULL, all window names match.
Return value
Type:
Type: HWND
If the function succeeds, the return value is a handle to the window that has the specified class name and window name.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
If the lpWindowName parameter is not NULL, FindWindow calls the GetWindowText function to retrieve the window name for comparison. For a description of a potential problem that can arise, see the Remarks for GetWindowText.
Examples
For an example, see Retrieving the Number of Mouse Wheel Scroll Lines.