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?
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 :/