Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
Note that for non-relative mouse movement (i.e. if MOUSEEVENTF_ABSOLUTE is not specified as part of dwFlags), negative values for dx and dy are desirable. As such, the "uint" type specification for C# can be safely replaced with Int32.
//Use the values of this enum for the 'dwData' parameter
//to specify an X button when using MouseEventFlags.XDOWN or
//MouseEventFlags.XUP for the dwFlags parameter.
public enum MouseEventDataXButtons : uint
XBUTTON1 = 0x00000001,
XBUTTON2 = 0x00000002
This function does indeed return a value, just as the keybd_event API does.
If there is a real error, it will in fact return a value of false, or zero.
The dwExtraInfo appears to be a ULONG_PTR in C++ (IntPtr in VB.NET)
Declare Function apimouse_event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
FYI, Microsoft tells us for "Windows NT/2000/XP: This function has been superseded. Use SendInput instead."
The C# code below works fine. However you have to keep in mind to add the namespace "System.Runtime.InteropServices" and keep in mind to write the code into a class.
Original contributor tells us:aa
I wanted to emulate the scroll. Searching for this information wasn't easy ... but here is how you do the mouse scroll button
The scroll value can actually be any value larger than 1, if used within a loop.
This allows you to smoothly increment the scrollbar, instead of relying on the inconsistent wheel delta,
which is a variable limited by the user, for mouse wheel. Start-Control Panel-Mouse-Wheel-Scrolling
VB.NET Sample Code:
Const MOUSEEVENTF_WHEEL As Int32 = 2048
Const MOUSEEVENTF_WHEEL_DELTA As Int32 = 120
Private Declare Function apimouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
Private Declare Function apiGetMessageExtraInfo Lib "user32" Alias "GetMessageExtraInfo" () As Int32
Private Sub PlayScroll(ByVal number As Int32, Optional ByVal increment As Int32 = 2)
On Error Resume Next
For i As Int32 = 1 To number
apimouse_event(MOUSEEVENTF_WHEEL, 0, 0, increment, apiGetMessageExtraInfo)
C# Sample Code:
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
This code assumes a form called frmMain with a command button called cmdClick a picture box called picClicker and a text box called txtResults
Note Twips are no more. Also, I stripped the FOR loop of delta moves from the command button click to the middle of the picture box.
Option Explicit On
Friend Class frmMain
Declare Auto Sub mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As IntPtr)
Const MOUSEEVENTF_MOVE As Int32 = &H1 ' mouse move
Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2 ' left button down
Const MOUSEEVENTF_LEFTUP As Int32 = &H4 ' left button up
Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8 ' right button down
Const MOUSEEVENTF_RIGHTUP As Int32 = &H10 ' right button up
Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20 ' middle button down
Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40 ' middle button up
Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000 ' absolute move
Const MOUSEEVENTF_WHEEL As Int32 = &H800 ' wheel button rolled
' Simulate moving the mouse to the center of the
' PictureBox and clicking.
Private Sub cmdClick_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdClick.Click
Dim cur_x As Single
Dim cur_y As Single
Dim dest_x As Single
Dim dest_y As Single
' mouse_event moves in a coordinate system where
' (0, 0) is in the upper left corner and
' (65535,65535) is in the lower right corner.
' Get the current mouse coordinates and convert
' them into this new system.
cur_x = System.Windows.Forms.Cursor.Position.X * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
cur_y = System.Windows.Forms.Cursor.Position.Y * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
' Convert the coordinates of the center of the
' picClicker PictureBox into this new system.
Dim pt As Point = picClicker.PointToScreen(New Point(picClicker.ClientRectangle.Width / 2, picClicker.ClientRectangle.Height / 2))
Dim eMouse As New System.Windows.Forms.MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, pt.X, pt.Y, 0)
to work as desired. Although it did trigger mouse events in the form showing the movement and click action desired, it didn't move the mouse pointer on the screen nor did it trigger events in the picture box located at pt.X, pt.Y
Thus I have resigned myself to unmanaged code for now. I will post the SendInput version shortly as mouse_event has been deprecated by Bill in favor of SendInput.
As the original contributor noted:
You can use the System.Windows.Forms.Cursor.Position property to set the position of the mouse, if you would like.
You will note I have replaced the GetCursorPos calls in the original VB source with this as suggested in the MSDN URL I mention above.
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 11:00:13 AM - tsahi-220.127.116.11
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).