简体   繁体   English

单击鼠标获取鼠标坐标

[英]Getting mouse coordinates on mouse click

I'm using this code below but it doesn't work like I want it and I have no idea how to actually make it.我在下面使用这段代码,但它不能像我想要的那样工作,而且我不知道如何实际制作它。

What I want it to do is get the mouse coordinates onClick , but this happens after the user confirm a messagebox.我想要它做的是获取鼠标坐标onClick ,但这是在用户确认消息框之后发生的。

MessageBox > User Click OK > User Click anywhere on screen > Get the coordinates MessageBox > 用户单击确定 > 用户单击屏幕上的任意位置 > 获取坐标

Should I start a timer at the "OK button"?我应该在“确定按钮”处启动计时器吗? What I do on timer code to wait for a mouse response?我在定时器代码上做了什么来等待鼠标响应?

This is what I have now (which shows the mouse position when I click OK button) :这就是我现在所拥有的(当我单击“确定”按钮时显示鼠标 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
        MouseEventArgs me = (MouseEventArgs)e;
        Point coordinates = me.Location;
        MessageBox.Show("Coordinates are: " + coordinates);
    }
}

You were almost there. 你快到了 The problem is that the EventArgs will give you the position relative to the button at the time of the click. 问题在于, EventArgs将在单击时为您提供相对于按钮的位置。

If you want the cursor position instead of the click, you can use the Cursor class to get its Position property: 如果希望光标位置而不是单击位置,则可以使用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);
    }
}

To get the coordinates after the user closed the MessageBox , you can use a timer. 要在用户关闭MessageBox之后获取坐标,可以使用计时器。 In order to do so, you will have to declare one at the class level, set its Tick event and move your cursor login into it. 为此,您将必须在类级别声明一个,设置其Tick事件并将光标登录移入该事件。

The button12_Click method will now start the timer, which will show the cursor position once it expires (In this example, after one second). 现在, 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();
    }
}

Cursor Position relative to the Screen 相对于屏幕的光标位置

System.Windows.Forms.Cursor.Position

Cursor Position relative to a control 相对于控件的光标位置

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

Global hooks are not supported in the .NET Framework. .NET Framework不支持全局挂钩。 See Reference 请参阅参考

If you want to handle global mouse click events have a look at this article. 如果要处理全局鼠标单击事件,请查看本文。

Processing Global Mouse and Keyboard Hooks in C# 在C#中处理全局鼠标和键盘挂钩

You have probably defined the click event for the wrong object. If you use the click event for a panel, you will get the mouse coordinates relative to the upper-left corner of that panel.您可能为错误的 object 定义了点击事件。如果您对面板使用点击事件,您将获得相对于该面板左上角的鼠标坐标。 If you use the click event for another form control, you will get the mouse coordinates relative to the upper-left corner of that form control.如果您对另一个表单控件使用单击事件,您将获得相对于该表单控件左上角的鼠标坐标。 The below code will give the mouse coordinates relative to the upper-left corner of formobject1, whatever it is.下面的代码将给出相对于 formobject1 左上角的鼠标坐标,无论它是什么。

            formobject1.Click += (sender, e) => { 
            MouseEventArgs e1 = (MouseEventArgs)e;
            System.Drawing.Point cc = e1.Location;
            MessageBox.Show(cc.ToString());
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM