【问题标题】:Getting mouse coordinates on mouse click在鼠标点击时获取鼠标坐标
【发布时间】:2015-07-31 04:03:59
【问题描述】:

我在下面使用这段代码,但它不能像我想要的那样工作,我不知道如何实际制作它。

我想要它做的是获取鼠标坐标onClick,但这发生在用户确认消息框之后。

MessageBox > 用户单击确定 > 用户单击屏幕上的任意位置 > 获取坐标

我应该在“确定按钮”处启动计时器吗?我在计时器代码上做什么以等待鼠标响应?

这就是我现在所拥有的(当我单击“确定”按钮时显示鼠标位置)

private void button12_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
    {
        // user clicked ok
        MouseEventArgs me = (MouseEventArgs)e;
        Point coordinates = me.Location;
        MessageBox.Show("Coordinates are: " + coordinates);
    }
}

【问题讨论】:

    标签: c# mouse


    【解决方案1】:

    你快到了。问题是EventArgs 会在点击时为您提供相对于按钮的位置

    如果你想要 cursor 位置而不是点击,你可以使用Cursor 类来获取它的Position 属性:

    private void button12_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
        {
            // user clicked ok
            Point coordinates = Cursor.Position;
            MessageBox.Show("Coordinates are: " + coordinates);
        }
    }
    

    要在用户关闭MessageBox 后获取坐标,可以使用计时器。为此,您必须在类级别声明一个,设置其Tick 事件并将您的光标登录移动到其中。

    button12_Click 方法现在将启动计时器,一旦到期(在本例中为一秒后),它将显示光标位置。

    private Timer timer; //Declare the timer at class level
    public Form1()
    {
        InitializeComponent();
        // We set it to expire after one second, and link it to the method below
        timer = new Timer {Interval = 1000}; //Interval is the amount of time in millis before it fires
        timer.Tick += OnTick;
    }
    
    private void OnTick(object sender, EventArgs eventArgs)
    {
        timer.Stop(); //Don't forget to stop the timer, or it'll continue to tick
        Point coordinates = Cursor.Position;
        MessageBox.Show("Coordinates are: " + coordinates);
    }
    
    
    private void button1_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
        {
            timer.Start();
        }
    }
    

    【讨论】:

    • 我的表达可能不正确。但这得到了我单击 MessageBox 上的“确定”按钮的鼠标位置。单击确定按钮后,它不允许我选择屏幕的任何位置。
    • 但是一旦你点击确定按钮,你希望如何等待用户选择一个位置?你不能使用计时器,因为你不知道他什么时候准备好。但是,您可以将鼠标放在某处,然后使用 ENTER 关闭弹出窗口,因此基本上是“在按 OK 之前选择一个位置(使用 Enter)”。如果这做不到,您希望“立即获取鼠标位置”事件是什么?
    • 我同意,这可行。所以我想要的不可能吗?因为我见过一些有这个功能的软件。正如我所说,我不知道怎么做。也许确定按钮启动一个计时器并在计时器内创建 onClick 函数?身份证
    • @GabrielDeFreitas 如果有帮助,我将添加一个带有计时器的示例。
    • @GabrielDeFreitas 如果您想在第二次点击后获得位置,屏幕上的任何位置,您需要使用鼠标挂钩,使用 Windows 的 DLL。可以找到一个例子here
    【解决方案2】:

    光标相对于屏幕的位置

    System.Windows.Forms.Cursor.Position
    

    光标相对于控件的位置

    var relativePoint = myControl.PointToClient(Cursor.Position);
    

    .NET Framework 不支持全局挂钩。参见Reference

    如果您想处理全局鼠标点击事件,请查看这篇文章。

    Processing Global Mouse and Keyboard Hooks in C#

    【讨论】:

    • @GabrielDeFreitas:“用户在您的应用程序或屏幕上的任何位置单击屏幕上的任何位置”?
    • 屏幕上的任何位置,而不是我的应用程序上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2013-03-20
    • 2015-02-10
    • 1970-01-01
    相关资源
    最近更新 更多