[python-win32] Cleanly Close System Tray Process
Tim Roberts
timr at probo.com
Wed Aug 2 19:18:40 CEST 2006
On Tue, 1 Aug 2006 15:34:56 -0400, " Gregory Pi?ero "
<gregpinero at gmail.com> wrote:
>Ok I figured out how to do it:
>
>Here's the code I made mostly from copying the examples. If anyone
>could tell me how the code works, that would still be appriciated, for
>example why/how do I need this function _MyCallback?
>
>
The EnumWindows API runs through the system's linked list of all
top-level windows. For every window it finds, it calls a function that
you provide. If you know C++, it's a lot like the for_each function in
STL. _MyCallback is the function that gets called, once for every
top-level window. It is passed the handle of each window in turn, along
with a "context" parameter that you provide. In your example, the
context parameter is a tuple that will be used to keep track of all of
the windows and window classes that were found.
So, when "get_all_windows" finishes, it returns a dictionary that
contains one entry for every window class. request_windows_to_close
then checks to see if the class(es) you requested are present in the
dictionary, and if they are, it sends them a WM_CLOSE message.
This code is not actually correct. There can be many windows for a
given window class, but your get_all_windows function will only return
the last one found for each class. A better implementation of
_MyCallback would be:
def _MyCallback( hwnd, extra ):
hwnds, classes = extra
hwnds.append(hwnd)
classn = win32gui.GetClassName(hwnd)
if not classn in classes:
classes[classn] = []
classes[classn].append( hwnd )
Then request_windows_to_close becomes:
for windesc_to_close in list_window_descs_to_close:
if windesc_to_close in classes:
for hwnd in classes[windesc_to_close]:
win32gui.PostMessage( hwnd, win32con, WM_CLOSE, 0, 0 )
...
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-win32
mailing list