2

I am working on my project in Windows Form and need to simulate a Mouse Click. I get coordinates from textbox and after pressing button it must make Double Click but unfortunately I it isn't making Click. Can anyone say the reason ? Here is a code :

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

        }
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
dave_bulac
  • 45
  • 2
  • 8

1 Answers1

0

Based on This MSDN discussion you might have to wait between the clicks.

[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);
    Thread.Sleep(50);
    mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97