鼠标单击桌面上的模拟

时间:2015-09-17 18:19:39

标签: c# .net

我为鼠标点击模拟编写代码,但有一个问题。它不是每秒都会点击但光标正在移动。谁能帮我 ?这是一个代码:

[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);
            }

多数人。

1 个答案:

答案 0 :(得分:1)

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