简体   繁体   English

如何在c#中执行鼠标事件? 这里的问题在哪里

[英]How to perform mouse event in c#? where is the probleme here

I'm trying do do three mousclicks in my c# programm.我正在尝试在我的 c# 程序中点击鼠标三下。 I found this on the inte.net: https://forum.chip.de/discussion/1668318/visual-studio-c-2010-mausklick-simulieren我在 inte.net 上找到了这个: 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任何人都知道这里的问题在哪里

If you were to move mouse to the position of (1700, 1138) then click, try this instead:如果您要将鼠标移动到 (1700, 1138) 的 position,然后单击,试试这个:

 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?)但这些并没有将鼠标移动到屏幕上的绝对点 (1700, 1138),而是它源自最后一个鼠标事件过程 position 或默认情况下控制台左上角(我猜?)

If you want to move the mouse to absolute position on screen, combine it with the flag MOUSEEVENTF_ABSOLUTE (0x8000) as following:如果要将鼠标移动到屏幕上的绝对 position,请将其与标志MOUSEEVENTF_ABSOLUTE (0x8000) 结合使用,如下所示:

 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请注意,我将它与(ushort.MaxValue / SCREEN_WIDTH)(ushort.MaxValue / SCREEN_HEIGHT) 相乘,如https://learn.microsoft.com/en-us/windows/win32/api/winuser中的备注部分所述/nf-winuser-mouse_event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM