How to Set mouse cursor position to a specified point on screen in C#?
Asked Answered
C

3

10

How to Set mouse cursor position to a specified point on screen in C#?

am i must hacke the motherboard buffer that receive the mouse and keyboard coordinates and presses ???

is there another one to do clicks or i am imagine ???

Cephalonia answered 8/9, 2009 at 5:45 Comment(3)
The question that jumps immediately to mind is 'why'?Calva
to do something in my head. why?Cephalonia
@MatthewScharley I came here for "automatization"..Vicechancellor
K
7

Get and set mouse position in Windows 10:

Much simpler in c# .NET Framework 4.0 using Cursor.Position Property

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.position?view=netcore-3.1

    public static void ClickSomePoint()
    {

        // get mouse position
        System.Drawing.Point screenPos = System.Windows.Forms.Cursor.Position;

        // create X,Y point (0,0) explicitly with System.Drawing 
        System.Drawing.Point leftTop = new System.Drawing.Point(0,0);

        // set mouse position
        Cursor.Position = leftTop; 
        Console.WriteLine(screenPos);
    }
Keary answered 7/6, 2020 at 15:0 Comment(0)
T
10

The following will set the mouse position and perform a click:

public static void ClickSomePoint() {
    // Set the cursor position
    System.Windows.Forms.Cursor.Position = new Point(20, 35);

    DoClickMouse(0x2); // Left mouse button down
    DoClickMouse(0x4); // Left mouse button up
}   

static void DoClickMouse(int mouseButton) {
    var input = new INPUT() {
        dwType = 0, // Mouse input
        mi = new MOUSEINPUT() { dwFlags = mouseButton }
    };

    if (SendInput(1, input, Marshal.SizeOf(input)) == 0) { 
        throw new Exception();
    }
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT {
    int dx;
    int dy;
    int mouseData;
    public int dwFlags;
    int time;
    IntPtr dwExtraInfo;
}   
struct INPUT {
    public uint dwType;
    public MOUSEINPUT mi;
}   
[DllImport("user32.dll", SetLastError=true)]
static extern uint SendInput(uint cInputs, INPUT input, int size);

Just keep in mind that this could be extremely annoying for a user.

:)


If you want to click a button on your form you can use the 'PerformClick()' method.

Tranche answered 8/9, 2009 at 5:52 Comment(4)
but if i want to click anything in the screen is there something for that or my imagination will end here ?Cephalonia
the Exception in the if scope in every time happen. what i could do ?Cephalonia
Keep in mind that the second parameter of SendInput should be ref, otherwise the signatures won't match and you'll get a PInvoke exception!Partida
I guess that won't work on any systems except MS Widows, right?Jenisejenkel
K
7

Get and set mouse position in Windows 10:

Much simpler in c# .NET Framework 4.0 using Cursor.Position Property

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.position?view=netcore-3.1

    public static void ClickSomePoint()
    {

        // get mouse position
        System.Drawing.Point screenPos = System.Windows.Forms.Cursor.Position;

        // create X,Y point (0,0) explicitly with System.Drawing 
        System.Drawing.Point leftTop = new System.Drawing.Point(0,0);

        // set mouse position
        Cursor.Position = leftTop; 
        Console.WriteLine(screenPos);
    }
Keary answered 7/6, 2020 at 15:0 Comment(0)
S
1

The easiest way to position the mouse:

Left zero for left position and zero right for top position (you can change it to any number you want)(to use this code you need to use using System.Drawing and using System.Windows.Forms namespace)

Cursor.Position = new Point(0,0);

Semifluid answered 16/1, 2022 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.