123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<Control> _styleControls = new List<Control>();
- 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<string, string> 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;
- }
- }
- /// <summary>
- /// 注册单个控件
- /// </summary>
- /// <param name="ctl"></param>
- 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;
- }
- /// <summary>
- /// 注册控件及其子控件及其子控件的子控件...
- /// </summary>
- /// <param name="ctl"></param>
- public void RegisterControlRecursion(Control ctl, List<Control> 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<Type> GetDefaultFiltedControlTypes()
- {
- return new List<Type>
- {
- typeof(UIRadioButtonGroup),
- };
- }
- void DeregisterControl(Control ctl)
- {
- _styleControls.Remove(ctl);
- }
- }
- }
|