[Python] Programmatically querying/toggling Windows "Show my taskbar on all displays" via...

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have an application in which I need to (at least while it's running) show the taskbar on all Windows displays, so I'm looking for a way to programmatically toggle the Windows "Show my taskbar on all displays" setting via Python.

    I've been able to successfully query and toggle taskbar auto-hiding by using this:

    import ctypes


    ABM_GETSTATE = 0x00000004
    ABM_SETSTATE = 0x0000000A
    ABS_AUTOHIDE = 0x00000001


    class RECT(ctypes.Structure):
    _fields_ = [
    ("left", ctypes.c_long),
    ("top", ctypes.c_long),
    ("right", ctypes.c_long),
    ("bottom", ctypes.c_long)
    ]

    class APPBARDATA(ctypes.Structure):
    _fields_ = [
    ("cbSize", ctypes.c_ulong),
    ("hWnd", wintypes.HWND),
    ("uCallbackMessage", ctypes.c_uint),
    ("uEdge", ctypes.c_uint),
    ("rc", RECT),
    ("lParam", ctypes.c_int)
    ]

    def get_taskbar_state() -> int:
    abd = APPBARDATA()
    abd.cbSize = ctypes.sizeof(APPBARDATA)
    return ctypes.windll.shell32.SHAppBarMessage(ABM_GETSTATE, ctypes.byref(abd))


    def get_taskbar_autohide() -> bool:
    return bool(get_taskbar_state() & ABS_AUTOHIDE)


    def set_taskbar_autohide(auto_hide: bool) -> bool:
    if auto_hide:
    new_state = get_taskbar_state() | ABS_AUTOHIDE
    else:
    new_state = get_taskbar_state() & ~ABS_AUTOHIDE
    abd = APPBARDATA()
    abd.cbSize = ctypes.sizeof(APPBARDATA)
    abd.lParam = new_state
    result = ctypes.windll.shell32.SHAppBarMessage(ABM_SETSTATE, ctypes.byref(abd))
    return bool(result)


    But I haven't had similar luck finding a way to modify the other taskbar settings.

    So far I've tried adding this value to the Registry:

    Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced


    MMTaskbarEnabled | REG_DWORD | 0x00000001


    But this requires either a reboot or restarting explorer.exe in order to take effect, which I don't want.

    With that off the table, I tried this:

    from ctypes import WinDLL

    user32 = WinDLL('user32')
    HIDE = 0
    SHOW = 5
    hWnd = user32.FindWindowW('Shell_SecondaryTrayWnd', None)
    user32.ShowWindow(hWnd, SHOW) # HIDE works the same way


    Which only works if "Show my taskbar on all displays" is already enabled, and doesn't gracefully resize any windows on the affected display(s) in the way that manually toggling the checkbox does.

    Additionally, neither of the above approaches seem to allow me to readily query the existing state - ideally I'd like to be able to restore whatever settings the user had before my app started.

    Continue reading...

Compartilhe esta Página