How can I apply my own theme to my Windows Forms application?
Asked Answered
D

4

7

On executing a Windows Forms application in C# the view of the form looks the same as the theme of Windows.

How can I give my own theme to my application which doesn't depend upon the Windows theme?

Decortication answered 3/11, 2010 at 12:54 Comment(3)
Have you tried removing the call to EnableVisualStyles() from your main program?Estrogen
yes, but it gives only classic styleDecortication
Making your UI resemble the theme that the user selected is widely considered to be an asset, not a liability. It takes the resources of big companies like Adobe and Microsoft to continuously update their custom skinning code to prevent it from getting stale and silly. Have a look at this one: stackoverflow.com/questions/238177/worst-ui-youve-ever-used/…Calondra
M
4

You can't do that easily. There are several alternatives at your disposal.

  1. The simplest way is to create a Skin XML file of your own, in which you specify your own colors for your Application, you read it via a Class you create as well and you apply the new colors. This will keep things separated and ready for future changes. But note that you still won't be able to change how the Title Bar is rendered and other system-specific things, such as how the X and Maximize buttons look.

  2. Expanding on point 1, you could create your forms as borderless and create your window with custom painting (override OnPaint) and images. This is harder to accomplish. You may want to inherit from the Form class and create your own CustomDrawnForm which you will use across your application.

  3. Use one of the many control libraries out there, such as DevExpress. Some are free, some are expensive.

What you're trying to do is not very simple in Windows.Forms, and maybe you should look at WPF and other alternatives.

Mossgrown answered 3/11, 2010 at 12:59 Comment(0)
W
2

It depends on your intent for the theming; as Hans says in his comment, generally using the system's "theme" for controls and window appearance is considered an asset.

However, to theme elements within your application - e.g. the background of a header panel or heading font color etc. then I would build an interface with definitions for the colors/images in your application (e.g. ITheme) and then just use regular databinding to configure them appropriately at runtime when the ITheme is set.

public interface ITheme
{
    string Name { get; }
    Image Logo { get; }
    String BrandText1 { get; }
    String BrandText2 { get; }
    Image BrandBannerLogo { get; }
    Color BrandPanelText_Left { get; }
    Color BrandPanelText_Centre { get; }
}

In fact, you could take it a step further... For example, in our application we also define an IThemeManager:

public interface IThemeManager : INotifyPropertyChanged
{
    event EventHandler CurrentThemeChanged;
    ITheme CurrentTheme { get; set; }
    Dictionary<string, ITheme> AvailableThemes { get; }
}

We allow the ThemeManager to be dependency injected and then we bind to it's Current property in our controls:

    [Dependency]
    public IThemeManager ThemeManager
    {
        get { return _themeManager; }
        set
        {
            if (_themeManager != value)
            {
                _themeManager = value;
                if (_themeManager != null && !DesignMode)
                {
                    _headerPanelBackgroundImageBinding = themePanel.DataBindings.Add("BackgroundImage", ThemeManager, "CuurentTheme.Logo", false, DataSourceUpdateMode.Never);
                }
                else
                {
                    // Reset to the default
                    this.DataBindings.Remove(_headerPanelBackgroundImageBinding);
                }

                Invalidate();
            }
        }
    }
Wellnigh answered 4/11, 2010 at 6:52 Comment(0)
D
1

Override the OnPaint method and draw whatever things you want. :)

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    SolidBrush brush = new SolidBrush(Color.Black);
    float percent = (float)(val - min) / (float)(max - min);
    Rectangle rect = this.ClientRectangle;

    rect.Width = (int)((float)rect.Width * percent);

    g.FillRectangle(brush, rect);

    brush.Dispose();
    g.Dispose();
}
Douceur answered 3/11, 2010 at 13:3 Comment(0)
E
0

I know this question is fairly old, but for those (like I was) still interested in creating truly "themed" Windows forms, as mentioned above, WPF is very good for creating themes. There are also quite a few pre-created themes available (Google and stackoverflow are always your friends) for download. There may a little bit of a learning curve from the Windows Forms project world, but IMHO well worth it. But if you wish to stay with a plain Windows Forms Application (as I did), the simplest suggestion is to create Borderless forms (Set FormBorderStyle to None). This will maintain most of the standard windows container properties. Of course you will need to create your own "themed" TitleBar and borders, but that is mostly where the "theme" of a Windows Form is. You will also need to create your own sizing and moving methods, but again, Google and stackoverflow are your friends. This simple suggestion as obvious as it may be to some, was huge for me.

Erichericha answered 21/7, 2016 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.