Win32 窗口篇(1)
?
例子來自某書…
目標:會用就OK,達到熟悉Win32 API的目的.
1.1 如何通過HWND獲得CWnd指針
CWnd::GetSafeHwnd:
Returns the window handle for a window. Returns NULL if the CWnd is not attached to a window or if it is used with a NULL CWnd pointer.
CWnd::FromHandle:
Returns a pointer to a CWnd object when given a handle to a window. If a CWnd object is not attached to the handle, a temporary CWnd object is created and attached.
1.2 如何獲得應用程序主窗口的指針
使用AfxGetApp獲取應用程序指針
//獲得應用程序指針 CDemoApp* pApp = (CDemoApp*)AfxGetApp();//獲得主窗口指針 CWnd* pMainWnd = pApp->m_pMainWnd;1.3 如何獲得指定點的窗口
如下代碼:
void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point) {//獲得指定點的窗口CWnd* pWnd = WindowFromPoint(point);if (pWnd != NULL){if (IsChild(pWnd)){CString strText = _T("");pWnd->GetWindowText(strText);SetWindowText(strText);}}CDialog::OnMouseMove(nFlags, point); }方法1:WindowFromPoint
A pointer to the window object in which the point lies. It is NULL if no window exists at the given point. The returned pointer may be temporary and should not be stored for later use.
方法2:IsChild(MFC中每個控件都是一個窗體)
Determines whether the specified window is a child window.
方法3:GetWindowText和SetWindowText
獲取和設置窗體標題
如下效果:
當鼠標經過TextBox,則IsChild判斷為True,在TextBox范圍內鼠標移動則更改窗體標題
1.4 如何最大化和最小化窗口
示例如下:
void CDemoDlg::OnTest1() {//最大化窗口SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); }void CDemoDlg::OnTest2() {//最小化窗口SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); }void CDemoDlg::OnTest3() {//恢復窗口SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); }WM_SYSCOMMAND 為系統內置的命令,只需要用SendMessage發送預先設置的命令就可以了
1.5 如何關閉窗口
同上,用SendMessage發送預先設置的命令
void CDemoDlg::OnTest() {//關閉窗口SendMessage(WM_SYSCOMMAND,SC_CLOSE, 0); }或者直接發送WM_CLOSE
關閉前確認:
轉載于:https://www.cnblogs.com/Clingingboy/archive/2011/03/19/1989157.html
總結
以上是生活随笔為你收集整理的Win32 窗口篇(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【OpenCV学习笔记4】OpenCV
- 下一篇: std::ostringstream::