三步实现MFC对话框Boder为None对话框鼠标拖动缩放功能。
第一步,实现对话框的WM_NCHITTEST消息,代码如下:
LRESULT CTestDlg::OnNcHitTest(CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect rect;
GetWindowRect(&rect);
if (point.x <= rect.left + 3 && point.y <= rect.top + 3){
return HTTOPLEFT;
}
else if (point.x >= rect.right - 3 && point.y <= rect.top + 3){
return HTTOPRIGHT;
}
else if (point.x <= rect.left + 3 && point.y >= rect.bottom - 3){
return HTBOTTOMLEFT;
}
else if (point.x >= rect.right - 3 && point.y >= rect.bottom - 3){
return HTBOTTOMRIGHT;
}
else 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;
}
return ((GetStyle() & WS_CHILD) == 0) ? HTCAPTION : HTTRANSPARENT;
}
第二步,实现WM_SETCURSOR消息,代码如下:
BOOL CTestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
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)));
}
return TRUE;
}
到此,鼠标移动到对话的边界上,鼠标样式会对应变化,但对话框是不能拖动缩放的。
第三步,实现WM_NCLBUTTONDOWN消息,代码如下:
void CTestDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
// 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
CDialogEx::OnNcLButtonDown(nHitTest, point);
}
至此,对话框就可以跟着鼠标实现缩放效果,代码完。