I'm trying do do three mousclicks in my c# programm. I found this on the internet: https://forum.chip.de/discussion/1668318/visual-studio-c-2010-mausklick-simulieren
So I've tried this, but for me nothing is happening.
private const UInt32 MouseEventLeftUp = 0x0004;
[DllImport("user32", EntryPoint = "mouse_event")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
And here the mousclicks i'm trying
Console.WriteLine(DateTime.Now " Vollbild");
mouse_event(MouseEventLeftDown, 1700, 1138, 0, new System.IntPtr());
mouse_event(MouseEventLeftUp, 1700, 1138, 0, new System.IntPtr());
Thread.Sleep(2000);
// OEE
Console.WriteLine(DateTime.Now " OEE");
mouse_event(MouseEventLeftDown, 1470, 380, 0, new System.IntPtr());
mouse_event(MouseEventLeftUp, 1470, 380, 0, new System.IntPtr());
Thread.Sleep(15000);
//Astronic
Console.WriteLine(DateTime.Now " Astronic");
mouse_event(MouseEventLeftDown, 220, 370, 0, new System.IntPtr());
mouse_event(MouseEventLeftUp, 220, 370, 0, new System.IntPtr());
Anyone have a idea where is the problem here
CodePudding user response:
If you were to move mouse to the position of (1700, 1138) then click, try this instead:
mouse_event(0x0001, 1700, 1138, 0, new IntPtr(0))
mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr(0))
mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr(0))
but these ain't moving the mouse to an absolute point (1700, 1138) on screen, instead it derived from the last mouse event proc position or the console top left by default (I guess?)
If you want to move the mouse to absolute position on screen, combine it with the flag MOUSEEVENTF_ABSOLUTE (0x8000) as following:
mouse_event(0x8000 | 0x0001, (ushort.MaxValue / SCREEN_WIDTH) * 1700, (ushort.MaxValue / SCREEN_HEIGHT) * 1138, 0, new IntPtr(0));
mouse_event(LEFT_DOWN, 0, 0, 0, new System.IntPtr(0));
mouse_event(LEFT_UP, 0, 0, 0, new System.IntPtr(0));
Note that I multiplied it with (ushort.MaxValue / SCREEN_WIDTH), (ushort.MaxValue / SCREEN_HEIGHT), as explained in the Remark section in https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event