3

我已更改我的app.config文件以允许用户更改程序的配色方案。我可以弄清楚如何更改他们更改这些设置的表单的背景颜色:

Color colBackColor = Properties.Settings.Default.basicBackground;
this.BackColor = colBackColor;

但是如何更改所有表单的背景颜色?这就像我仍然想将我所有的表单传递给一个函数。我已经问过这个问题,有人告诉我使用该app.config文件。既然我已经这样做了,我是不是用错了?

4

1 回答 1

5

只是您需要一个基础表单,项目中的所有表单都必须从该基础表单继承

public class FormBase : Form {
   protected override void OnLoad(EventArgs e){
    Color colBackColor = Properties.Settings.Default.basicBackground;
    BackColor = colBackColor;
   }
}
//Then all other forms have to inherit from that FormBase instead of the standard Form
public class Form1 : FormBase {
  //...
}
public class Form2 : FormBase {
  //...
}

更新

public interface INotifyChangeStyle {
   void ChangeStyle();
}    
public class FormBase : Form, INotifyChangeStyle {
   protected override void OnLoad(EventArgs e){
      ChangeStyle();
   }
   public void ChangeStyle(){
      //Perform style changing here
      Color colBackColor = Properties.Settings.Default.basicBackground;
      BackColor = colBackColor;
      //--------
      foreach(var c in Controls.OfType<INotifyChangeStyle>()){
         c.ChangeStyle();
      }
   }
}
public class MyButton : Button, INotifyChangeStyle {
   public void ChangeStyle(){
      //Perform style changing here
      //....
      //--------
      foreach(var c in Controls.OfType<INotifyChangeStyle>()){
         c.ChangeStyle();
      }
   }
}
//...   the same for other control classes
于 2013-10-12T17:59:30.730 回答