mouse_event (user32)
C# Signature:
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
UIntPtr dwExtraInfo);
or
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
int dwExtraInfo);
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.
User-Defined Types:
[Flags]
public enum MouseEventFlags : uint
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010,
WHEEL = 0x00000800,
XDOWN = 0x00000080,
XUP = 0x00000100
}
//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
}
VB.Net Signature:
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
Notes:
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
dim ScrollValue as Integer
ScrollValue = 120 'or -120 for up or down scrolling
mouse_event(&H800, 0, 0, ScrollValue, 0)
Tips & Tricks:
Another contributor tells us:
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)
Next
End Sub
C# Sample Code:
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public static void LeftClick(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y);
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
VB.NET 2005 Sample Code:
Based on http://vb-helper.com/howto_move_click_mouse.html
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
Inherits System.Windows.Forms.Form
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))
dest_x = pt.X * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
dest_y = pt.Y * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
txtResults.Text = txtResults.Text & "From " & System.Windows.Forms.Cursor.Position.X & " " & System.Windows.Forms.Cursor.Position.Y & " to " & pt.X & " " & pt.Y & vbCrLf
txtResults.Text = txtResults.Text & "From " & cur_x & " " & cur_y & " to " & dest_x & " " & dest_y & vbCrLf
' Move the mouse to its final destination and click it.
mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, dest_x, dest_y, 0, 0)
End Sub
Private Sub picClicker_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles picClicker.Click
txtResults.Text = txtResults.Text & "MouseClick" & vbCrLf
End Sub
Private Sub picClicker_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picClicker.MouseDown
txtResults.Text = txtResults.Text & "MouseDown (" & e.X & ", " & e.Y & ")" & vbCrLf
End Sub
Private Sub picClicker_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picClicker.MouseUp
txtResults.Text = txtResults.Text & "MouseUp (" & e.X & ", " & e.Y & ")" & vbCrLf
End Sub
End Class
Alternative Managed API:
Per http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/win32map.asp , mouse_event is supposed to be replaced by .NET Framework API System.Windows.Forms.MouseEventArgs but this is not the case. MouseEventArgs is just a data structure w/out event capability. I tried to get:
Dim eMouse As New System.Windows.Forms.MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, pt.X, pt.Y, 0)
Me.OnMouseDown(eMouse)
Me.OnMouseMove(eMouse)
Me.OnMouseUp(eMouse)
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 ofSendInput.
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.
'Programming > C#' 카테고리의 다른 글
좌표에서 RGB값 받아오기 (0) | 2012.05.24 |
---|---|
how to get the RGB value of a pixel inside the Form workspace (0) | 2012.05.24 |
keybd_event (user32) (0) | 2012.05.21 |
C#과 C++ DLL 간의 스트링 데이타 교환 (0) | 2012.05.14 |
C# String을 C++ char로 변환 (0) | 2012.05.14 |