C++ WM_LBUTTONDOWN 在使用 WM_NCHITTEST 时不触发

C++ WM_LBUTTONDOWN not triggering when using WM_NCHITTEST

我正在使用 win32 创建带有自定义标题栏和边框的 window。我的问题是,当我使用 WM_NCHITTEST WM_LBUTTONDOWN 时,点击事件不会被触发。我在不使用 WM_NCHITTEST 时收到点击事件。我没有收到任何错误。

我已经尝试删除 WM_NCHITTEST 中的代码,所以我只 return 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

但是windows的哪一部分是客户区?确定此存在 WM_NCHITTEST 消息:

Sent to a window in order to determine what part of the window corresponds to a particular screen coordinate

仅当您 return HTCLIENT 响应 WM_NCHITTEST - 您在客户区。只有在这种情况下你得到 WM_LBUTTONDOWN

但你从不return HTCLIENT回应WM_NCHITTEST - 你总是return 0,这意味着HTNOWHERE

所以 return HTNOWHERE 你需要调用 DefWindowProc 和 return 它的值:

//return HTNOWHERE;
return DefWindowProc(hWnd, Msg, wParam, lParam);

或您自己以某种方式检测您的情况下的客户区和 return HTCLIENT 当光标位于 window

的客户区时