4
votes

OK there, after really doing some research and trying a lot of examples-

Moving mouse cursor programmatically

How to move mouse cursor using C#?

Getting mouse position in c#

Control the mouse cursor using C#

http://www.blackwasp.co.uk/MoveMousePointer.aspx

and becomes frustrated I'm asking you guys for help.

I'm trying to move the cursor, programmatically (in console application, c#).

--> As I read the altered location it seems fine - but I can't actually see the difference. The cursor's image staying the same place...

I want to actually see the cursor at it's altered location

Thanks

Edit 2: Thank you all guys for helping, currently I'm just keep working without seeing the cursor moving anywhere... so its hard to get any feedback about it's locations (just guessing).

1
Are you actually trying to move the mouse cursor (which doesn't seem to make sense for a console application) or are you trying to manipulate the text cursor inside the console? - Justin Niessner
@JustinNiessner I'm want to create a program that make the mouse cursor to move and click in different windows and buttons in my Win-OS. - Eli
@Sinatr Already been at that page. Sorry, its not seem to be helpful - Eli
From your previous comment it is clear what you need to set mouse position programmatically (not cursor, not move, this will lead you to very other topics). In winforms you do it this way. See also this and perhaps this - Sinatr

1 Answers

4
votes

I really don't see any problem, here is a test code, it works for me (win7_64):

class Program
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MousePoint
    {
        public int X;
        public int Y;
    }

    [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetCursorPos(int X, int Y);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetCursorPos(out MousePoint lpMousePoint);

    static void Main(string[] args)
    {
        ConsoleKey key;
        MousePoint point;
        while ((key = Console.ReadKey(true).Key) != ConsoleKey.Escape)
        {
            GetCursorPos(out point);
            if (key == ConsoleKey.LeftArrow)
                point.X -= 10;
            if (key == ConsoleKey.RightArrow)
                point.X += 10;
            if (key == ConsoleKey.UpArrow)
                point.Y -= 10;
            if (key == ConsoleKey.DownArrow)
                point.Y += 10;
            SetCursorPos(point.X, point.Y);
        }
    }
}

Credits goes to @Keith answer.


It does this

enter image description here