【发布时间】:2014-10-30 21:34:29
【问题描述】:
第一次在这里提出问题,我在这里找到的解决方案似乎由于某种原因不起作用。当窗口变为活动状态时,我的应用程序需要设置鼠标位置,我已设置功能但无法使光标属性正常工作。由于某种原因,我不能使用 Cursor.Position 或任何东西。我曾希望访问聊天室以找到解决方案,但显然在我获得 20 声望之前我无法说话。
所以我在这里问如何用类似的东西改变光标位置
this.Cursor.SetPosition(x, y);
感谢您的帮助。
编辑:已经在here 进行了测试:
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
但编译器会抱怨 Current、Position、Clip、Location、Size
最终解决方案:
using System.Runtime.InteropServices;
...
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
...
Point relativePoint = MouseCaptureButton.TransformToAncestor(this)
.Transform(new Point(0, 0));
Point pt = new Point(relativePoint.X + MouseCaptureButton.ActualWidth / 2,
relativePoint.Y + MouseCaptureButton.ActualHeight / 2);
Point windowCenterPoint = pt;//new Point(125, 80);
Point centerPointRelativeToSCreen = this.PointToScreen(windowCenterPoint);
SetCursorPos((int)centerPointRelativeToSCreen.X, (int)centerPointRelativeToSCreen.Y);
【问题讨论】:
-
为什么忽略错误信息。编译器抱怨?阅读错误消息会重现它们。
-
示例:错误 1“System.Windows.Input.Cursor”不包含“Current”的定义,并且没有接受“System.Windows.Input”类型的第一个参数的扩展方法“Current”。可以找到光标'(您是否缺少 using 指令或程序集引用?)
-
它在 System.Windows.Forms 中。如文件所述。
-
无论如何,网络搜索都有帮助。试试这些搜索词:c# set cursor position wpf
-
我从未来来到这里是为了说这个 Stackoverflow 现在是谷歌搜索如何在 C# WPF 中移动鼠标的热门搜索。非常感谢有帮助的 cmets 向 Google 说出答案而不是提供答案。
标签: c# wpf cursor-position