我正在使用 win32 创建一个带有自定义标题栏和边框的窗口。我的问题是,当我使用 WM_NCHITTEST
WM_LBUTTONDOWN
时,点击事件不会被触发。当我没有使用 WM_NCHITTEST
时,我收到了点击事件。我没有收到任何错误。
我已经尝试删除 WM_NCHITTEST
中的代码,所以我只返回 0
。我试过删除 WM_NCHITTEST
然后 WM_LBUTTONDOWN
工作,但我需要两者都工作。
//Declaring before switch
int x, y;
RECT rect;
int iTitlebarY = 20;
case WM_NCHITTEST: {
GetWindowRect(hwnd, &rect);
x = GET_X_LPARAM(lParam);
y = GET_Y_LPARAM(lParam);
if (y <= rect.top + iTitlebarY) {
return HTCAPTION;
}
//Some more code for resizing...
return 0;
}
case WM_LBUTTONDOWN:
//Never gets triggered
//Do something...
我预计我会收到鼠标点击,因为当我不使用 WM_NCHITTEST
时我会收到。
最佳答案
来自 WM_LBUTTONDOWN
留言
Posted when the user presses the left mouse button while the cursor is in the client area of a window
将此与 WM_NCLBUTTONDOWN
进行比较留言
Posted when the user presses the left mouse button while the cursor is within the nonclient area of a window
但是窗口的哪一部分是客户区?确定这个存在WM_NCHITTEST
留言:
Sent to a window in order to determine what part of the window corresponds to a particular screen coordinate
仅当您返回 HTCLIENT
以响应 WM_NCHITTEST
- 您在客户区。只有在这种情况下,您才会得到 WM_LBUTTONDOWN
但您永远不会返回 HTCLIENT
来响应 WM_NCHITTEST
- 您总是返回 0,这意味着 HTNOWHERE
所以不是返回 HTNOWHERE
你需要调用 DefWindowProc
并返回它的值:
//return HTNOWHERE;
return DefWindowProc(hWnd, Msg, wParam, lParam);
或者你自己以某种方式检测你的客户区是什么,并在光标位于窗口的客户区时返回 HTCLIENT
关于C++ WM_LBUTTONDOWN 在使用 WM_NCHITTEST 时不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57942789/