2017-07-12 47 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()

希望這可以幫助別人。

相關問題