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?
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?
You can't do that easily. There are several alternatives at your disposal.
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.
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.
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.
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();
}
}
}
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();
}
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.
© 2022 - 2024 — McMap. All rights reserved.