How do I set the position of the mouse cursor from a Console app in C#?
Asked Answered
F

4

15

I've found many articles on how to set the mouse position in a C# windows forms project - I want to do this in a console application. How can I set the absolute mouse position from a C# windows console application?

Thanks!

Hint: it's not Console.setCursorPosition, that only sets the position of the text cursor in the console.

Fogel answered 16/7, 2011 at 8:30 Comment(3)
Why does your console application need to set the position of the Windows mouse cursor? This is quite an unusual situation to say the least.Underdone
@Cody in fairness there are very few times you should control the mouse - it would be odd even if it was a windows app :)Erminois
As you have the solution can you share it with more code? I'm trying to do the same thing but I'm not very experienced with C#Dibasic
E
5

Inside your console application, add a reference to System.Windows.Forms.dll and use the other techniques you've read about. The choice of console vs windows exe only impacts the PE header (and maybe the default code template, but you can hack that trivially); you can still use the full framework in a console exe.

The mouse you want to control is in windows, not the console.

Erminois answered 16/7, 2011 at 8:33 Comment(0)
S
19

This is an old thread, but for the sake of completion it can be done this way...

use System.Runtime.InteropServices;

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

then in method whatever position you wish e.g.

  SetCursorPos(500, 500);
Salenasalene answered 16/3, 2016 at 19:58 Comment(1)
For feedback, I tried it, works well.Incapable
E
5

Inside your console application, add a reference to System.Windows.Forms.dll and use the other techniques you've read about. The choice of console vs windows exe only impacts the PE header (and maybe the default code template, but you can hack that trivially); you can still use the full framework in a console exe.

The mouse you want to control is in windows, not the console.

Erminois answered 16/7, 2011 at 8:33 Comment(0)
H
5

Fixed little mistake in Chaz unswer:

using System.Runtime.InteropServices;


namespace ConsoleImageWorker
{
    public static class Mouse
    {

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

        public static void SetCursorPosition(int x, int y)
        {
            SetCursorPos(x, y);
        }
    }
}

After that in any class you can just call:

Mouse.SetCursorPosition(100, 100);
Homosexual answered 4/2, 2020 at 11:11 Comment(0)
M
3

You can simply assign to Cursor.Position.

However, in a console application you will need to add references to the WinForms assemblies because console application projects do not include references to WinForms by default.

You will need to add System.Windows.Forms and System.Drawing, the latter to gain access to the Point class.

Masterstroke answered 16/7, 2011 at 8:32 Comment(7)
Note that this will only work if you first add a reference to the System.Windows.Forms libraries to your Console app. (Then again, Console apps shouldn't be messing with the Windows cursor in the first place...)Underdone
Oh, I see. EDIT: C# is not finding System.Windows.Forms.Fogel
@Cody Well, I was taking for granted that anyone using something in System.Windows.Forms would add a reference to it!Masterstroke
The question specifically started out by saying that he had found many articles about how to do this from a WinForms app, but didn't know how to do it from a Console app. I've learned not to take anything for granted around here...Underdone
Add a reference first, from file browser window, then you can put using System.Windows.Forms in your main classJaniuszck
Thanks all, got it figured out.Fogel
@guy Sorry, I misunderstood what you were stuck on.Masterstroke

© 2022 - 2024 — McMap. All rights reserved.