我在C#中遇到了一个SetCursorPos的问题,SetCursorPos在SetForegroundWindow之后不工作了。它返回错误2。
代码的一部分:
Rect patrat = new Rect();
GetWindowRect(parinte, ref patrat);
Console.WriteLine(SetForegroundWindow(parinte));
Thread.Sleep(1000);
Console.WriteLine(SetCursorPos(patrat.Left + 10, patrat.Top + 20));但是如果我把SetCursorPos放在SetForegroundWindow之前,它就会起作用
这是方法的导入
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}编辑:我发现某个程序在前台SetCursorPos拒绝改变光标,我该怎么办?
发布于 2018-06-19 08:35:08
以管理员身份运行Visual Studio应该可以解决您的问题。
SetForegroundWindow为设置前景窗口的线程分配的优先级比为前景窗口分配的优先级略高。因此,您需要使用比前台窗口更高的权限来运行程序。
https://stackoverflow.com/questions/26624503
复制相似问题