Table of Contents

Themes

A theme is an ExTheme object, a palette of colors that the controls of a form take over. Ex* controls apply it themselves, standard WinForms controls through handlers that the application registers in ExThemer.

The palette

Property Used for
UseSystemStyle Ex* controls let Windows draw them and restore what the custom style changed. Boxes, lists, trees and labels still take the box and label colors, so a dark scheme with the Windows look of the controls works too.
DarkScrollBars Scroll bars in the dark Windows theme (DarkMode_Explorer).
PanelBackColor, PanelForeColor Panels, forms, menus and group boxes.
BoxBackColor, BoxForeColor Text boxes, lists, combo boxes and grids.
ButtonBackColor, ButtonForeColor Buttons, tab headers and headers of grids.
BorderColor Borders of boxes, buttons and tab pages.
HighlightBackColor, HighlightForeColor Selection, hover and focus.
MarkColor Check marks, radio marks and week numbers of the calendar.
LabelForeColor Labels.
TodayText Text of the Today button of the calendar, null keeps the default one.

The default values are the system colors, so new ExTheme() in the custom style looks close to Windows. A dark theme sets all colors and DarkScrollBars; a light theme can keep the scroll bars.

Applying a theme

ExThemer.Apply(controls, theme) walks a collection of controls, usually form.Controls:

  • a control that implements IThemeable (all Ex* controls) applies the theme itself, including its child controls;
  • any other control is passed to the handler registered for its type or the nearest base type;
  • a control without a handler stays as it is and its child controls are not visited;
  • the context menu of a control (ContextMenuStrip) gets the theme too, if a handler for its type exists.

ExThemer.Apply(control, theme) does the same for a single control. The form itself isn't part of its Controls collection, set its BackColor, ForeColor and ExForm.DarkTitleBar (or TitleBarBackColor) yourself.

Apply a theme again whenever it changes, for example after the user switches between a light and a dark theme. Forms opened later need it too, so apply it in their constructor or OnLoad.

Handlers for standard controls

ExThemer.Register<T> registers a handler for controls of type T and its derived types that have no handler of their own. Register the handlers once at startup, before the first form is shown:

ExThemer.Register<DataGridView>((grid, theme) =>
{
    var cells = grid.DefaultCellStyle;
    ExThemer.KeepDefaults(grid, theme, nameof(grid.BackgroundColor), nameof(grid.GridColor));
    ExThemer.KeepDefaults(cells, theme, nameof(cells.BackColor), nameof(cells.ForeColor),
        nameof(cells.SelectionBackColor), nameof(cells.SelectionForeColor));
    if (theme.UseSystemStyle)
        return;

    grid.BackgroundColor = theme.BoxBackColor;
    grid.GridColor = theme.BorderColor;
    cells.BackColor = theme.BoxBackColor;
    cells.ForeColor = theme.BoxForeColor;
    cells.SelectionBackColor = theme.HighlightBackColor;
    cells.SelectionForeColor = theme.HighlightForeColor;
});

ExThemer.Register<SplitContainer>((split, theme) =>
{
    ExThemer.Apply(split.Panel1.Controls, theme);
    ExThemer.Apply(split.Panel2.Controls, theme);
    split.BackColor = theme.PanelBackColor;
    split.ForeColor = theme.PanelForeColor;
});

A handler of a container calls ExThemer.Apply on its child controls before it changes its own colors. A child control that inherits BackColor or ForeColor from its parent would otherwise see the theme color as its original value: a DataGridView even copies an inherited ForeColor into its cell style, and KeepDefaults would then restore the theme color instead of the system one.

ExThemer.Register replaces a handler registered earlier for the same type, so an application can override a default handler of a shared library.

Returning to the system style

A theme with UseSystemStyle = true switches Ex* controls back to DefaultStyle, in which Windows draws them. The controls then look as if they never had the custom style, because the custom style keeps the properties it changes.

Handlers of standard controls do the same with ExThemer.KeepDefaults(target, theme, propertyNames). Call it before the handler changes the properties:

  • on the first call it remembers the current values of the properties;
  • with UseSystemStyle = true it restores them in the given order. A property that wasn't set (inherited from the parent or default) is reset, so it inherits again.

KeepDefaults works on any object, also on a DataGridViewCellStyle or an ExFlatButtonAppearance. A property name that the object doesn't have throws ArgumentException.

Borders and scroll bars

Two extension methods in ExTools help with standard controls:

  • control.SetBorderColor(color) draws the 1 px border of a control with BorderStyle.FixedSingle (ListBox, TreeView, CheckedListBox, RichTextBox) in the given color, like the border of ExTextBox in the custom style. null restores the system border. The color stays after the handle is recreated and appears also in DrawToBitmap.
  • control.SetDarkScrollBars(dark) turns the dark scroll bars of a control on or off. Turning them off restores the default theme only on controls that had the dark scroll bars.

In the custom style a standard control with a border should get a border in the theme color; a control without a border in the design stays without one.

Themes of your own controls

A user control or a custom control can style itself by implementing IThemeable:

public class StatusPanel : UserControl, IThemeable
{
    public void ApplyTheme(ExTheme theme)
    {
        ExThemer.Apply(Controls, theme);
        ExThemer.KeepDefaults(this, theme, nameof(BackColor), nameof(ForeColor));
        if (theme.UseSystemStyle)
            return;

        BackColor = theme.PanelBackColor;
        ForeColor = theme.PanelForeColor;
    }
}

ExThemer calls ApplyTheme instead of a registered handler, so the control decides how its child controls are styled.