如何获取当前屏幕光标位置?

Kel*_*lly 5 c#

我的目标是在屏幕上获取当前位置(在表格的外侧),然后按"C"保存X,Y坐标.

我谷歌并发现了一些使用api钩子的建议,但我想知道有没有办法可以完全用C#代码(.NET Lib)来完成这个任务?

如果可能的话请给我快速的样品,因为我是c#的新手.

谢谢

MPe*_*ier 9

只需使用:

Cursor.Position
Run Code Online (Sandbox Code Playgroud)

要么

Control.MousePosition
Run Code Online (Sandbox Code Playgroud)

获得这个位置.然后,您可以映射表单的KeyPress事件:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'c')
        MessageBox.Show(Cursor.Position.ToString());
}
Run Code Online (Sandbox Code Playgroud)

单个X和Y坐标是Position对象的两个属性.

文档:http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx