繁体   English   中英

使python控制台在Windows任务栏中闪烁

[英]Getting python console to flash in Windows taskbar

我试图使我的python应用程序在任务栏中闪烁其父控制台的图标。 我尝试了以下方法:

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

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

但是,这些都不能达到预期的效果。

我在Windows 10上使用python 3.5.4,正在将cmder用作控制台。

使用FlashWindowEx专门仅刷新任务栏图标。 对于常规控制台窗口,请通过GetConsoleWindow获取窗口句柄。 对于ConEmu之类的替代控制台,这可能不起作用,除非它是他们破解的API函数之一。

例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM