MFC 无边框对话框调整大小问题

5522 人浏览 | 时间: 2015-06-26 14:29:35 | 作者: codexia

现在很多软件的界面都会选择无边框窗口,所以在做MFC对话框时总结了无边框的移动问题。
无边框: 对话框 属性:border -> none;
现在的对话框变为无边框的了,一些移动、调整大小都必须我们手动去添加消息函数去操作了。
先说说窗口移动吧:
实现 WM_LBUTTONDOWN 消息 ;实现鼠标按下移动位置问题;
函数内实现代码:

SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
PostMessage(WM_LBUTTONUP,NULL,MAKELPARAM(point.x,point.y));

也是通过发消息来实现... 

接下来就是鼠标在边框处的调整大小问题:
(1)实现WM_NCHITTEST消息,实现四条边框的模拟
(2)只要鼠标移动OnMouseMOve就会自动发送 WM_SETCURSOR从而触发OnSetCursor,鼠标变化.
(3)实现WM_NCLBUTTONDOWN,发送窗体拖曳变化的消息

代码实现:
(1)

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;
if(nHitTest == HTCAPTION || nHitTest == HTSYSMENU || nHitTest == HTMENU || nHitTest == HTCLIENT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW))); //IDC_ARROW 标准的箭头
return 0;
}
else if(nHitTest == HTTOP || nHitTest == HTBOTTOM)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));//IDC_SIZENS 双箭头指向南北
return 0;
}
else if(nHitTest == HTLEFT || nHitTest == HTRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));//IDC_SIZEWE 双箭头指向东西
return 0;
}
else if(nHitTest == HTTOPLEFT || nHitTest == HTBOTTOMRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));//IDC_SIZENWSE 双箭头指向西北和东南
return 0;
}
else if(nHitTest == HTTOPRIGHT || nHitTest == HTBOTTOMLEFT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW))); //IDC_SIZENESW 双箭头指向东北和西南
return 0;
}
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));
请微信扫码阅读
为防止恶意爬虫,
已开启反爬机制

相关阅读