在 WPF 中设置鼠标位置? [英] Set mouse position in WPF?
问题描述
我有一个带坐标的二维数组,我想让鼠标按照这些坐标在 WPF 应用程序中创建的特定模式移动.你能帮助我吗?我试过 Cursor 类,但它不起作用.显然我做错了什么.
I have a two dimensional array with coordinates and i want to make the mouse move with the specific pattern those coordinates create in a WPF application. Can you help me? I've tried the Cursor class but it will not work. Obviously I am doing something wrong.
private void SetPosition( int a, int b)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(a, b);
}
这是我使用的方法,a 和 b 来自数组.提前致谢!
This is the method i use the a and b are from the array. thanks in advance!
PS 该方法位于每秒触发 20 次的事件中.
PS that method is inside an event that fires 20 times a second.
推荐答案
我不完全确定在 WPF 中是否有更好的方法(您使用的代码似乎是针对 WinForms 的),但是使用SetCursorPos
上的平台调用似乎可以解决问题:
I'm not entirely sure if there is a better way to do it in WPF (It seems the code you are using is targeted at WinForms), but using Platform Invoke on SetCursorPos
seems to do the trick:
private void SetPosition(int a, int b)
{
SetCursorPos(a, b);
}
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
这篇关于在 WPF 中设置鼠标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!