DotNetBrowser provides an ability to simulate mouse move, wheel, and click events. Simulation is available in both lightweight and heavyweight rendering modes for WPF and Windows Forms.

DotNetBrowser v1.17 and higher versions provide 2 possible ways of simulating mouse events. Simulation through the BrowserView.InputSimulator and through the Browser instance. DotNetBrowser 1.16 and earlier versions support simulation through the BrowserView.InputSimulator only. Both approaches require for non zero sizes of loaded document. If mouse input coordinates are out of the loaded document's size, mouse events won't be fired.


If you want to use BrowserView.InputSimulator, you must create BrowserView instance.

C#

// WPF
Browser browser = BrowserFactory.Create();
WPFBrowserView browserView = new WPFBrowserView(browser); 

// WinForms
Browser browser = BrowserFactory.Create();
WinFormsBrowserView browserView = new WinFormsBrowserView(browser); 

VB.NET

' WPF
Dim browser As Browser = BrowserFactory.Create()
Dim browserView As WPFBrowserView = New WPFBrowserView(browser)

' WinForms
Dim browser As Browser = BrowserFactory.Create()
Dim browserView As WinFormsBrowserView = New WinFormsBrowserView(browser)


If you want to use Browser just create Browser instance.

C#

Browser browser = BrowserFactory.Create();
browser.SetSize(1024, 800);

VB.NET

Dim browser As Browser = BrowserFactory.Create()
browser.SetSize(1024, 800)


Note: Make sure you set up size correctly.


When to use BrowserView.InputSimulator and Browser implementation?
In general, there is no big difference between them. But using Browser gives you a possibility to simulate mouse input without creating BrowserView instance. It might be helpful for testing purposes.

Mouse move simulation example


To simulate mouse move you must call an appropriate method with coordinates you want to set a virtual mouse cursor.

C#

int x = 887;
int y = 47;

VB.NET

Dim x As Integer = 887
Dim y As Integer = 47


Using BrowserView.InputSimulator:

C#

browserView.InputSimulator.SimulateMouseMoveEvent(x, y);

VB.NET

browserView.InputSimulator.SimulateMouseMoveEvent(x, y)


Using Browser:

C#

browser.MouseMove(x, y);

VB.NET

browser.MouseMove(x, y)


Mouse click simulation example


The mouse click is a sequence of mouse down and mouse up events. To simulate mouse button click you must specify coordinates you want to set a virtual mouse cursor and the button you want to click.

C#

MouseButton mouseButton = MouseButton.Left;
int x = 887;
int y = 47;
int clickCount = 1;

VB.NET

Dim mouseButton As DotNetBrowser.MouseButton = DotNetBrowser.MouseButton.Left
Dim x As Integer = 887
Dim y As Integer = 47
Dim clickCount As Integer = 1


Using BrowserView.InputSimulator:

C#

browserView.InputSimulator.SimulateMouseButtonEvent(mouseButton,
    MouseButtonState.Pressed, clickCount, x, y);
browserView.InputSimulator.SimulateMouseButtonEvent(mouseButton,
    MouseButtonState.Released, clickCount, x, y);

VB.NET

browserView.InputSimulator.SimulateMouseButtonEvent(mouseButton,
       MouseButtonState.Pressed, clickCount, x, y)
browserView.InputSimulator.SimulateMouseButtonEvent(mouseButton,
       MouseButtonState.Released, clickCount, x, y)


Note: In DotNetBrowser 1.17 and higher versions DotNetBrowser.MouseButton enumeration can be passed as mouseButton parameter. 1.16 and earlier versions require for System.Windows.Input.MouseButton or  System.Windows.Forms.MouseButtons, depending on which framework is used.


Using Browser:

C#

MouseButtonParams mouseButtonParams = new MouseButtonParams(
    mouseButton, x, y, clickCount);
browser.MouseDown(mouseButtonParams);
browser.MouseUp(mouseButtonParams);

VB.NET

Dim mouseButtonParams As MouseButtonParams = New MouseButtonParams(
    mouseButton, x, y, clickCount)
browser.MouseDown(mouseButtonParams)
browser.MouseUp(mouseButtonParams)


Mouse wheel simulation example


To simulate mouse wheel event you must specify coordinates, offset and direction of the wheel rotation.

C#

int x = 887;
int y = 47;
int offset = -5;

VB.NET

Dim x As Integer = 887
Dim y As Integer = 47
Dim offset As Integer = -5


Note: Positive offset value will scroll up, negative will scroll down.


Using BrowserView.InputSimulator:

C#

bool isHorizontalScroll = false;
browserView.InputSimulator.SimulateMouseWheelEvent(x, y, offset, isHorizontalScroll);


VB.NET

Dim isHorizontalScroll As Boolean = False
browserView.InputSimulator.SimulateMouseWheelEvent(x, y, offset, isHorizontalScroll)



Using Browser:

C#

ScrollDirection scrollDirection = ScrollDirection.Vertical;
MouseWheelParams mouseWheelParams = new MouseWheelParams(x, y, offset, scrollDirection);
browser.MouseWheel(mouseWheelParams);

VB.NET

Dim scrollDirection As ScrollDirection = ScrollDirection.Vertical
Dim mouseWheelParams As MouseWheelParams = New MouseWheelParams(x, y, offset, scrollDirection)
browser.MouseWheel(mouseWheelParams)


Note: Calling the Browser.MouseWheel method will have no effect for the Browser that works in the heavyweight rendering mode until this Browser is shown in a visible BrowserView control. This restriction is not present if the Browser works in the lightweight rendering mode.