你就快到了。问题是EventArgs
会给你这个位置相对于按钮在点击时。
如果你想要cursor位置而不是单击,您可以使用Cursor https://msdn.microsoft.com/en-us/library/system.windows.forms.cursor%28v=vs.110%29.aspx类来获取它的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
事件并将您的光标登录移动到其中。
The 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();
}
}