gpt4 book ai didi

c# - 如何更改所有控件的 MetroFramework 样式颜色

转载 作者:行者123 更新时间:2023-12-02 09:44:56 25 4
gpt4 key购买 nike

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

检查设计 UI

http://thielj.github.io/MetroFramework

更改主题

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

通过该操作,主窗体颜色发生变化,但 Controller 和子窗体样式颜色没有变化。

最佳答案

嗨,苏尼尔,

示例-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);
}

关于c# - 如何更改所有控件的 MetroFramework 样式颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34901295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com