How to simulate mouse events (Windows Forms .NET)

Simulating mouse events in Windows Forms isn't as straight forward as simulating keyboard events. Windows Forms doesn't provide a helper class to move the mouse and invoke mouse-click actions. The only option for controlling the mouse is to use native Windows methods. If you're working with a custom control or a form, you can simulate a mouse event, but you can't directly control the mouse.

Events

Most events have a corresponding method that invokes them, named in the pattern of On followed by EventName, such as OnMouseMove. This option is only possible within custom controls or forms, because these methods are protected and can't be accessed from outside the context of the control or form. The disadvantage to using a method such as OnMouseMove is that it doesn't actually control the mouse or interact with the control, it simply raises the associated event. For example, if you wanted to simulate hovering over an item in a ListBox, OnMouseMove and the ListBox doesn't visually react with a highlighted item under the cursor.

These protected methods are available to simulate mouse events.

  • OnMouseDown
  • OnMouseEnter
  • OnMouseHover
  • OnMouseLeave
  • OnMouseMove
  • OnMouseUp
  • OnMouseWheel
  • OnMouseClick
  • OnMouseDoubleClick

For more information about these events, see Using mouse events (Windows Forms .NET)

Invoke a click

Considering most controls do something when clicked, like a button calling user code, or checkbox change its checked state, Windows Forms provides an easy way to trigger the click. Some controls, such as a combobox, don't do anything special when clicked and simulating a click has no effect on the control.

PerformClick

The System.Windows.Forms.IButtonControl interface provides the PerformClick method which simulates a click on the control. Both the System.Windows.Forms.Button and System.Windows.Forms.LinkLabel controls implement this interface.

button1.PerformClick();
Button1.PerformClick()

InvokeClick

With a form a custom control, use the InvokeOnClick method to simulate a mouse click. This is a protected method that can only be called from within the form or a derived custom control.

For example, the following code clicks a checkbox from button1.

private void button1_Click(object sender, EventArgs e)
{
    InvokeOnClick(checkBox1, EventArgs.Empty);
}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    InvokeOnClick(CheckBox1, EventArgs.Empty)
End Sub

Use native Windows methods

Windows provides methods you can call to simulate mouse movements and clicks such as User32.dll SendInput and User32.dll SetCursorPos. The following example moves the mouse cursor to the center of a control:

[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetCursorPos(int x, int y);

private void button1_Click(object sender, EventArgs e)
{
    Point position = PointToScreen(checkBox1.Location) + new Size(checkBox1.Width / 2, checkBox1.Height / 2);
    SetCursorPos(position.X, position.Y);
}
<Runtime.InteropServices.DllImport("USER32.DLL", EntryPoint:="SetCursorPos")>
Public Shared Function SetCursorPos(x As Integer, y As Integer) As Boolean : End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim position As Point = PointToScreen(CheckBox1.Location) + New Size(CheckBox1.Width / 2, CheckBox1.Height / 2)
    SetCursorPos(position.X, position.Y)
End Sub

See also