如何创建显示鼠标 C# 连续坐标的标签

How to create a label that show continuous coordinates of mouse C#

如何创建显示鼠标连续坐标的标签 C#

我测试了几个不同的选项,我觉得计时器滴答事件是持续更新标签的最佳选择。但是我做错了什么,我似乎无法让它工作。

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point position = Cursor.Position;
            position = Cursor.Position;
            int x = position.X;
            int y = position.Y;
            string str = x.ToString() + ":" + y.ToString();
            coords.Text = str;

        }

为什么不使用简单的 MouseMove 事件?! 假设您使用的是 WinForms,这个非常简单的代码显示了鼠标在窗体上的位置:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    label1.Text = $"{e.X:0},{e.Y:0}";
}

对于效率较低的定时器方法:

private void timer1_Tick(object sender, EventArgs e)
{
    label1.Text = $"{Cursor.Position.X:0},{Cursor.Position.Y:0}";
}

只需确保将 Timer.Enabled 设置为 True 即可。