Kyle
Kyle

Reputation: 151

Getting a cursor position value and calling it later

I am stumped as to how I would go about storing the screen/cursor position and calling it back to the exact pixel after it has finished completing the rest of the function.

I am able to run my code when I click a button, but I want to be able to return the cursor to the position it was in when the button was clicked, so I can loop it from that exact location.

Any help would be greatly appreciated

I am writing in C# and using VS2010

Upvotes: 0

Views: 1009

Answers (2)

oxilumin
oxilumin

Reputation: 4833

In case of Windows Forms (if you are able to use System.Windows.Forms assembly) you can use Position property from Cursor class. It allows you to get and set cursor position. But is's a bad practice to move cursor from your code.

using System.Windows.Forms;

namespace MyApplication
{
    class MyClass
    {
        void Go()
        {
            var previousPosition = Cursor.Position;

            // Do smth

            Cursor.Position = previousPosition;
        }
    }
}

Upvotes: 1

bendewey
bendewey

Reputation: 40235

It's bad user experience to modify the users cursor, that is why the mouse cursor position is generally exposed as Read-Only information.

Upvotes: 1

Related Questions