【问题标题】:Would like to add different themes to my application想为我的应用程序添加不同的主题
【发布时间】:2022-08-14 12:31:09
【问题描述】:

基本上,我正在制作一个基本的笔记应用程序,并想添加不同的颜色选项,例如,浅色版本和深色版本,以便用户查看。我打算把它给几个朋友,但没有人能就颜色达成一致,所以我想我只是让用户通过小按钮对其进行自定义。

然而,我不是很有经验,想知道我将如何去做这件事?

  • 这个词是“主题”。你想要主题。您使用的是 UI 控件框架或库,例如 RAD 控件、Telerik 或类似的东西,还是只是普通的 ol\' WinForms?
  • 只是 Visual Studio 2022 中的基本 C# 和 winforms。感谢您提供正确的术语!
  • 在 2022 年,不要浪费时间开发自己的主题解决方案,而是使用搜索引擎评估现有的商业/开源解决方案。

标签: c# winforms


【解决方案1】:

您可以遍历所有控件并更改它们的 .BackColor:

private void ChangeTheme_btn_Click(object sender, EventArgs e)
{
    ChangeTheme(this.Controls, Color.Aqua);
}

private void ChangeTheme(Control.ControlCollection controls, Color color)
{
    foreach (Control control in controls)
    {
        if (control.HasChildren)
        {
            // Recursively loop through the child controls
            ChangeTheme(control.Controls, color);
        }
        else
        {
            if (control is TextBox textBox)
            {
                textBox.BackColor = color;
            }
            else if (control is Button button)
            {
                button.BackColor = color;
            }
            // Example (remove this if you are not using Guna UI)
            else if (control is Guna.UI2.WinForms.Guna2Button gBbutton)
            {
                gBbutton.FillColor = color;
            }
        }
    }
}

您可以为每种类型的控件设置单独的颜色,还可以更改其他属性。例如,如果您使用Guna UI,那么您可能需要设置.FillColor 而不是.BackColor。

如果您有多个表格,您可以执行以下操作:

private void MyForm1_form_Load(object sender, EventArgs e)
{
    // Add all the controls to the list
    foreach (Control item in MyForm1.instance.Controls)
    {
        listOfAllFormControls.Add(item);
    }
    foreach (Control item in MyForm2.instance.Controls)
    {
        listOfAllFormControls.Add(item);
    }
}

private void ChangeTheme_btn_Click(object sender, EventArgs e)
{
    SetColorThemeToLight(listOfAllFormControls);
}

private readonly List<Control> listOfAllFormControls = new List<Control>();

private void SetColorThemeToLight(List<Control> list)
{
    foreach (Control control in list)
    {
        if (control.HasChildren)
        {
            // Recursively loop through the child controls
            List<Control> controlList = new List<Control>();
            foreach (Control item in control.Controls)
            {
                controlList.Add(item);
            }
            SetColorThemeToLight(controlList);
        }
        else
        {
            switch (control)
            {
                case TextBox _:
                    control.BackColor = Color.Blue;
                    break;

                case Button _:
                    control.BackColor = Color.Blue;
                    break;

                // Example (remove this if you are not using Guna UI)
                case Guna.UI2.WinForms.Guna2Button _:
                    control.BackColor = Color.Blue;
                    break;
            }
        }
    }
}

我也改用switch case

如果您使用的是 GUNA UI

仅当您使用 Guna 时才需要这样做:

一些 Guna 控件默认有一个子控件,这不适用于上面的代码。例如,Guna2RadioButton 包含一个名为 Guna2CustomRadioButton 的子控件。

因此,如果您使用的是 Guna,这是更新后的代码:

private static void SetColorThemeToDark(List<Control> list)
{
    foreach (object control in list)
    {
        bool pass = false;
        Control realControl = (Control)control;
        if (realControl.HasChildren)
        {
            // Recursively loop through the child controls
            List<Control> childList = new List<Control>();
            foreach (object item in realControl.Controls)
            {
                childList.Add(item);
            }
            SetColorThemeToDark(childList);
            pass = true;
        }
        if (!realControl.HasChildren || pass)
        {
            switch (control)
            {
                case Button button :
                    button.BackColor = Color.Red;
                    button.ForeColor = Color.White;
                    break;

                case Label label:
                    label.BackColor = Color.Red;
                    break;

                case Guna.UI2.WinForms.Guna2Button guna2Button :
                    guna2Button.FillColor = Color.Red;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2020-02-04
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多