I'm not sure if this is the right place to ask, but here goes. I would like to monitor system wide window messages to see when windows are created/destroyed. The reason being I am writing a small app that will reposition windows automatically. I thought of a couple of different approaches.
First: In a loop: use win32gui.EnumWindows() to see what windows are currently displayed and in the next iteration of the loop, see what windows have changed since the last run. This somewhat worked, but doesn't catch only new windows. For example, some programs may change their title bar text while they are displaying, causing some problems. Second: Also in a loop enumerate running processes. If I see a new process that I want to find windows for, act accordingly. However, this ate up bunches of CPU enumerating processes over and over. The following code demonstrates that: import win32com.client wmi = win32com.client.GetObject('winmgmts:') while self.keep_looking: time.sleep(0.1) if self.processes == []: for p in wmi.InstancesOf('win32_process'): process_name = str(p.Properties_('Name')) process_id = int(p.Properties_('ProcessId')) print process_name, process_id So then I thought there must be a way to receive a message when a new window is created so I can act accordingly. After some research it appears that SetWindowsHookEx is what I'm after, however it is not wrapped by python-win32. However ctypes will let me call those functions, but I'm not quite sure how to proceed. I found a page explaining how to do it in VB, and I've created my code based on it. The site is down, but can be viewed with google cache. http://209.85.165.104/search?q=cache:orLCeZV4kjoJ:04.code-hosting.com/Vi sual_Basic/1353728-Notification-of-program-launch-and-termination+python +SetWindowsHookExA&hl=en&ct=clnk&cd=6&gl=us I'm not sure where to go from here though. The following runs without error, but doesn't show any messages. I know I need to setup a loop, but how do I get the messages? Do I need to create a window with the win32gui functions and read it's messages? Any help would be greatly appreciated. import win32con, win32gui, ctypes hwnd = win32gui.GetActiveWindow() msgShellHook = ctypes.windll.user32.RegisterWindowMessageA("SHELLHOOK") hShellLib = ctypes.windll.kernel32.LoadLibraryA("shell32") #GetModuleHandleA also return the same value lpfnHookProc = ctypes.windll.kernel32.GetProcAddress(hShellLib, "ShellHookProc") ctypes.windll.user32.RegisterShellHookWindow(hwnd) hHook = ctypes.windll.user32.SetWindowsHookExA(win32con.WH_SHELL, lpfnHookProc, hShellLib, 0) Also, some useful links: SetWindowsHookEx http://msdn2.microsoft.com/en-us/library/ms644990.aspx GetProcAddress http://msdn2.microsoft.com/en-us/library/ms683212.aspx RegisterWindowMessage http://msdn2.microsoft.com/en-us/library/ms644947(VS.85).aspx LoadLibrary http://msdn2.microsoft.com/en-us/library/ms684175(VS.85).aspx -Kyle Rickey
_______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32