4

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Eli
  • 4,576
  • 1
  • 27
  • 39
  • 2
    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 Jul 07 '14 at 14:41
  • [Click](http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx) ? – Sinatr Jul 07 '14 at 14:55
  • @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 Jul 08 '14 at 06:41
  • @Sinatr Already been at that page. Sorry, its not seem to be helpful – Eli Jul 08 '14 at 06:42
  • 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](http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx) way. See also [this](http://stackoverflow.com/q/8185916/1997232) and perhaps [this](http://stackoverflow.com/q/14063542/1997232) – Sinatr Jul 08 '14 at 07:20
  • @Sinatr Every method is working. But - I cannot actually see the cursor, only moving symptoms (like rising tool tip somewhere) – Eli Jul 08 '14 at 10:07

1 Answers1

4

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

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • sorry it does making the mouse moving - but the cursor actually can't be seen – Eli Jul 08 '14 at 12:17
  • 1
    Did you try to press cursor keys? My mouse pointer is moving pretty well.. all around other windows. Or what do you mean as `cursor can't be seen`? You may have to add screenshots to your question, as you can see peoples (me?) having problem to understand what you want. – Sinatr Jul 08 '14 at 12:25
  • Well.. it does working and its very nice one - but still, I'm just seeing tool tips rising up because some ghost cursor – Eli Jul 08 '14 at 13:09