Reputation: 524
I have implemented Metro Framework in my Windows Form Application. I want to change the style color to custom color. (As shown in the screenshot) How do I do this?
Upvotes: 0
Views: 10512
Reputation: 21
I realise this is an old question but this was driving me mad and I couldn't find answers anywhere. I did, however, figure it out so thought I'd share... As long as you don't mind editing the source code a little, there is a way to add custom colors.
1. Start with the MetroColors
Class. Right at the bottom there is some code relating to a Custom option. Replace it with the following:
public static Color customColor = new Color();
public static Color Custom
{
get
{
return customColor;
}
}
2. Next, move onto the MetroPaint
Class. Near the bottom, expand the Helper Methods
region and add a case for Custom to GetStyleColor
,
public static Color GetStyleColor(MetroColorStyle style)
{
switch (style)
{
case MetroColorStyle.Custom:
return MetroColors.Custom;
}
GetStyleBrush
and
public static SolidBrush GetStyleBrush(MetroColorStyle style)
{
switch (style)
{
case MetroColorStyle.Custom:
return MetroBrushes.Custom;
}
GetStylePen
. (Make sure to leave the defaults at the end)
public static Pen GetStylePen(MetroColorStyle style)
{
switch (style)
{
case MetroColorStyle.Custom:
return MetroPens.Custom;
}
}
3. Make sure that the MetroPens
public static Pen Custom
{
get
{
return GetSavePen("Custom", MetroColors.Yellow);
}
}
and MetroBrushes
have the Custom options at the end of them (I don't remember if they were already there or not)
public static SolidBrush Custom
{
get
{
return GetSaveBrush("Custom", MetroColors.Custom);
}
}
4. Save your changes and build it, then replace the MetroFramwork.dll
in your project with the newly edited one.
5. I'll assume you already have a comboBox
hooked up to the StyleManager
. Add "Custom" to the end of the comboBox
items (its index should be 15 as long as you haven't excluded any of the other colors) and add a colorDialog
to the form.
6. Add a SelectedIndexChanged
event handler to the comboBox
and then add the following:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 15)
{
Color c = new Color();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
c = colorDialog1.Color;
}
MetroColors.customColor = c;
metroStyleManager1.Style = MetroColorStyle.Custom;
}
else
{
metroStyleManager1.Style = (MetroFramework.MetroColorStyle)Convert.ToInt32(comboBox1.SelectedIndex);
}
Refresh();
}
Now when you select Custom from the combBox
, the colorDialog
will appear and you can choose any color you want. If you just wanted to change to a fixed custom color that you don't want to be changed, you can just specify the RGB and apply it on FormLoad
instead:
private void Form1_Load(object sender, EventArgs e)
{
MetroColors.customColor = Color.FromArgb(0, 0, 0);
metroStyleManager1.Style = MetroColorStyle.Custom;
}
You can repeat these steps as many times as you want to add new colors (Custom1, Custom2 etc.). Just remember to add these to the list in the MetroColorStyle.cs
as well.
Upvotes: 2
Reputation: 3
As already stated before, a custom color is not possible. However, you can use the already defined colors. (See @SomeCoder's answer)
If you want to make a ComboBox to change the theme/style you don't have to make a big switch statement with all the themes/styles, you can simply do this:
private void ColorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Enum.TryParse(colorComboBox.Text, out MetroColorStyle color);
this.Style = color;
}
catch (ArgumentException)
{
this.Style = MetroColorStyle.Default;
}
Refresh();
}
Upvotes: 0
Reputation: 3025
Short answer: You can't.
A workaround would be to override the OnPaint
method of the MetroForm
and draw it yourself. For example:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
// custom draw the top border
using (Brush b = new SolidBrush(Color.White))
{
int borderWidth = 5; // MetroFramework source code
e.Graphics.FillRectangle(b, 0, 0, Width, borderWidth);
}
}
Long answer: I am using MetroModernUI
from Nuget. And wanted to remove the top border.
Reading the source-code of the Framework, I found the method responsible for the draw of the top border, which is similar to the code above.
The method responsible for get the color for draw is MetroPaint.GetStyleBrush(Style)
, where Style
is the Enum MetroColorStyle
. It is a switch-case:
public static SolidBrush GetStyleBrush(MetroColorStyle style)
{
switch (style)
{
case MetroColorStyle.Black:
return MetroBrushes.Black;
case MetroColorStyle.White:
return MetroBrushes.White;
.
.
.
}
}
Basically you are limited to choose one of the colors available. (or fork the code and live the freedom!)
Reference: MetroModernUI (Github)
Upvotes: 3
Reputation: 293
You can use:
this.Style = MetroFramework.MetroColorStyle.<color>;
Possible colors:
So for example:
this.Style = MetroFramework.MetroColorStyle.Red;
You can use this on any MetroFramework control:
metroProgressBar1.Style = MetroFramework.MetroColorStyle.Blue;
You can also switch between light and dark theme:
this.Theme = MetroFramework.MetroThemeStyle.Light;
//or
this.Theme = MetroFramework.MetroThemeStyle.Dark;
Upvotes: 0
Reputation: 31
I know this is an old question but it might help others looking for answers in the future.
If you are using the nuget Metro Framework package for C# in Visual Studio then you need to set the "UseCustomBackColor" of the object to true:
metroTile_YourTile.UseCustomBackColor = true;
and then change the back color in the designer to "Custom" and pick the color, or set it programmatically:
metroTile_YourTile.UseCustomBackColor = Color.FromArgb(100,100,100);
This is true for a multitude of objects in the package, including buttons, dropdowns, etc. I hope that is helpful.
Upvotes: 3
Reputation: 107
I am not sure if this will work, but try to use
this.BackColor = (your colour)
Also please let me know if this is working or not.
Upvotes: -1