2017-07-12 44 views
-2

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

enter image description here

正如你所看到的是按键的按键是在小窗口中稍轻。 (不幸的是,你不能在屏幕截图中看到鼠标的位置)但是我可以确认鼠标位于正确的位置。

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

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() dosnt将窗口置于forground。通话SetForegroundWindow()经过我测试:

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

输出为:WTF? not on Foreground?!

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

+0

游戏总是防御假冒投入。正如这里每天在类似的问题中讨论的那样。毫无疑问WOW会这样做。 –

+0

昨天它简短的工作。因此,我会说这个说法是错误的。 – Lendoria

+0

我想下一步就是修复代码中的所有缺陷。当然你对'SendInput'的使用严重破坏。千万不要一次发送一个事件。对于鼠标点击,您发送两个事件,一个长度为二的数组,第一个是鼠标向下,第二个是鼠标向上。 –

回答

-2

我想我找到了褶皱。

我需要使用AttachThreadInput()和使用的一些功能,这样:SetFocus() SetActiveWindow() SetWindowPos() SetForegroundWindow()

希望这可以帮助别人。

相关问题