1

I am attempting to cause my python application to flash its parent console's icon in the taskbar. I have tried the following:

ctypes.windll.user32.FlashWindow(
ctypes.windll.kernel32.GetConsoleWindow(), True)

and

ctypes.windll.user32.FlashWindow(
ctypes.windll.user32.GetParent(ctypes.windll.kernel32.GetConsoleWindow()), True)

But neither of these cause the desired effect.

I am using python 3.5.4, on Windows 10. I am using cmder as my console.

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78
  • https://stackoverflow.com/questions/53182796/python-ctypes-issue-on-different-oses/53185316#53185316, https://stackoverflow.com/questions/52268294/python-ctypes-cdll-loadlibrary-instantiate-an-object-execute-its-method-priva/52272969#52272969. **Define *argtypes* and *restype* for the 2 functions**. – CristiFati Jan 16 '19 at 23:52

1 Answers1

2

Use FlashWindowEx to specifically flash just the taskbar icon. For a regular console window, get the window handle via GetConsoleWindow. This may not work for alternative consoles such as ConEmu, unless it's one of the API functions that they hack.

For example:

import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
user32 = ctypes.WinDLL('user32', use_last_error=True)

FLASHW_STOP = 0
FLASHW_CAPTION = 0x00000001
FLASHW_TRAY = 0x00000002
FLASHW_ALL = 0x00000003
FLASHW_TIMER = 0x00000004
FLASHW_TIMERNOFG = 0x0000000C

class FLASHWINFO(ctypes.Structure):
    _fields_ = (('cbSize', wintypes.UINT),
                ('hwnd', wintypes.HWND),
                ('dwFlags', wintypes.DWORD),
                ('uCount', wintypes.UINT),
                ('dwTimeout', wintypes.DWORD))
    def __init__(self, hwnd, flags=FLASHW_TRAY, count=5, timeout_ms=0):
        self.cbSize = ctypes.sizeof(self)
        self.hwnd = hwnd
        self.dwFlags = flags
        self.uCount = count
        self.dwTimeout = timeout_ms

kernel32.GetConsoleWindow.restype = wintypes.HWND
user32.FlashWindowEx.argtypes = (ctypes.POINTER(FLASHWINFO),)

def flash_console_icon(count=5):
    hwnd = kernel32.GetConsoleWindow()
    if not hwnd:
        raise ctypes.WinError(ctypes.get_last_error())
    winfo = FLASHWINFO(hwnd, count=count)
    previous_state = user32.FlashWindowEx(ctypes.byref(winfo))
    return previous_state
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111