模拟鼠标双击不起作用

时间:2015-09-17 13:31:21

标签: c# .net

我正在使用Windows窗体处理我的项目,需要模拟鼠标单击。我从文本框中获取坐标,按下按钮后必须进行双击,但不幸的是我没有点击。谁能说出原因?这是一个代码:

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

        private const int MOUSEEVENT_LEFTDOWN = 0x0002;
        private const int MOUSEEVENTF_LEFTUP = 0x0004;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;
        private const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        private void button2_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBox1.Text);
            int y = Convert.ToInt32(textBox2.Text);
                mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);

        }

1 个答案:

答案 0 :(得分:0)

根据This MSDN discussion,您可能需要在点击之间等待。

App.Current.Dispatcher.BeginInvoke(action);