实现效果需要处理以下这三个消息:
WM_NCHITTEST WM_SETCURSOR WM_NCLBUTTONDOWN
很多人在问三个函数是做什么用的,下面我大概介绍一下:
OnNcHitTest(UINT nHitTest, CPoint point)是用来测试鼠标的位置,Nc表示非客户区
OnSetCursor(HWND hWnd, UINT nHitTest, UINT message) 是用来设置鼠标状态
OnNcLButtonDown(UINT nHitTest, CPoint point)是用来处理非客户区的鼠标左键点击
有不懂的可以留言交流一下,希望本文章对大家有所帮助
消息处理函数:
UINT CXXXDlg::OnNcHitTest(UINT nHitTest, CPoint point)...{
CRect rect;
GetWindowRect(&rect);
if(point.x<=rect.left+3)
returnHTLEFT;
elseif(point.x>=rect.right-3)
returnHTRIGHT;
elseif(point.y<=rect.top+3)
returnHTTOP;
elseif(point.y>=rect.bottom-3)
returnHTBOTTOM;
elseif(point.x<=rect.left+10&&point.y<=rect.top+10)
returnHTTOPLEFT;
elseif(point.x>=rect.right-10&&point.y<=rect.top+10)
returnHTTOPRIGHT;
elseif(point.x<=rect.left+10&&point.y>=rect.bottom-10)
returnHTBOTTOMLEFT;
elseif(point.x>=rect.right-10&&point.y>=rect.bottom-10)
returnHTBOTTOMRIGHT;
else
return CDialog::OnNcHitTest(point);}
BOOL CXXXDlg::OnSetCursor(HWND hWnd, UINT nHitTest, UINT message)
...{
if(nHitTest==HTCAPTION||nHitTest==HTSYSMENU||
nHitTest==HTMENU||nHitTest==HTCLIENT)
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
elseif(nHitTest==HTTOP||nHitTest==HTBOTTOM)
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
}
elseif(nHitTest==HTLEFT||nHitTest==HTRIGHT)
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
}
elseif(nHitTest==HTTOPLEFT||nHitTest==HTBOTTOMRIGHT)
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
}
elseif(nHitTest==HTTOPRIGHT||nHitTest==HTBOTTOMLEFT)
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
}
else
...{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
}
voidCXXXDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
...{
if(nHitTest==HTTOP)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOP, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTBOTTOM)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTLEFT)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_LEFT, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTRIGHT)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTTOPLEFT)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTTOPRIGHT)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTBOTTOMLEFT)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
elseif(nHitTest==HTBOTTOMRIGHT)
SendMessage(WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
}