2

I want to focus a windows when it is blinking/flashing. The more common case is when someone sends some text by a chat software (for example MSN). In this case the windows bar is going to start blink in the start bar. I don't know if i am explaining myself. I want to get the HWND of the blinking windows. If more info is necesary to understand it, I will try to explain me better.

I have already searched info about this case but I find nothing. Maybe it can be resolver using "win32gui" library.

Thank you for your help!!!

user1618465
  • 1,813
  • 2
  • 32
  • 58
  • Python's probably the wrong tool for this. You might want to look at [Autohotkey](http://www.autohotkey.com/). Although you can hook into autohotkey and similar libraries from python, you don't get that much of a benefit. – forivall Feb 02 '13 at 02:40
  • @forivall: Does AutoHotkey provide any way to trigger on a window flashing? If not, I don't think it really helps. The actual "focus the window" part is 1 trivial line with `pywinauto` or maybe 3 lines with `win32gui`, so there's really no reason to try to simplify that. – abarnert Feb 02 '13 at 02:46
  • https://www.google.com/search?q=autohotkey+get+flashing+window – forivall Feb 02 '13 at 02:49
  • OK, it looks like you have to register a windows shell hook, and honestly (see [this answer](http://www.autohotkey.com/board/topic/36510-detect-flashingblinking-window-on-taskbar/?p=229583)), and the code to do that doesn't look any simpler than the equivalent `win32gui` code. – abarnert Feb 02 '13 at 02:52

1 Answers1

1

First, most programs flash their windows by calling FlashWindowEx (or some higher-level function that wraps it). But there are a few apps—mostly from Microsoft—that do some custom stuff instead that looks like window-flashing to the end user, but may not look the same under the covers. Hopefully, you don't care about any such custom apps.

Anyway, the easiest way to capture that information is to install a shell hook with SetWindowsHookEx or RegisterShellHookWindow. (You could instead explicitly inject code in front of user32.dll… but you don't want to try that from Python.) When you do this, Windows will treat your window as if it were part of Explorer (the "shell") and send it special messages about what other programs are doing—in particular, WM_SHELLHOOKMESSAGE.

As forivall pointed out, this might be easier to do from AutoHotkeythis answer in the forums shows how to do it. It might also be easier to do from VB, or even C++. Yes, those languages are generally more difficult than Python, but the actual logic in your code is pretty trivial, and the only hard part is getting the shell hook messages, and that part will be easier in those languages. Another alternative is to use IronPython and do it via .NET.

But you asked if it's possible to do it from Python, and… yes, it is. I believe the relevant functions are not wrapped up by win32gui, so you'll have to use ctypes to do it from Python. See this SO question for a possible example, and look at some of the Related questions on the side and the ctypes docs for other examples of using ctypes to call different functions out of user.dll.


If you want to set a windows hook, the key function will look something like this (see ShellProc for details):

HSHELL_REDRAW=6
WM_SHELL=10

def my_callback(nCode, wParam, lParam):
    if nCode == HSHELL_REDRAW and lParam:
        got_flashing_window_with_hwnd(wParam)

hook = user32.SetWindowsHookEx(WM_SHELL, my_callback, None, 0)

But you need to set the types and push the callback through ctypes.


If you already have a window that you're managing from Python, it's probably easier to set yourself up as a shell hook window instead:

user32.RegisterShellHookWindow(my_hwnd)

Then, in your window proc:

WM_SHELLHOOKMESSAGE = None
def wndproc(hWnd, uMsg, lParam, wParam):
    if WM_SHELLHOOKMESSAGE is None:
        WM_SHELLHOOKMESSAGE = user32.RegisterWindowMessage('SHELLHOOK')
    if uMsg == WM_SHELLHOOKMESSAGE and wParam == HSHELL_FLASH:
        got_flashing_window_with_hwnd(lParam)

I'm not sure whether you need elevated privileges for either of these, but I would suspect you do.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thank you. I tried:`import user32 user32.RegisterShellHookWindow(my_hwnd) M_SHELLHOOKMESSAGE = None def wndproc(hWnd, uMsg, lParam, wParam): if WM_SHELLHOOKMESSAGE is None: WM_SHELLHOOKMESSAGE = user32.RegisterWindowMessage('SHELLHOOK') if uMsg == WM_SHELLHOOKMESSAGE and wParam == HSHELL_FLASH: got_flashing_window_with_hwnd(lParam)` But it said that I dont have the `user32` library. Do you know where can i download it?? – user1618465 Feb 03 '13 at 13:35
  • That i really need is to check if a list of HWND is flashing or not. I have looked around and seen this: `WasFlashing := DllCall("FlashWindow", "uInt", hwnd, "int", true) if (WasFlashing) { MsgBox, % "Was flashing..." } else { MsgBox, % "Was NOT flashing..." }` How can i do it in python??? Thank you so much!! – user1618465 Feb 03 '13 at 13:39
  • `user32` is a Windows DLL. You can't `import` it; you get it from `ctypes.windll`. Look at the `ctypes` documentation. – abarnert Feb 03 '13 at 20:43
  • `FlashWindow` is another method in the `user32` DLL, which you can also call via `ctypes`. However, unless you already somehow know that a window is flashing, this won't be very useful; you'd have to loop, calling it on all of your windows over and over until one of them flashes, which would use up 100% CPU. – abarnert Feb 03 '13 at 20:46