admin管理员组

文章数量:1389872

I am using a global mouse hook (WH_MOUSE_LL) to detect left mouse clicks on a specific button in another application. The detection works fine when clicking within the main area of the button, but it becomes inaccurate when I click on the button's border.

bool leftButtonDown = false;
HWND clickedHandle = nullptr;

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0)
    {
        return CallNextHookEx(nullptr, nCode, wParam, lParam);
    }

    auto pMouseStruct = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
    if (pMouseStruct == nullptr)
    {
        return CallNextHookEx(nullptr, nCode, wParam, lParam);
    }

    switch (wParam)
    {
    case WM_LBUTTONDOWN:
        leftButtonDown = true;
        clickedHandle = WindowFromPoint(pMouseStruct->pt);
        break;

    case WM_LBUTTONUP:
        if (leftButtonDown)
        {
            HWND releaseHandle = WindowFromPoint(pMouseStruct->pt);
            if (clickedHandle == (HWND)0x00310716 && clickedHandle == releaseHandle)
            {
                std::cerr << "Button clicked" << '\n';
            }
        }
        leftButtonDown = false;
        break;

    default:
        break;
    }
    return CallNextHookEx(nullptr, nCode, wParam, lParam);
}

本文标签: winapiHow to check click button handle CStack Overflow