在所有表格上更改按钮'和图片框'BackColor

时间:2013-12-02 14:48:16

标签: c# button picturebox backcolor

在我的应用程序中,我有一个选项可以自定义显示。基本上,现在它是关于更改按钮和图片框的图像,更改表单图标等。正如您在下面提供的代码中看到的那样。所以,在这种情况下,我通过简单地加载一堆资源(图像)然后当我更改主题时完成了这个:

if (Properties.Settings.Default.Theme == "Purple")
{
    foreach (var form in Application.OpenForms.Cast<Form>())
    {
        form.Icon = Properties.Resources.Purple;
    }

    Main f1 = (Main)Application.OpenForms["Main"];
    Settings f2 = (Settings)Application.OpenForms["Settings"];

    f1.btn_Exit.Image = Properties.Resources.EXIT_purple;
    f2.btn_SaveSettings.Image = Properties.Resources.SaveSettings_purple;

    f1.pictureBox1.Image = Properties.Resources.Preview_purple;
}

这大大增加了应用程序的大小,所以我想出了改变BackColor而不是加载另一个图像。我试图通过以下方式解决这个问题:

if (Properties.Settings.Default.Theme == "Purple")
{
    foreach (var form in Application.OpenForms.Cast<Form>())
    {
        form.Icon = Properties.Resources.Purple;
    }

    Main f1 = (Main)Application.OpenForms["Main"];
    Settings f2 = (Settings)Application.OpenForms["Settings"];

    f1.btn_Exit.BackColor = Color.FromArgb(164, 57, 226);
    f2.btn_SaveSettings.BackColor = Color.FromArgb(164, 57, 226);

    f2.pictureBox1.BackColor = Color.FromArgb(164, 57, 226);
}

因此,应该在所有正在运行的表单上更改表单图标,我已成功地这样做了,但是当涉及到按钮和图片框时,我没有看到任何结果,即按钮和图片框根本没有改变。除了表单图标,我还应更改位于btn_Exit表单上的Main,位于btn_SaveSettings表单上的Settings以及位于pictureBox1上的Settings形式也是如此。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果是我这样做,我会创建一个界面。此界面将引用不同控件的正确图片/图标/颜色。 “应用程序的设置”将包含正确主题的实例。每个表单我都会OnLoad更新所有必要的项目并称之为一天。它可能不是最优雅的解决方案,但是当你去添加更多主题时它会变得简单快捷。

这就是我的想法。我希望这可以解决我的问题。

public class Settings
{
    public static ITheme Theme {get {return theme;}{set theme = value}}

    theme = new DefaultTheme();
}
public interface ITheme
{
    public Color BackgroundColor {get;}
    public Color ButtonBackgroundColor { get;}

    //... etc
}
public class DefaultTheme : ITheme
{
    public Color BackgroundColor {get{ return Color.White;}}
    public Color ButtonBackgroundColor { get { return Color.Gray;}}

}
从这里你有两种方法。两者都要求你把一个事件“OnLoad”如果你有1或2个表格,可以把这个代码放在每个表格(不推荐),或者你可以使用类似扩展的方法。

这是第一种方法。将代码直接放在每一帧中。

public class SaveFrame : Frame
{
    public SaveFrame()
    {
        InitializeComponents();
    }
    public Form_OnLoad()
    {
        var theme = Settings.Theme;

        this.Background = theme.BackgroundColor;
        foreach(Button b in this.Controls)
        {
            if(b != null)
                b.Background = theme.ButtonBackgroundColor;
        }
    }

}

或创建扩展方法。并将其放入OnLoad

public static class FormExtensions
{
    public static void UpdateTheme(this Form form, ITheme theme)
    {
        form.Background = theme.BackgroundColor;
        foreach(Button b in form.Controls)
        {
            if(b != null)
                b.Background = theme.ButtonBackgroundColor;
        }

    }
}
public class SaveFrame : Form
{
    public SaveFrame()
    {
        InitializeComponents();
    }
    public Form_OnLoad()
    {
        this.UpdateTheme(Settings.Theme);
    }
}
public class MainFrame :Form
{
    public MainFrame()
    {
        InitializeComponents();
    }
    public Form_OnLoad()
    {
        this.UpdateTheme(Settings.Theme);
    }
}