這代碼的確很老,網上也沒別的了。。。。
LRESULT CbarDlg::OnNcHitTest(CPoint point){// TODO: 在此添加訊息處理常式代碼和/或調用預設值const int borderwidth = 7;CRect rect;GetClientRect(rect);ScreenToClient(&point);if (point.y < borderwidth){return HTTOP;}if (point.x < borderwidth){return HTLEFT;}if(point.y > rect.bottom - rect.top - borderwidth && point.x > rect.right - rect.left - borderwidth)return HTBOTTOMRIGHT;if (point.y > rect.bottom - rect.top - borderwidth){return HTBOTTOM;}if (point.x > rect.right - rect.left - borderwidth){return HTRIGHT;}return HTCAPTION;//return CDialog::OnNcHitTest(point);}void CbarDlg::OnNcLButtonDown(UINT nHitTest, CPoint point){// TODO: 在此添加訊息處理常式代碼和/或調用預設值// TODO: 在此添加訊息處理常式代碼和/或調用預設值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));}
另一種
LRESULT CSocketPhoneDlg::OnNcHitTest(CPoint point){// TODO: 在此添加訊息處理常式代碼和/或調用預設值static int m_TitleHeight = 5 , m_BorderLeftWidth = 5 ,m_BorderRightWidth=5, m_BorderBottomHeight = 5;CWnd *pWnd = CWnd::FromHandle(m_hWnd);CRect wr;pWnd->GetWindowRect(wr);point.x -= wr.left;point.y -= wr.top;CRect rc;int cx = GetSystemMetrics(SM_CXSMICON);int cy = GetSystemMetrics(SM_CYSMICON);rc = CRect( 0, 0, m_BorderLeftWidth, m_TitleHeight );if ( PtInRect( rc, point )) //!IsZoomed(m_hWnd) )return HTTOPLEFT;rc = CRect( wr.Width() - m_BorderLeftWidth, 0, wr.Width(), m_TitleHeight );if ( PtInRect( rc, point ) ) //!IsZoomed(m_hWnd) )return HTTOPRIGHT;rc = CRect( 0, wr.Height() - m_BorderBottomHeight, m_BorderLeftWidth, wr.Height() );if ( PtInRect( rc, point ) ) //!IsZoomed(m_hWnd) )return HTBOTTOMLEFT;rc = CRect( wr.Width()-m_BorderRightWidth, wr.Height() - m_BorderBottomHeight, wr.Width(), wr.Height() );if ( PtInRect( rc, point )) //!IsZoomed(m_hWnd) )return HTBOTTOMRIGHT;rc = CRect( 0, m_TitleHeight, m_BorderLeftWidth, wr.Height() - m_BorderBottomHeight );if ( PtInRect( rc, point )) //!IsZoomed(m_hWnd) )return HTLEFT;rc = CRect( wr.Width()-m_BorderRightWidth, m_TitleHeight, wr.Width(), wr.Height() - m_BorderBottomHeight );if ( PtInRect( rc, point ) ) //!IsZoomed(m_hWnd) )return HTRIGHT;rc = CRect( m_BorderLeftWidth, wr.Height() - m_BorderBottomHeight, wr.Width()-m_BorderRightWidth, wr.Height() );if ( PtInRect( rc, point ) ) //!IsZoomed(m_hWnd) )return HTBOTTOM;rc = CRect( m_BorderLeftWidth, 0, wr.Width()-m_BorderRightWidth, m_BorderBottomHeight );if ( PtInRect( rc, point )) //!IsZoomed(m_hWnd) )return HTTOP;return HTCLIENT;return CDialogEx::OnNcHitTest(point);}