无边框对话框改变大小

时间:2021-01-29 10:49:07

1.  主要涉及三个消息WM_NCHITTEST     WM_SETCURSOR     WM_NCLBUTTONDOWN


OnNcHitTest(UINT nHitTest, CPoint point)是用来测试鼠标的位置,Nc表示非客户区,  它可以把改变点击的类型,比如,点击的是client,可以改成点击的ncclient,

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)
return HTLEFT;
else if(point.x >= rect.right-3)
return HTRIGHT;
else if(point.y <= rect.top+3)
return HTTOP;
else if(point.y >= rect.bottom-3)
return HTBOTTOM;
else if(point.x <= rect.left+10 && point.y <= rect.top+10)
return HTTOPLEFT;
else if(point.x >= rect.right-10 && point.y <= rect.top+10)
return HTTOPRIGHT;
else if(point.x <= rect.left+10 && point.y >= rect.bottom-10)
return HTBOTTOMLEFT;
else if(point.x >= rect.right-10 && point.y >= rect.bottom-10)
return HTBOTTOMRIGHT;

return 0;
}


BOOL CXXXDlg::OnSetCursor(HWND hWnd, UINT nHitTest, UINT message)
{
if(nHitTest == HTCAPTION || nHitTest == HTSYSMENU ||
nHitTest
== HTMENU || nHitTest == HTCLIENT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
else if(nHitTest == HTTOP || nHitTest == HTBOTTOM)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
}
else if(nHitTest == HTLEFT || nHitTest == HTRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
}
else if(nHitTest == HTTOPLEFT || nHitTest == HTBOTTOMRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
}
else if(nHitTest == HTTOPRIGHT || nHitTest == HTBOTTOMLEFT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
}
else
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
}


void CXXXDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
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));
}