Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I do virtual mouse click?
 

How can I do virtual mouse click?

Hi all,

I have to do a virtualclick on any position on window(I mean click by not use a mouse ). How can I do that in C#?

Thank a lot.

Nobita  Wednesday, May 10, 2006 9:30 AM

You can simulate a mouse click by calling the mouse_event API.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public void MouseClick()
{
int x = 100;
int y = 100;

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

Change the values of x and y to simulate a click elsewhere on th screen

Sven De Bont  Wednesday, May 10, 2006 11:23 AM

You can simulate a mouse click by calling the mouse_event API.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public void MouseClick()
{
int x = 100;
int y = 100;

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

Change the values of x and y to simulate a click elsewhere on th screen

Sven De Bont  Wednesday, May 10, 2006 11:23 AM

Thanks a lot De Bont,

This code is veryhelpful .

Nobita  Thursday, May 11, 2006 2:15 AM

Nobita,

Can you mark the reply as an answer please. I makes it easier for others to find answers that way.

Sven De Bont  Thursday, May 11, 2006 5:59 AM

Hi De Bont,

I throught I marked it if it haven't been marked yet, please suggest me how to mark that reply.

Nobita  Friday, May 19, 2006 2:42 AM
Has anyone got this code in VB? VB Express 2005 prefreably but any will do
Alan Churley  Monday, August 07, 2006 10:09 PM
I've never used VB.NET (just VB6), but try this:

Declare Auto Function mouse_event Lib "user32.dll" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer) As Integer

Const MOUSEEVENTF_LEFTDOWN as Integer = 2
Const MOUSEEVENTF_LEFTUP as Integer = 4
Const MOUSEEVENTF_RIGHTDOWN as Integer = 8
Const MOUSEEVENTF_RIGHTUP as Integer = 16

Public Shared Sub MouseClick()
{
Dim x As Integer = 100
Dim y As Integer = 100;

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
}


sirjis  Tuesday, August 08, 2006 12:26 AM

My interest is to send a click event to a specific control, a Button for an example ^^

I'm pretty sure it's easy because it is in any other GUI API I used, but I have to admit Windows.Form assembly disappoint me sometime...

Anyway I'll probably find the solution alone and thanks a lot for the tip to simulate a click anywhere in the screen !!!

sebus  Wednesday, September 13, 2006 5:47 PM
To click button programmatically use PerformClick() method:
myButton.PerformClick();
boban.s  Wednesday, September 13, 2006 7:39 PM
Hi,

I tried to find the PerformClick method in the System.Windows.Forms.Button object but with no success.
This method probably exist in the .NET framework 2.0 version of the Button Class but not in the compact framework one.
Thanks anyway for the tip !

See you boban
sebus  Thursday, January 11, 2007 3:15 PM

Hi Sven de Bond,

Cane you telle me how to do a virtual mouse click in Visual Basic?

nicknehoux@hotmail.com

dehoux  Friday, May 18, 2007 2:58 PM
I answered this question already, if you would read above. If you already saw this, please let us know specifically what part you're having trouble with.
sirjis  Tuesday, May 22, 2007 2:12 AM

Sirjis,

The example you posted is a mix between VB.NET & C# code and will not work.

Try this in VB.NET

PublicConstMOUSEEVENTF_LEFTDOWNAsInteger=2

PublicConstMOUSEEVENTF_LEFTUPAsInteger=4

PublicConstMOUSEEVENTF_RIGHTDOWNAsInteger=8

PublicConstMOUSEEVENTF_RIGHTUPAsInteger=16

PublicDeclareSubmouse_eventLib"user32.dll"(ByValdwFlagsAsInteger,ByValdxAsInteger,ByValdyAsInteger,ByValcButtonsAsInteger,ByValdwExtraInfoAsInteger)

PublicSubMouseClick()
DimxAsInteger=100
DimyAsInteger=100
mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,0,0)
mouse_event(MOUSEEVENTF_LEFTUP,x,y,
0,0)
EndSub

Sven De Bont  Tuesday, May 22, 2007 12:13 PM

How do you use this? I think I know how to add it but how do you assign it to like a button press?
WeldFire  Tuesday, January 15, 2008 1:14 PM

You give the MouseClick method a screen coordinate that will hit the button you are interested.

JRQ  Tuesday, January 15, 2008 10:36 PM

You can use google to search for other answers

Custom Search

More Threads

• Rotating Pictures
• Richtextbox, undo issue.
• C# Datagridview removing mutiple rows at the same time
• Using custom Event Arguments with a System.EventHandler
• ListBox Woes???
• Setting position of text cursor in AxSHDocVw.AxWebBrowser Control
• How to Find all Control in Form ?
• HOW CAN I DESIGN MY OWN TOOLBOX CONTROL
• Hiding a tab in a Tab Control
• OWC11 (pivottable) & Drag and Drop = comexception error...