[Tutor] win32gui, SetForegroundWindow() and setting focus
sirgnip at gmail.com
Sun Mar 16 20:01:54 CET 2008
I'm looking to use the win32api and win32gui modules to do a bit of
Windows tinkering (win2k) and I've hit a snag.
I'd like to programmatically set which window has the focus. But
win32gui.SetForegroundWindow(hwnd) seems to be what I'm looking for,
but, it isn't working quite as I expect meaning I probably have a bad
SetForegroundWindow() works like a charm in simple cases from the
command line. However, when I enumerate all top level windows and try
to set each one as the foreground window in turn, it gets thru 1-2
windows out of the 8-10 I have open before giving me this:
Traceback (most recent call last):
File "D:\_code\python\apps\WinTests.py", line 25, in ?
File "D:\_code\python\apps\WinTests.py", line 20, in cycle_foreground
pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
What am I doing wrong in this case? Can my app "give away" its own
focus but can't force a different app's window with focus to "give
away" its focus? Is there any other way to accomplish this? I've
attempted using a mix of SetFocus(), BringWindowToTop(), and
EnableWindow() but couldn't find the magic combo.
I've attached my test script. Run it as "WinTests cycle" to attempt
to cycle the focus through all top level windows and see the traceback
above. Run it as "WinTests 12345" to set focus the window with hwnd
of 12345. Run it as "WinTests" to get a list of all top level windows
and their hwnd's.
I've tried reading the very-sparse win32 docs provided with
ActiveState's PythonWin, I've read a bit on the win32 API on MSDN and
I've googled around a bit, and haven't found much.
Thanks for making this a great list, all...
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
More information about the Tutor