win32gui.SetActiveWindow() ERROR : The specified procedure could not be found

前端 未结 2 1096
星月不相逢
星月不相逢 2020-12-03 22:31

I get the active window like so:

window = win32gui.GetForegroundWindow()

which is an Int, say 1053634. And afterwards I try to set the fore

相关标签:
2条回答
  • 2020-12-03 22:53

    This also worked for me

    import win32gui, win32com.client
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%')
    win32gui.SetForegroundWindow(window.hwnd)
    
    0 讨论(0)
  • 2020-12-03 23:15

    My program works fine on my desktop with Windows 7, but when I use my laptop with Windows Vista (even with UAC off), I get the error:

    pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
    

    The program will flash in the taskbar, but no characters are sent.

    I even tried sending 'notepad' SetForegroundWindow and get the same error.

    Here is a link with a workaround that combines threads to get the computer to think they work together: http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/

    This article has more information about the problem.

    UPDATE: I'm sorry that link goes to a C program. I researched some more and found out it will let you SetForegroundWindow, if you send an alt key first.

    For example:

    import win32gui, win32com.client
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%')
    win32gui.SetForegroundWindow(window)
    
    0 讨论(0)
提交回复
热议问题