【问题标题】:Can't see the cursor's movement programmatically无法以编程方式查看光标的移动
【发布时间】:2014-08-28 02:10:45
【问题描述】:

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

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:谢谢大家的帮助,目前我只是继续工作,没有看到光标在任何地方移动......所以很难得到任何关于它的位置的反馈(只是猜测)。

【问题讨论】:

  • 您实际上是在尝试移动鼠标光标(这对于控制台应用程序似乎没有意义)还是您在尝试操作控制台内的文本光标?
  • Click ?
  • @JustinNiessner 我想创建一个程序,使鼠标光标在我的 Win-OS 中的不同窗口和按钮中移动和单击。
  • @Sinatr 已经在那个页面了。抱歉,它似乎没有帮助
  • 从您之前的评论中可以清楚地看出您需要以编程方式设置鼠标位置(不是cursor,而不是move ,这将引导您进入其他主题)。在 winforms 中,您可以使用 this 方式进行操作。另请参阅thisthis

标签: c# console-application mouse-cursor


【解决方案1】:

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


这样做

【讨论】:

  • 抱歉,它确实使鼠标移动 - 但实际上看不到光标
  • 您是否尝试按光标键?我的鼠标指针移动得很好......在其他窗口周围。或者cursor can't be seen 是什么意思?您可能需要为您的问题添加屏幕截图,因为您可以看到人们(我?)在理解您想要的内容时遇到问题。
  • 嗯.. 它确实有效,而且非常好 - 但是,我只是看到工具提示上升,因为一些幽灵光标
猜你喜欢
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多