顺便记录一下有关窗口对象指针和窗口句柄相关的函数。
Win32 API函数对窗口的操作总是需要一个窗口句柄(hWnd)来指向需要操作的对象;比如::SetWindowPos(hWnd,...)
而MFC内,窗口句柄已经包含在对象成员内,需要的是指向窗口对象的指针(pWnd),这样就可以对此对象的成员函数操作了。比如pWnd->SetWindowPos(...)
常用获得函数:
AfxGetApp() // 这个不是窗口,相当于theApp,顺便列在这里。
以下是MFC中获取窗口对象指针的函数(Win32 API的话就是获得窗口句柄):
AfxGetMainWnd()
GetParent()
GetOwner()
GetDlgItem( int nID )
GetActiveWindow( )
GetTopWindow( ) // 获取Child风格的窗口对象指针
GetForegroundWindow( )
GetPane( int row, int col )
FindWindow( LPCTSTR lpszClassName, LPCTSTR lpszWindowName )
GetWindow( UINT nCmd )
FromHandle( HWND hWnd )
GetTopLevelParent( )
GetTopLevelOwner( ) // the window that is a child of the desktop
另外,在文档/视图架构程序中,还有:
GetActivePane(int* pRow = NULL, int* pCol = NULL) // Virtual, called by framework,Override to require an action by the user before getting the active pane
GetActiveView( ) // View也是从CWnd派生而来
GetActiveFrame( ) // Frame也是从CWnd派生而来 virtual
GetTopLevelFrame( ) // Frame也是从CWnd派生而来
以下是MFC中获取窗口句柄函数:
GetSafeHwnd( )
当然在MFC内,你通过以上诺干方法获取了窗口对象的指针,调用GetSafeHwnd( )也就获得了这些窗口的句柄。
另外顺便列些常用的获取句柄的函数:
AfxGetInstanceHandle( ) // 应用程序的Instance句柄,有些Win32 API函数调用的时候需要这个句柄。
AfxGetResourceHandle( )
AfxGetThread( )
GetMenu( )
GetDlgCtrlID( ) // The numeric identifier of the CWnd child window (not only for dialog window)