鼠标点击模拟桌面

本文关键字:桌面 模拟 鼠标 | 更新日期: 2023-09-27 18:13:15

我写了一个鼠标点击模拟的代码,但有一个问题。它不是每一秒都点击,但光标在移动。有人能帮我吗?下面是一个代码:

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
            [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetCursorPos(int X, int Y);
    private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
            private const int MOUSEEVENTF_MOVE = 0x0001;
            private const int MOUSEEVENT_LEFTDOWN = 0x0002;
            private const int MOUSEEVENTF_LEFTUP = 0x0004;
    private void button2_Click(object sender, EventArgs e)
            {
                SetCursorPos(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text), 0, 0);
            }

这。

鼠标点击模拟桌面

最可能的问题是您在单个mouse_event调用中设置了MOUSEEVENT_LEFTDOWNMOUSEEVENTF_LEFTUP。尝试模拟使用MOUSEEVENT_LEFTDOWN设置的mouse_event调用按下按钮,然后使用MOUSEEVENTF_LEFTUP设置进行后续的额外mouse_event调用以完成单击操作(每次单击两次调用)。