Search code examples
c#winformsuser-interfacedrawingaero

How to get Windows 7 aero selection color?


How can I retreive system colors related to current Aero style? I especially need the colors used in the selection gradient. SystemColors structure does not contain required colors.

Aero colors

Alternatively: How can I use WinAPI to draw selection on the specific canvas (Graphics object)?


Solution

  • OK, so the answer to the initial question is: there is no way to determine the specific colors I asked for. It is evaluated by internal theming routines provided by the OS.

    Fortunately, there is a way to ask the OS to draw a themed piece of control, so called part. .NET provides two classes for drawing UI:

    Selection I asked for may be drawn by the following code:

    // Graphics g = ...
    VisualStyleRenderer selectionRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Item.Selected);
    selectionRenderer.DrawBackground(g, someRectangle);
    

    Unfortunately, neither TreeView.Item.Selected, nor ListView.Item.Selected is supported by the default window theme. However, one may switch theme to the Explorer using WinAPI via p/invoke:

    // C++
    SetWindowTheme(hwnd, L"Explorer", nullptr);
    

    And then P/invoke his way through few UXTheme.h routines, which works perfectly.