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

When running a Windows Forms application in C #, the form view looks just like a Windows theme.

How can I transfer my theme to my application, which is not dependent on the Windows theme?

+4
source share
4 answers

Override the OnPaint method and draw whatever 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(); } 
+3
source

You cannot do it easily. At your disposal are several alternatives.

  • The easiest way is to create your own Skin XML file in which you specify your own colors for your application, you read it through the class that you create, and also apply new colors. This will keep things separate and ready for future changes. But note that you still won’t be able to change the way the title bar and other system things are displayed, such as what the X and Maximize buttons look like.

  • Expanding at point 1, you can create your forms as borderless and create your own window with custom painting (override OnPaint) and images. This is harder to do. You can inherit from the Form class and create your own CustomDrawnForm, which you will use in your application.

  • Use one of the many management libraries, such as DevExpress. Some of them are free, some of them are expensive.

What you are trying to do is not very easy on Windows.Forms, and maybe you should look at WPF and other alternatives.

+2
source

It depends on your intentions regarding the subject; as Hans says in his comment, usually using the system "theme" for controls and the appearance of the window is considered active.

However, for theme elements in your application - for example, the title bar background or title font color, etc., then I would build an interface with color / image definitions in your application (e.g. ITheme ) and then just use regular data binding for proper configuration at runtime when ITheme .

 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 can take another step ... For example, in our application, we also define IThemeManager :

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

We allow the ThemeManager attachment to get involved, and then bind the Current property to it 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(); } } } 
+1
source

I know this question is quite old, but for those who (like me) are still interested in creating truly “thematic” forms of Windows, as mentioned above, WPF is very good for creating themes. There are also several pre-created themes (Google and stackoverflow - always your friends) to download. In the world of Windows Forms projects, there might be a bit of a learning curve, but IMHO is worth it. But if you want to stay with a simple Windows Forms application (like me), the simplest suggestion is to create forms without fields (Set FormBorderStyle - None). This will support most of the standard Windows container properties. Of course, you will need to create your own thematic title and borders, but basically this is the "theme" of the Windows form. You will also need to create your own calibration and moving methods, but again, Google and stackoverflow are your friends. This simple sentence, as obvious as it may be for some, was huge for me.

0
source

All Articles