可以通过编程方式查看光标的移动

时间:2014-07-07 14:38:06

标签: c# cursor mouse console-application

好的,在做了一些研究并尝试了很多例子之后 -

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

并且变得沮丧我要求你们帮忙。

我正在尝试以编程方式移动光标(在控制台应用程序中,c#)。

- > 当我读到改变的位置时似乎很好 - 但我实际上看不出其中的差别。光标的图像保持不变...

我希望实际看到光标位于更改位置

由于

编辑2:谢谢所有人的帮助,目前我只是继续工作而没有看到光标移动到任何地方......所以很难得到关于它的位置的任何反馈(只是猜测)。

1 个答案:

答案 0 :(得分:3)

我真的没有看到任何问题,这是一个测试代码,它对我有用(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);
        }
    }
}

积分归@Keith answer


这样做

enter image description here

相关问题