C# WPF 应用程序 .NET 4.5 设置鼠标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25694208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF application .NET 4.5 Set Mouse Position
提问by DeadJohnDoe
First time asking a question here, the solutions I found on here do not seem to work for some reason. My app needs to set the mouse position when the window becomes active, I have the function set up but cannot get cursor properties to work. I can't use Cursor.Position or anything really for some reason. I had hoped to visit the chat rooms to find a solution but apparently I cannot speak until I have 20 reputation.
第一次在这里提问,我在这里找到的解决方案由于某种原因似乎不起作用。我的应用程序需要在窗口变为活动状态时设置鼠标位置,我设置了该功能但无法使光标属性起作用。出于某种原因,我真的不能使用 Cursor.Position 或任何东西。我曾希望访问聊天室以找到解决方案,但显然在我获得 20 声望之前我无法说话。
So here I am asking how to change the cursor position with something like
所以在这里我问如何用类似的东西改变光标位置
this.Cursor.SetPosition(x, y);
this.Cursor.SetPosition(x, y);
Thanks for the help.
谢谢您的帮助。
Edit: Tried this already as a test from 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);
}
but the compiler complains about Current, Position, Clip, Location, Size
但是编译器会抱怨 Current、Position、Clip、Location、Size
Final solution:
最终解决方案:
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);
回答by B.K.
You can use InteropServicesto accomplish this fairly easily:
您可以使用InteropServices它来轻松完成此操作:
// Quick and dirty sample...
public partial class MainWindow : Window
{
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
public MainWindow()
{
InitializeComponent();
SetCursorPos(100, 100);
}
}
Just make sure you include System.Runtime.InteropServicesnamespace. There are also a lot of other ways, such as the one pointed out in the duplicate link above. Use what's best for you.
只要确保包含System.Runtime.InteropServices命名空间即可。还有很多其他的方法,比如上面重复链接中指出的那个。使用最适合您的东西。
EDIT:
编辑:
Per request in the comment, here's one way to make it an application window coordinate system, rather than a global one:
根据评论中的每个请求,这里有一种方法可以使其成为应用程序窗口坐标系,而不是全局坐标系:
public partial class MainWindow : Window
{
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SetCursor(200, 200);
}
private static void SetCursor(int x, int y)
{
// Left boundary
var xL = (int)App.Current.MainWindow.Left;
// Top boundary
var yT = (int)App.Current.MainWindow.Top;
SetCursorPos(x + xL, y + yT);
}
}
I don't think you would do this... but just make sure that you don't try to get Windowcoordinates during the initialization phase (in the constructor). Wait until it's loaded, similarly to how I've done above; otherwise, you may get NaNfor some of the values.
我不认为你会这样做......但只要确保你不要Window在初始化阶段(在构造函数中)尝试获取坐标。等待它加载,类似于我上面所做的;否则,您可能会得到NaN某些值。
If you want to restrict it to the confinements of the window, an easy way would be to add System.Windows.Formsto your references and use the code provided in the duplicate link. However, if you want to use my method (all about personal preference... I use what I like... and I like PInvoke), you may check the xand ypositions in SetCursor(..)prior to passing them to SetCursorPos(..), similarly to this:
如果您想将其限制在窗口的范围内,一个简单的方法是添加System.Windows.Forms到您的引用中并使用重复链接中提供的代码。但是,如果您想使用我的方法(所有关于个人喜好......我使用我喜欢的......我喜欢的PInvoke),你可以在将它们传递给 之前检查x和y位置,类似于:SetCursor(..)SetCursorPos(..)
private static void SetCursor(int x, int y)
{
// Left boundary
var xL = (int)App.Current.MainWindow.Left;
// Right boundary
var xR = xL + (int)App.Current.MainWindow.Width;
// Top boundary
var yT = (int)App.Current.MainWindow.Top;
// Bottom boundary
var yB = yT + (int)App.Current.MainWindow.Height;
x += xL;
y += yT;
if (x < xL)
{
x = xL;
}
else if (x > xR)
{
x = xR;
}
if (y < yT)
{
y = yT;
}
else if (y > yB)
{
y = yB;
}
SetCursorPos(x, y);
}
Note that you may want to account for the border if you application uses Windows look and feel.
请注意,如果您的应用程序使用 Windows 外观,您可能需要考虑边框。

