c# - 如何从应用程序窗口获取/设置鼠标的相对位置?

标签 c# css-position cursor-position

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

我还想设置鼠标的位置。

这如何在 C# 中完成?我正在使用 Windows 窗体应用程序。

最佳答案

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

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

关于c# - 如何从应用程序窗口获取/设置鼠标的相对位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1728286/

相关文章:

css - 位置固定时隐藏在内容后面的网站标题

css - Android浏览器的位置: fixed and z-index issue

html - 具有相关内容的 Div 大小

javascript - 如何删除光标前的值 - React

c# - 如何通过捏合来缩放游戏对象?

c# - Server.MapPath 给出了错误的路径,在 IIS 服务器上运行时不支持异常 "The given path' s 格式?

c# - linq to entities 无法将 int 转换为字符串

c# - WPF 中的 Application.ProductName 等价物?

c - 为什么 XWarpPointer 只工作一次(在循环中)?

delphi - 如何在单击 TButton 时显示 TPopupMenu?