主要需要添加处理WM_NCHITTEST,WM_SETCURSOR和WM_NCLBUTTONDOWN消息
版本1.0(本版本有问题,右下角不响应消息)
case WM_NCHITTEST:
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
RECT rect;
GetWindowRect(hWnd,&rect);
if(point.x <= rect.left+5)
{
return HTLEFT;
}
else if(point.x >= rect.right-5)
{
return HTRIGHT;
}
else if(point.y <= rect.top+5)
{
return HTTOP;
}
else if(point.y >= rect.bottom-5)
{
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 ::DefWindowProc(hWnd, message, wParam, lParam);
case WM_SETCURSOR:
{
UINT nHitTest = LOWORD(lParam);
switch(nHitTest)
{
case HTTOP:
case HTBOTTOM:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
break;
case HTLEFT:
case HTRIGHT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
break;
case HTTOPLEFT:
case HTBOTTOMRIGHT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
break;
case HTTOPRIGHT:
case HTBOTTOMLEFT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
break;
case HTCAPTION:
case HTSYSMENU:
case HTMENU:
case HTCLIENT:
default:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
break;
}
}
return TRUE;
case WM_NCLBUTTONDOWN:
switch(wParam)
{
case HTTOP:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, lParam);
break;
case HTBOTTOM:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, lParam);
break;
case HTLEFT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, lParam);
break;
case HTRIGHT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, lParam);
break;
case HTTOPLEFT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, lParam);
break;
case HTTOPRIGHT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, lParam);
break;
case HTBOTTOMLEFT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, lParam);
break;
case HTBOTTOMRIGHT:
SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, lParam);
break;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
版本2.0(修正版本1.0的问题)
switch (message)
{
case WM_NCHITTEST:
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
RECT rect;
GetWindowRect(&rect);
if(point.x <= rect.left+5)
{
return HTLEFT;
}
else if(point.x >= rect.right-5 && point.y < rect.bottom-10)
{
return HTRIGHT;
}
else if(point.y <= rect.top+5)
{
return HTTOP;
}
else if(point.y >= rect.bottom-5&& point.x < rect.right-10)
{
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;
}
case WM_SETCURSOR:
{
UINT nHitTest = LOWORD(lParam);
switch(nHitTest)
{
case HTTOP:
case HTBOTTOM:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
break;
case HTLEFT:
case HTRIGHT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
break;
case HTTOPLEFT:
case HTBOTTOMRIGHT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
break;
case HTTOPRIGHT:
case HTBOTTOMLEFT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
break;
case HTCAPTION:
case HTSYSMENU:
case HTMENU:
case HTCLIENT:
default:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
break;
}
}
return TRUE;