Hey,
I recently wanted to create a (yet) simple program that simulates a mousemovement.
So far I managed to make the program work. It does move the mouse, click when expected but the problem is the location it does click at.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <Windows.h>
#include <stdio.h>
int leftclick (DWORD x, DWORD y);
int main(){
POINT ptC;
POINT ptC2;
Sleep (5000);
leftclick (1920,1080);
GetCursorPos (&ptC);
printf ("First position:\nX-Cord: %d\nY-Cord: %d\n", ptC.x, ptC.y);
leftclick (100, 100);
GetCursorPos (&ptC2);
printf("Second position:\nX-Cord: %d\nY-Cord: %d\n", ptC2.x, ptC2.y);
system("PAUSE");
return 0;
}
int leftclick (DWORD dwx, DWORD dwy){
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dwExtraInfo = 0;
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
input.mi.mouseData = 0;
input.mi.time = 0;
input.mi.dx = (65535 / GetSystemMetrics (SM_CXSCREEN)) * dwx;
input.mi.dy = (65535 / GetSystemMetrics (SM_CYSCREEN)) * dwy;
SendInput (1, &input, sizeof(input));
return 0;
}
|
The problem now is:
I want the program (for testing purposes) to click at (1920, 1080) and (100, 100) afterwards.
Now it does click within a specific range. When I use GetCursorPos to retreive the cursors position it differs quite a bit from where I expected the click to be.
Any solutions, ideas, hints or at least SOMETHING I missed?
Oh, a second question I have is:
When I declare the following flag (in the code above) the program does use relative coordinates even though it shouldn't.
|
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
|
Where as it works out well when I add a MOUSEEVENTF_MOVE to it.
Why is that? I couldn't find any solution to this in Microsoft MSDN or any other website.
Thank you for your help!