原作者版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://keegoo.blogbus.com/logs/31246850.html
在编写基于对话框的程序的时候,很喜欢将Border的属性设置为none,这样整个对话框看起来什么也没有(没有棱角也没有非客户区),这样你可以用一些好看的图片自己设置绘制非客户区和对话框边缘。但唯一不爽的是,对话框不能跟随鼠标移动,也不能通过鼠标改变大小。以下的方法会实现这些功能:
在对话框类当中添加一个变量m_nHitTest,
1.添加WM_MOUSEMOVE消息处理函数,在处理函数中添加以下代码:
CRect rect;
GetClientRect(&rect);
if(point.x <= rect.left+3)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
m_nHitTest = HTLEFT;
}
else if(point.x >= rect.right-3)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
m_nHitTest = HTRIGHT;
}
else if(point.y <= rect.top+3)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
m_nHitTest = HTTOP;
}
else if(point.y >= rect.bottom-3)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
m_nHitTest = HTBOTTOM;
}
else if(point.x <= rect.left+10 && point.y <= rect.top+10)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
m_nHitTest = HTTOPLEFT;
}
else if(point.x >= rect.right-10 && point.y <= rect.top+10)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
m_nHitTest = HTTOPRIGHT;
}
else if(point.x <= rect.left+10 && point.y >= rect.bottom-10)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
m_nHitTest = HTBOTTOMLEFT;
}
else if(point.x >= rect.right-10 && point.y >= rect.bottom-10)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
m_nHitTest = HTBOTTOMRIGHT;
}
else
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
m_nHitTest = 0;
}
2.添加WM_LBUTTONDOWN消息处理函数,在处理函数中添加以下代码:
if(m_nHitTest == HTTOP)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTBOTTOM)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTLEFT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTTOPLEFT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTTOPRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTBOTTOMLEFT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
}
else if(m_nHitTest == HTBOTTOMRIGHT)
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
}
else
{
//实现对话框跟随鼠标移动
::SendMessage (GetSafeHwnd(), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}