1. 实现自绘窗口的Resize
自绘的主窗口,往往没有WS_BORDER【特别是Win7下的Border,厚厚的~】,但是没有Border,就等于没有了WS_SIZEBOX。那么我们怎么样才能实现同等的功能呢?
接管以下三个消息:
MESSAGE_HANDLER(WM_NCHITTEST, OnNcHitTest)
MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
MESSAGE_HANDLER(WM_NCLBUTTONDOWN, OnNcLButtonDown)
MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
MESSAGE_HANDLER(WM_NCLBUTTONDOWN, OnNcLButtonDown)
第一个好说,到处都是MFC的例子,判断以下Border的位置,返回HTBOTTOMRIGHT 这些值即可。
Cursor这里,我直接给出WTL的代码好了,以后直接抄:
LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
int nHitTest = LOWORD(lParam);
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 S_OK;
{
int nHitTest = LOWORD(lParam);
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 S_OK;
}
NCLBUTTONDOWN可以这么写:
LRESULT OnNcLButtonDown(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
POINT point;
point.x = HIWORD(lParam);
point.y = LOWORD(lParam);
int nHitTest = LOWORD(wParam);
if(nHitTest == HTTOP)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOM)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTRIGHT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTTOPLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTTOPRIGHT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOMLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOMRIGHT)
pT->SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
return 0;
{
T* pT = static_cast<T*>(this);
POINT point;
point.x = HIWORD(lParam);
point.y = LOWORD(lParam);
int nHitTest = LOWORD(wParam);
if(nHitTest == HTTOP)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOM)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTRIGHT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTTOPLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTTOPRIGHT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOMLEFT)
pT->SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
else if(nHitTest == HTBOTTOMRIGHT)
pT->SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
return 0;
}
参考:
http://hi.baidu.com/bdxiong/blog/item/b7273b3031103294a9018e06.html