pythonc++11winapi

How to get color settings of system UI (color theme) in Windows 10 with Python?


I'm trying to get some color settings in UI of Windows 10, e.g. colors of window title bar when windows are activated/inactivated, the edge color of window. Or to be more simple, I'm searching for a function like getSysColor() in winuser.h(Documentation) in Windows 10.

What I mean by "colors of window title bar when windows are activated" under "color settings in UI of Windows 10". (red circle)

Here's something I've tried:

  1. getSysColor(nIndex) in winuser.h:

    This function cannot work well under Windows 10, for some keys(nIndex) are not supported according to the (Documentation) . For example, if I ask for the colors of activated window title bar with ctypes.windll.user32.GetSysColor(2) in Python, it will return the color of ##99B4D1, which is maybe the color under Windows 7, rather than the ##0078D7 in Windows 10. But I don't want a color set of Windows 7.

  2. DwmGetColorizationColor() in dwmapi.h:

    Some suggest that this function may retrieve the color of title bar. But I can only get a strange yellow (##F8FFA9) with ctypes.windll.dwmapi.DwmGetColorizationColor(), which is definetly not what I want. Meanwhile this function can only offer color of title bar, rather than all the color used in Windows UI.


Solution

  • You need to call UISettings::GetColorValue with UIColorType::Accent.

    Sadly the official Python/WinRT bindings don't support anything newer than Python 3.9 since they have been abandoned (and while you can use RoGetActivationFactory and friends directly, it's not convenient), but the community fork works quite well.

    You need winrt-Windows.UI and winrt-Windows.UI.ViewManagement:

    from winrt.windows.ui.viewmanagement import UISettings, UIColorType
    
    accent_color = UISettings().get_color_value(UIColorType.ACCENT)
    print("accent color:", accent_color.r, accent_color.g, accent_color.b, accent_color.a)