11

I tried [DllImport("user32.dll")] static extern bool SetCursorPos(int X, int Y);

and it works pretty fine to move the cursor to the desired point. I have never tried such kind of a DLL import before but it works :). However I want more what else can I extract? Mainly I want to make double click, click or use wheel options without any mouse input, just the code how can I do that? and how can I check what else is included in user32dll?

Thanx

albatross
  • 455
  • 2
  • 8
  • 27
  • 1
    A topic you might be interested in is [UI Automation in .NET](http://msdn.microsoft.com/en-us/library/ms753326(v=vs.110).aspx). -- Also, keep in mind, those X & Y positions are in Physical screen coordinates (which are the same as Logical for 96 DPI only -- but for other DPIs they're not -- most of the .NET Framework uses Logical coordinates for everything). – BrainSlugs83 Mar 22 '14 at 18:10

3 Answers3

17
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10;

You should Import and Define these Constant's to work with Mouse using Win32API

Use method's below to do Mouse Operation's

void sendMouseRightclick(Point p)
{
    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
}

void sendMouseDoubleClick(Point p)
{
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);

Thread.Sleep(150);

    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
}

void sendMouseRightDoubleClick(Point p)
{
    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);

    Thread.Sleep(150);

    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
}

void sendMouseDown()
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 50, 50, 0, 0);
}

void sendMouseUp()
{
    mouse_event(MOUSEEVENTF_LEFTUP, 50, 50, 0, 0);
}

If you want to do a Mouse Drag you should First Send MouseDown(Mouse Click) and keep it Clicked While Changing the Mouse Position than Send MouseUp(Release Click) something like this .

sendMouseDown();
Cursor.Position = new Point(30,30);
sendMouseUp();
Serdalis
  • 10,296
  • 2
  • 38
  • 58
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
  • 2
    Additional Information: A call to PInvoke function 'KinectHandTracking!KinectHandTracking.MainWindow::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. – albatross Jan 05 '12 at 08:59
  • 1
    by the way longs should be uint otherwise it gives an exception. – albatross Jan 05 '12 at 14:30
  • 1
    FYI, you shouldn't mix `Cursor.Position` (which works in logical coordinates) with `User32`'s `SetCursorPos` (which works in physical coordinates). Here's an MSDN article on Screen Scaling and UI Automation: http://msdn.microsoft.com/en-us/library/aa970067(v=vs.110).aspx – BrainSlugs83 Mar 22 '14 at 18:34
  • Also, `long` gives an exception because it's a 64-bit, `uint` works because it's 32-bit -- but it won't work for negative coordinates (which is a common monitor setup in Windows). Instead of using `long` or `uint`, you *should* instead be using the regular `int` type. – BrainSlugs83 Mar 22 '14 at 18:38
  • You should declare `mouse_event` with `int dwExtraInfo` otherwise your sample code will not compile. – GDavoli Apr 21 '19 at 03:37
7

Using long type raises an "PInvoke" error.

We should use int type:

[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);
}

source: http://www.pinvoke.net/default.aspx/user32.mouse_event

Behzad
  • 1,740
  • 1
  • 22
  • 49
3

Take a look at pinvoke.net for a listing of the available APIs. Code examples and the import statements are included. You can also expand the selection on the left to see the available APIs for each DLL.

Lukazoid
  • 19,016
  • 3
  • 62
  • 85