以下代码执行以下操作
PushWindowToFront():
回调:
然而,它总是返回win32错误5 - “访问被拒绝”。为什么应用程序无法访问属于同一进程的窗口调用此函数?
public void PushWindowToFront()
{
currentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;
Win32Methods.EnumWindowsCallbackDelegate callback = new Win32Methods.EnumWindowsCallbackDelegate(this.EnumWindowsCallback);
Win32Methods.EnumWindows(callback, (IntPtr) 0);
}
public bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
{
uint i = 0;
Win32Methods.GetWindowThreadProcessId(hWnd, out i);
if (currentProcessId == i)
{
StringBuilder sb = new StringBuilder(512);
Win32Methods.GetWindowText(hWnd, sb, sb.Capacity);
if (sb.ToString().Split(' ')[0].ToLower().Equals("select"))
{
IntPtr result = Win32Methods.SetFocus(hWnd);
Console.WriteLine("Window found: {0}\r\nSetting focus...\r\nResult: {1}\r\nLastError: {2}",
sb.ToString(), result, Marshal.GetLastWin32Error().ToString());
}
}
return true;
}
答案 0 :(得分:2)
我收到了这个确切的情况;我的理解是“SetFocus”是罪魁祸首。我通过将“SetFocus”替换为“SetForegroundWindow”
来解决错误hwnd = win32gui.FindWindow(None, winName)
win32gui.SetForegroundWindow(hwnd)