主要通过处理WM_LBUTTONDOWN 消息, 将此消息转发出WM_NCLBUTTONDOWN 消息并将参数WPARAM设置为HTCAPTION
1. 處理WM_LBUTTONDOWD 消息 發送消息: SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0); 或 SendMessage(hWnd, WM_SYSCOMMAND, 0XF012, 0);
代碼:
a. 處理 WM_NCBUTTONDOWN 消息
LRESULT WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_LBUTTONDOWN:
SendMessage(hWnd, WM_NCLBUTTON, HTCAPTION, lParam);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
b. 處理 WM_SYSCOMMAND 消息
LRESULT WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_LBUTTONDOWN:
SendMessage(hWnd, WM_SYSCOMMAND, 0XF012, 0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
2. 處理WM_NCHITEST消息 直接返回 HTCAPTION
LRESULT WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_NCHITTEST:
return HTCAPTOIN
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}