【问题标题】:SendInput() MouseClick have the right Position but no clickSendInput() MouseClick 有正确的位置但没有点击
【发布时间】:2017-07-12 09:09:36
【问题描述】:

我有一个问题,希望你有任何解决这个问题的方法!从 SendInput 函数执行时,鼠标放在正确的位置,但可笑的是没有点击。我可能忘记了什么吗?

如您所见,按钮的按钮在小窗口中略亮。 (很遗憾你在截图中看不到鼠标位置)但是我可以确认鼠标在正确的位置。

非常感谢您的时间和精力。

    bool ClickOnWindowPosition(HWND window, int x, int y)
{
    if (window == NULL)
    {
        cout << "ClickOnWindowPosition() parameter window is null" << endl;
        return false;
    }

    INPUT input;
    input.type = INPUT_MOUSE;

    if (SetForegroundWindow(window))
    {
        input.mi.dx = x * (65536.0f / GetSystemMetrics(SM_CXSCREEN));
        input.mi.dy = y * (65536.0f / GetSystemMetrics(SM_CYSCREEN));
        input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP);
        input.mi.mouseData = 0;
        input.mi.dwExtraInfo = NULL;
        input.mi.time = 0;

        //SetCursorPos(new_cursor_x, (new_cursor_y + windowPosForeground.bottom));

        SendInput(1, &input, sizeof(INPUT));
    }
    else {
        cout << "SetForegroundWindow() cant bring window in foreground" << endl;
        return false;
    }

    return true;
}

    int main(){
         vector<HWND> StoreWindows;
//mouseBroadCaststate called by other function

                if (mouseBroadCaststate)
            {
                //cout << GetAsyncKeyState(VK_LBUTTON) << endl;
                if (GetAsyncKeyState(VK_LBUTTON)) {

                    GetCursorPos(&pos_cursor);

                    cout << "POS X:" << pos_cursor.x << "POS Y: " << pos_cursor.y << endl;


                    //Find the current Forground Window from the Store and get ClientRect data.
                    HWND hwndForegroundWindow = NULL;
                    for (int x = 0; x < StoreWindows.size(); x++)
                    {
                        if (IsWindowOnFocus(StoreWindows[x])) {
                            hwndForegroundWindow = StoreWindows[x];
                            if (!GetClientRect(StoreWindows[x], &windowPosForeground))
                            {
                                cout << "Error: GetClientRect()" << endl;
                            }
                            break;
                        }
                    }



                    //BLOCK CODE IF NOT THE CORRECT WINDOW FIND
                    if(hwndForegroundWindow != NULL)
                    {
                    //cout << "Debug: " << windowPosForeground.bottom << endl;
                        for (int x = 0; x < StoreWindows.size(); x++)
                        {
                            if (StoreWindows[x] != NULL && GetForegroundWindow() != StoreWindows[x])
                            {

                                GetClientRect(StoreWindows[x], &windowPos);

                                //Get the new cursor position for the small windows...
                                float pos_cursor_math = (((float)pos_cursor.y / (float)windowPosForeground.bottom) * 100);

                                float posCursorSmallWindow = (((float)windowPos.bottom / 100) * pos_cursor_math);
                                long new_cursor_y = posCursorSmallWindow + windowPosForeground.bottom;

                                long new_cursor_x = pos_cursor.x / 4;


                                if (ClickOnWindowPosition(StoreWindows[x], new_cursor_x, new_cursor_y))
                        {
                            cout << "MouseClick success." << endl;
                        }
                            }
                        }
                    }

                    //Back to the roots...
                    //SetCursorPos(pos_cursor.x, pos_cursor.y);
                    SetForegroundWindow(hwndForegroundWindow);

                    //Break the mouseBraodCast
                    mouseBroadCaststate = false;
                } //END VK_LBUTTON
            }
    }

调试:

GetLastError() 总是 = 0 SendInput()回调 = 1

我只有大窗口的测试(添加函数ClickOnWindowPosition())没有问题。

现在我很困惑为什么它不能在小窗口上工作:/

会不会是虽然调用了函数SetForegroundWindow (),但是窗口不在前台?

我已经对此进行了测试,我现在知道为什么没有点击。函数SetForegroundWindow() 上的函数ClickOnWindowPosition() 不会将窗口置于前台。致电SetForegroundWindow() 后,我对此进行了测试:

if (GetForegroundWindow() != window)
        {
            cout << "WTF? not on Foreground?!" << endl;
        }

输出是:WTF? not on Foreground?!

所以窗口不在前台,为什么:P? 我已经用于尺寸调整的相同程序。没有问题:/

【问题讨论】:

  • 游戏总是防御虚假输入。正如每天在类似问题中讨论的那样。毫无疑问,WOW 会这样做。
  • 昨天它工作了一段时间。因此,我会说这种说法是错误的。
  • 我想下一步就是修复代码中的所有缺陷。当然,您对SendInput 的使用被严重破坏了。永远不要一次发送一个事件。对于鼠标单击,您发送两个事件,一个长度为 2 的数组,第一个是鼠标向下,第二个是鼠标向上。
  • 它是否可以在其他窗口上使用,例如 MS Paint?
  • 我现在已经用大窗口进行了测试。 (添加代码)它可以正常工作。点击功能工作正常,只在小窗口不是:/

标签: c++ winapi input hwnd


【解决方案1】:

我想我找到了折叠。

我需要使用AttachThreadInput() 并使用其中一些功能:SetFocus() SetActiveWindow() SetWindowPos() SetForegroundWindow()

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 2014-10-11
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多