using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Reflection; using System.Drawing; namespace Uni_Trend.MSO7000X.UserControls.Style { public class StyleManager { //静态只读单例 public readonly static StyleManager Instance = new StyleManager(); private List _styleControls = new List(); private IControlStyle _controlStyle; //属性 private IStyleValue _styleValue; public IStyleValue StyleValue { get => _styleValue; set { _styleValue = value; ChangeStyle(); } } public event EventHandler StyleChanged; private StyleManager() { StyleValue = StyleConfig.GetStyleValue(); _controlStyle = StyleConfig.GetControlStyle(); } private void ChangeStyle() { foreach(var styleCtl in _styleControls) { FreshControlStyle(styleCtl); } StyleChanged?.Invoke(this, new EventArgs()); } private void FreshControlStyle(Control ctl) { Dictionary filedDir = _controlStyle.GetControlStyleDir(ctl.GetType()); foreach (var filedPair in filedDir) { object? value = _styleValue.GetStyleFiledValue(filedPair.Value); SetProperty(ctl, filedPair.Key, value); } } private void SetProperty(Control ctl, string propertyString, object? value) { object? instance = ctl; PropertyInfo pInfo; var propertyPath = propertyString.Split(new char[] { '.'}); foreach(var p in propertyPath) { pInfo = instance.GetType().GetProperty(p); if(p.Equals(propertyPath.Last())) pInfo.SetValue(instance, value); else instance = pInfo.GetValue(instance); } } private void SpecialSetForFloatForm(FloatForm fForm) { //判定Form是否包含NavBarGroup;不包含-改变标题栏颜色; Boolean hasNbgFalg = false; foreach(var ctl in fForm.Controls) { if (ctl is NavBarGroup) hasNbgFalg = true; } if(!hasNbgFalg) fForm.HeadBackColor = Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleValue.DefaultTitleBackColor; //统一为FloatForm添加边框 if (fForm is BaseForm bForm) { bForm.BorderThickness = 2; bForm.BorderBackColor = bForm.HeadBackColor; } } /// /// 注册单个控件 /// /// public void RegisterControl(Control ctl) { if(!_styleControls.Contains(ctl)) { _styleControls.Add(ctl); FreshControlStyle(ctl); ctl.Disposed += (o, e) => DeregisterControl(ctl); } //统一给FloatForm进行设置 if (ctl is FloatForm fForm) SpecialSetForFloatForm(fForm); //统一给UestcSwitchButton进行设置 if (ctl is UestcSwitchButton usBtn) usBtn.MouseEnter += (_, _) => usBtn.UseAnimation = true; } /// /// 注册控件及其子控件及其子控件的子控件... /// /// public void RegisterControlRecursion(Control ctl, List filtedCtls = null) { RegisterControl(ctl); //递归结束:1)ctl是被过滤类型的;2)ctl.Controls中控件数为0; if (GetDefaultFiltedControlTypes().Contains(ctl.GetType())) return; foreach (Control childCtl in ctl.Controls) { if(filtedCtls == null || !filtedCtls.Contains(childCtl)) RegisterControlRecursion(childCtl); } } private List GetDefaultFiltedControlTypes() { return new List { typeof(UIRadioButtonGroup), }; } void DeregisterControl(Control ctl) { _styleControls.Remove(ctl); } } }