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

Sun*_*rya 2 c# frameworks winforms microsoft-metro modern-ui

我正在使用MetroFramework在桌面应用程序中,并将所有主题颜色广告设置为默认值,同时更改父表单,我想更新所有子表单并将控件颜色设置为标准主题颜色。

检查设计UI

http://thielj.github.io/MetroFramework

变更主题

var m = new Random();
int next = m.Next(0, 13);
this.Style = (MetroColorStyle)next;
Run Code Online (Sandbox Code Playgroud)

通过该操作,主窗体颜色会更改,但控制窗体和子窗体样式颜色不会更改。

Fat*_*DAL 5

嗨,苏尼尔,

在MetroStyleManager中添加示例-1工具箱

添加StyleManager 设定设定

示例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;
    }
Run Code Online (Sandbox Code Playgroud)

使用方法:

    public frmMain()
    {
        InitializeComponent();            
        this.components.SetDefaultStyle(this, MetroColorStyle.Purple);
    }
Run Code Online (Sandbox Code Playgroud)

示例-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;
    }
}
Run Code Online (Sandbox Code Playgroud)

步骤2:在所有表单中,您都需要在“ Load”方法中调用劣等方法。例如Form1.cs

private void Form1_Load(object sender, EventArgs e)
{
    this.components.SetStyle(this);
}
Run Code Online (Sandbox Code Playgroud)