-2

I have a problem and hope you have any solution for this problem! When executing from the SendInput function, the mouse is put in the right place but comically does not come a click. Did I possibly forget something?

enter image description here

As you can see is the button of the button was slightly lighter in the small window. (Unfortunately you can not see the mouse position in the screenshot) But I can confirm that the mouse is in the right place.

I thank you very much for your time and effort.

    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
            }
    }

Debug:

GetLastError() always = 0 SendInput() callback = 1

I have test with only big Window (add function ClickOnWindowPosition()) works without problems.

Now im confused why it dont work on small window :/

Can it be that although the function SetForegroundWindow () was called, that the window is not in the foreground?

I have test this i know now why is not clicked. The Function SetForegroundWindow() on function ClickOnWindowPosition() dosnt bring the window in forground. After call SetForegroundWindow() i test this:

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

The Output is: WTF? not on Foreground?!

So the window are not in foreground, why :P? The same procedure I have already used for the size adjustment. Without problems :/

Lendoria
  • 127
  • 1
  • 11
  • Games invariably defend against fake input. As is discussed in similar questions here every day. Doubtless WOW will do so. – David Heffernan Jul 12 '17 at 09:33
  • Yesterday it worked briefly. Therefore, I would say that this statement is wrong. – Lendoria Jul 12 '17 at 09:40
  • I guess the next step then is to fix all the defects in your code. Certainly your use of `SendInput` is badly broken. Don't ever send events one at a time. For a mouse click you send two events, an array of length two, first one is the mouse down, second one is the mouse up. – David Heffernan Jul 12 '17 at 09:49
  • 1
    Does it work on other windows, like MS Paint? – rustyx Jul 12 '17 at 09:57
  • I have now test with the Big Window. (code added) It works without problems. The click function works fine, only on small window not :/ – Lendoria Jul 12 '17 at 10:14
  • #David Heffernan, I can refuse that. ;) You can send one event for mouse click. For keyevents your answer is right. – Lendoria Jul 12 '17 at 10:17

1 Answers1

-2

I think i have the fold found.

I need use AttachThreadInput() and use some of this functions: SetFocus() SetActiveWindow() SetWindowPos() SetForegroundWindow()

hope this helps others.

Lendoria
  • 127
  • 1
  • 11