Original URL: http://www.pythonschool.com/CPP_JHK/5003.html
Gets the handle of the class window
this->m_hwnd; This is a pointer to an instance of the current class
Handle to the main window, get its own window handle
AfxGetMainWnd ()->m_hwnd; // in the main window class, or in the child window class, gets the main window handle method such as::: SendMessage (AfxGetMainWnd ()->m_hwnd , wm_my_dosome , 0 , (LPARAM) i); This->getsafehwnd ()// How to safely get a window handle pointer when we want a handle (HWND) for a Window object (a CWnd derived object) pointer, the safest way to do this is to use the GetSafeHwnd () function, as the following example looks at the reason: cwnd *pwnd = findwindow ("Explorewclass", NULL); //want to find the resource manager hwnd hwnd = pwnd->m_hwnd; // Get it's HWND like this code when you start to get a pwnd is empty, a "General protection error" appears and closes the application, because it is generally not possible to access its members to a null pointer if the following code is used: Cwnd *pwnd = findwindow ("Explorewclass", NULL); //want to find the resource manager hwnd hwnd = pwnd- >getsafehwnd (); //gets its hwnd without a problem because, although when pwnd is null, GetSafeHwnd can still be used, just return NULL, the implementation code through GETSAFEHWND () is clearer: _afxwin_inline hwnd cwnd::getsafehwnd () const{return this == null? Null:m_hwnd;} NOTE: This function first determines if the caller is empty. You see that there are many functions that require HWND, which can come in handy. hwnd hwnd; Cwnd* pwnd;pwnd= ((cframewnd*) (AfxGetApp ()->m_pmainwnd))->getactiveview (); HWNd=pwnd->getdlgitem (Idc_edit2)->getsafehwnd ();:: SetWindowText (Hwnd,m_strresult);
child window Handle with FindWindow, (just know the name of the child window)
:: FindWindow (Null,windowsname)//Return the handle of the found window//Find out whether the current window is running Windowname program, the name is the window name, such as: cwnd* Pwnd=findwindow (null,_t (" Notepad "));//
Gets the handle to the control in the window, using the GetDlgItem
CWnd *pwnd = GetDlgItem (id_***); Gets the pointer to the control HWND hwnd = Pwnd->getsafehwnd (); The handle syntax for getting the control is: GetDlgItem (IDC_COMBO3)->m_hwnd//parameter is the ID of the control, the return value is: cwnd* type, but it is a function within the CWnd class, so first get a pointer to the window where the control is located, such as: CString STR (_t ("DD"));:: SendMessage (AfxGetMainWnd ()->getdlgitem (IDC_COMBO3)->m_hwnd, Wm_settext, 0, (LPARAM) str. GetBuffer (0));
The subordinate handle obtains the pointer, uses the FromHandle
Syntax: cwnd* pWnd = Cwnd::fromhandle (hWnd);
Get the window handle correlation function method
This->m_hwndgetsafehwnd take the handle of the window class where you are GetActiveWindow take the current active window handle AfxGetMainWnd take the main window handle GetForegroundWindow Take the foreground window handle Findwindowenumwindow
Gettopwindow?
Gettopwindow function Function: This function examines the z-order of the child window associated with a particular parent window (Z-order: The direction of the vertical screen, that is, the stacking order), and returns a handle to the child window at the top of the z-order. Function prototype: HWND? Gettopwindow (Hwnd?hwnd);? Parameters:??? HWnd: The handle of the parent window being sequenced. If the parameter is NULL, the function returns the window handle at the top of the z-order. Return value:?? If the function succeeds, the return value is the child window handle at the top of the z-order. If the specified window has no child windows, the return value is null.
GetForegroundWindow
GetForegroundWindow function Function: This function returns the window handle of the foreground window of the current system. Function prototype: HWND GetForegroundWindow (VOID) return value: The function returns a handle to the foreground window back.
GetActiveWindow
function function: The function can get the window handle of the active window associated with the message queue of the thread that called the method (that is, the window handle of the active window that gets the current process). Function prototype: HWND GetActiveWindow (VOID) return value: The return value is a handle to the active window associated with the calling thread's message queue. Otherwise, the return value is null.
IsWindowVisible
function function: This function obtains the visual state of a given window. Function prototype: BOOL IsWindowVisible (HWND hwnd); HWnd: The handle of the window being tested. Return value: If the specified window and its parent window have a ws_visible style, the return value is not 0, and the return value is zero if the specified window and its parent window do not have a ws_visible style. Because the return value indicates whether the window has a ws_visible style, the function return value is nonzero even if the window is obscured by other windows. Note: The visual state of the window is indicated by the ws_visible bit. When the ws_visible bit is set, the window can be displayed, and any information that is drawn in the window will be displayed as long as the window has a ws_visible style.
IsWindow
function function: This function determines whether a given window handle indicates a window that already exists. Function prototype: BOOL IsWindow (HWND hwnd); parameter: hwnd: The handle of the window being tested. Return value: If the window handle identifies an existing window, the return value is ture, and the return value is false if the window handle does not identify an existing window.
FindWindow
HWND FindWindow (LPCSTR lpclassname,lpcstr lpwindowname); parameter: Lpclassname points to a null-terminated string that specifies the class name or an atom that can determine the class name string. If this argument is an atom, then it must be a global atom that has been created by the Globaladdatom function before calling this function. This atom (a 16bit value) must be placed in the low byte of the lpclassname, and the high byte of the Lpclassname is zeroed.
Lpwindowname
Points to a null-terminated string that specifies the window name (that is, the window caption). If this parameter is null, all window names are matched. Return value: If the function executes successfully, the return value is the handle to the window that owns the specified window class name or window name. If the function execution fails, the return value is NULL. You can get more detailed error information by calling the GetLastError function. For an application, the change of the window title can be achieved by SetWindowText: note: If the window itself property is not displayed, the function's call will not affect the window properties. Set title for application ' s main frame window. AfxGetMainWnd (), SetWindowText (_t ("Application title"))//set title for View's MDI child frame window. GetParentFrame (), SetWindowText ("_t (" MDI child Frame new title ")//set the title for dialog ' s push Button control. Getdigitem (Idc_button), SetWindowText (_t ("button new title")
Get the CDC pointer
cdc* PDC = PWND->GETWINDOWDC ();
1234567891011121314151617181920212223242526272829 |
使用到的类需要包含响应的头文件。首先一般获得本类(视,文档,对话框都支持)实例指针
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得到指针
最后提醒大家,在提取到各个句柄之后,因为初次提取的都是标准类句柄,所以,在使用时要注意将标准句柄转换成自己的类的句柄。
如:
AfxGetApp();
//得到的是WinApp类的句柄,
所以操作前记得转换成自己定义的类的句柄。
如:
((CMyApp*)AfxGetApp())->XXXX();
//这的xxxx()就是你定义的类中间的成员。
|
"Go" MFC Window handle all kinds of pointer get function