MFC重繪非客戶區
阿新 • • 發佈:2019-01-04
3.拉伸視窗
新增WM_NCHITTEST和WM_NCLBUTTONDOWN
我們先新增一個變數,是否可以拉伸,預設初始化為true即可,畢竟視窗拉伸是視窗基本的屬性之一
bool m_bExtrude; //是否可以拉伸
LRESULT CDialogEx::OnNcHitTest(CPoint point) { if( m_bExtrude ) { CRect rcWindow; GetWindowRect(&rcWindow); if ((point.x <= rcWindow.left+BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) ) return HTLEFT; else if ((point.x >= rcWindow.right-BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) ) return HTRIGHT; else if ((point.y <= rcWindow.top+BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2)) return HTTOP; else if ((point.y >= rcWindow.bottom-BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2)) return HTBOTTOM; else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2)) return HTTOPLEFT; else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2)) return HTTOPRIGHT; else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2)) return HTBOTTOMLEFT; else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2)) return HTBOTTOMRIGHT; else return CWnd::OnNcHitTest(point); return 0; } else return CDialog::OnNcHitTest(point); } void CDialogEx::OnNcLButtonDown(UINT nHitTest, CPoint point) { if( m_bExtrude ) { if (nHitTest == HTTOP) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTBOTTOM) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTLEFT) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTRIGHT) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTTOPLEFT) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTTOPRIGHT) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTBOTTOMLEFT) SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y)); else if (nHitTest == HTBOTTOMRIGHT) SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y)); else if (nHitTest==HTCAPTION) SendMessage(WM_SYSCOMMAND, SC_MOVE | 4, MAKELPARAM(point.x, point.y)); } CDialog::OnNcLButtonDown(nHitTest, point); }