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

Related searches

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.

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.

Console.SetCursorPosition(Int32, Int32) Method (System), 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� Console.SetCursorPosition () Method in C#. Console.SetCursorPosition (Int32, Int32) Method is used to set the position of cursor. Basically, it specifies where the next write operation will begin in the console window. The window origin changes automatically to make the cursor visible if the specified cursor position is outside the area that is currently visible in the console window.

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

Console.SetCursorPosition() Method in C#, Sets the position of the cursor. public: static void SetCursorPosition(int left, int top );. C# Copy. If the cursor is at the bottom right character position of the console window, the next write operation causes the console window to scroll so the cursor remains visible. If you want to write a character to the bottom right character position without causing the console window to scroll, use the MoveBufferArea method to move a character to that position.

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.

Obtaining the Mouse Pointer Screen Co-ordinates, SetCursorPosition(Int32, Int32) Method is used to set the position of cursor. C# Program to illustrate. // Console.CursorPosition() method. System.Windows.Forms.Cursor.Position.X = control.PointToScreen(coordinate).X; C# does a pretty good job of hiding the difference between value types and reference types. But it's a leaky abstraction. When you use the Position property, you get a copy of the value, not a reference to the value. Assigning the X property of that copy won't have an effect, you'll just update the copy, not the actual cursor position.

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

Cursors In C, In this article we'll create a simple C# program that tracks the position of the mouse pointer. We'll use a console application to show that no� Use Console.SetCursorPosition (which you mentioned you didn't want to do). Make a custom control or other mechanism to display the data, which will write the data as you desire. The Console class does not provide any type of "reverse line feed" operation, other than manually controlling the cursor position. There is no "backwards " you can use.

A cursor in Windows is an icon that is displayed when you move a the Size cursors are loaded when you are resizing an application. Figure 1. Windows Forms allows us to change this cursor image using the The Position property of Cursor class is used to change the position Net Core Console App. Active Oldest Votes. This answer is useful. 7. This answer is not useful. Show activity on this post. 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

Private Sub MoveCursor() ' Set the Current cursor, move the cursor's Position, ' and set its clipping rectangle to the form. Me.Cursor = New Cursor(Cursor.Current.Handle) Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50) Cursor.Clip = New Rectangle(Me.Location, Me.Size) End Sub Remarks

The position of the mouse pointer is calculated relative to the specified element with the upper-left corner of element being the point of origin, 0,0. During drag-and-drop operations, the position of the mouse cannot be reliably determined through GetPosition.

Comments
  • 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.
  • @Cody in fairness there are very few times you should control the mouse - it would be odd even if it was a windows app :)
  • 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#
  • 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...)
  • Oh, I see. EDIT: C# is not finding System.Windows.Forms.
  • @Cody Well, I was taking for granted that anyone using something in System.Windows.Forms would add a reference to it!
  • 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...
  • Add a reference first, from file browser window, then you can put using System.Windows.Forms in your main class