如何将MetroFramework样式颜色更改为所有控件

时间:2016-01-20 13:23:57

标签: c# winforms frameworks microsoft-metro modern-ui

我在我的桌面应用程序中使用MetroFramework并将所有主题颜色设置为默认,同时在父表单中进行更改我想要将所有子表单和控件颜色更新为par主题颜色。

检查设计UI

http://thielj.github.io/MetroFramework

更改主题

var m = new Random();
int next = m.Next(0, 13);
this.Style = (MetroColorStyle)next;

通过操作,主窗体颜色正在改变,但控制器和子窗体样式颜色不会改变。

3 个答案:

答案 0 :(得分:5)

hHi Sunil,

Exmple -1 MetroStyleManager中的工具箱添加

Add StyleManager Set Setting

示例2(扩展方法)

    public static void SetDefaultStyle(this IContainer contr, MetroForm owner, MetroColorStyle style)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Style = style;
    }
    public static void SetDefaultTheme(this IContainer contr, MetroForm owner, MetroThemeStyle thme)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Theme = thme;
    }
    private static MetroStyleManager FindManager(IContainer contr, MetroForm owner)
    {
        MetroStyleManager manager = new MetroStyleManager(contr);
        foreach (IComponent item in contr.Components)
        {
            if (((MetroStyleManager)item).Owner == owner)
            {
                 manager = (MetroStyleManager)item;
            }
        }
        return manager;
    }

使用:

    public frmMain()
    {
        InitializeComponent();            
        this.components.SetDefaultStyle(this, MetroColorStyle.Purple);
    }

例子 - 3 :如果您想为所有表单设置主题。

第1步:创建新类" MyExtensions.cs"。这是内容:

public static class MyExtensions
{
    //What is your style
    private const MetroColorStyle FormStyle = MetroColorStyle.Green;
    public static void SetStyle(this IContainer container, MetroForm ownerForm)
    {
        if (container == null)
        {
            container = new System.ComponentModel.Container();
        }
        var manager = new MetroFramework.Components.MetroStyleManager(container);
        manager.Owner = ownerForm;
        container.SetDefaultStyle(ownerForm, FormStyle);


    }
    public static void SetDefaultStyle(this IContainer contr, MetroForm owner, MetroColorStyle style)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Style = style;
        owner.Style = style;
    }
    public static void SetDefaultTheme(this IContainer contr, MetroForm owner, MetroThemeStyle thme)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Theme = thme;
    }
    private static MetroStyleManager FindManager(IContainer contr, MetroForm owner)
    {
        MetroStyleManager manager = null;
        foreach (IComponent item in contr.Components)
        {
            if (((MetroStyleManager)item).Owner == owner)
            {
                manager = (MetroStyleManager)item;
            }
        }
        return manager;
    }
}

第2步:在所有表单中,您需要在" Load"中调用劣质方法。方法。

的Form1.cs例外
private void Form1_Load(object sender, EventArgs e)
{
    this.components.SetStyle(this);
}

答案 1 :(得分:0)

metroStyleManager1.Theme = metroStyleManager1.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light;
this.Theme = metroStyleManager1.Theme;
this.Refresh();

答案 2 :(得分:0)

我知道这是一个老问题,但是就像您在寻找解决方案一样,就像使用Clone()的{​​{1}}方法一样简单,这会将样式更改为表单以及其中包含的所有MetroFrameWork控件

StyleManager
  

注意:必须启用表单控件的Form1 _form1 = new Form1(); this.StyleManager.Clone(_form1); //This will do everything for you 属性UseStyleColors,才能正常工作。

enter image description here