【问题标题】:How to get/set the position of the mouse relative from the application window?如何从应用程序窗口获取/设置鼠标的相对位置?
【发布时间】:2009-11-13 10:01:51
【问题描述】:

我希望能够获得鼠标指针的当前位置,相对于我的表单窗口所在的位置。因此,如果我将鼠标放在表单的左上角,它会给我 x=0 和 y=0 的 x 和 y 值,而表单本身可能位于屏幕中间。

我还想设置鼠标的位置。

如何在 C# 中做到这一点?我正在使用 Windows 窗体应用程序。

【问题讨论】:

    标签: c# css-position cursor-position


    【解决方案1】:

    使用Cursor.Position获取屏幕坐标,然后在窗口上调用PointToClient转换为窗口坐标。

    Point p = this.PointToClient(Cursor.Position);
    

    【讨论】:

    • 这工作得很好,虽然我希望已经有一个内置函数可以做到这一点
    【解决方案2】:

    查看Cursor.Position 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多