Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. 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 :
C#
[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);

        }
Posted
Updated 17-Sep-15 3:44am
v2
Comments
CHill60 17-Sep-15 9:49am    
I think you also need to position the cursor - static extern bool SetCursorPos(int x, int y);
dave_bulac 17-Sep-15 14:25pm    
thank it halped
Bernhard Hiller 17-Sep-15 11:08am    
Try a short Thread.Sleep between the calls (a few milliseconds).
Ralf Meier 17-Sep-15 11:33am    
Which control should "simulate" the MouseClick ?
The Textbox itself ? Or the Form ?
I would suggest that you call the corresponding On...-Method of the control which should do the action ...

1 solution

I would change

mouse_event(MOUSEEVENT_LEFTDOWN, x, y, 0, 0);


to

C#
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTFLEFTUP, x, y,0,0);


As per this; how to simulate a mouse click in c#[^]
 
Share this answer
 
Comments
dave_bulac 17-Sep-15 12:40pm    
I have tried that too but gives it isn't adding a Click
CHill60 17-Sep-15 14:00pm    
How are you checking for the Click? And did you try positioning the cursor first as per my comment?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900