StyleManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Reflection;
  8. using System.Drawing;
  9. namespace Uni_Trend.MSO7000X.UserControls.Style
  10. {
  11. public class StyleManager
  12. {
  13. //静态只读单例
  14. public readonly static StyleManager Instance = new StyleManager();
  15. private List<Control> _styleControls = new List<Control>();
  16. private IControlStyle _controlStyle;
  17. //属性
  18. private IStyleValue _styleValue;
  19. public IStyleValue StyleValue
  20. {
  21. get => _styleValue;
  22. set
  23. {
  24. _styleValue = value;
  25. ChangeStyle();
  26. }
  27. }
  28. public event EventHandler StyleChanged;
  29. private StyleManager()
  30. {
  31. StyleValue = StyleConfig.GetStyleValue();
  32. _controlStyle = StyleConfig.GetControlStyle();
  33. }
  34. private void ChangeStyle()
  35. {
  36. foreach(var styleCtl in _styleControls)
  37. {
  38. FreshControlStyle(styleCtl);
  39. }
  40. StyleChanged?.Invoke(this, new EventArgs());
  41. }
  42. private void FreshControlStyle(Control ctl)
  43. {
  44. Dictionary<string, string> filedDir = _controlStyle.GetControlStyleDir(ctl.GetType());
  45. foreach (var filedPair in filedDir)
  46. {
  47. object? value = _styleValue.GetStyleFiledValue(filedPair.Value);
  48. SetProperty(ctl, filedPair.Key, value);
  49. }
  50. }
  51. private void SetProperty(Control ctl, string propertyString, object? value)
  52. {
  53. object? instance = ctl;
  54. PropertyInfo pInfo;
  55. var propertyPath = propertyString.Split(new char[] { '.'});
  56. foreach(var p in propertyPath)
  57. {
  58. pInfo = instance.GetType().GetProperty(p);
  59. if(p.Equals(propertyPath.Last()))
  60. pInfo.SetValue(instance, value);
  61. else
  62. instance = pInfo.GetValue(instance);
  63. }
  64. }
  65. private void SpecialSetForFloatForm(FloatForm fForm)
  66. {
  67. //判定Form是否包含NavBarGroup;不包含-改变标题栏颜色;
  68. Boolean hasNbgFalg = false;
  69. foreach(var ctl in fForm.Controls)
  70. {
  71. if (ctl is NavBarGroup)
  72. hasNbgFalg = true;
  73. }
  74. if(!hasNbgFalg)
  75. fForm.HeadBackColor = Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleValue.DefaultTitleBackColor;
  76. //统一为FloatForm添加边框
  77. if (fForm is BaseForm bForm)
  78. {
  79. bForm.BorderThickness = 2;
  80. bForm.BorderBackColor = bForm.HeadBackColor;
  81. }
  82. }
  83. /// <summary>
  84. /// 注册单个控件
  85. /// </summary>
  86. /// <param name="ctl"></param>
  87. public void RegisterControl(Control ctl)
  88. {
  89. if(!_styleControls.Contains(ctl))
  90. {
  91. _styleControls.Add(ctl);
  92. FreshControlStyle(ctl);
  93. ctl.Disposed += (o, e) => DeregisterControl(ctl);
  94. }
  95. //统一给FloatForm进行设置
  96. if (ctl is FloatForm fForm)
  97. SpecialSetForFloatForm(fForm);
  98. //统一给UestcSwitchButton进行设置
  99. if (ctl is UestcSwitchButton usBtn)
  100. usBtn.MouseEnter += (_, _) => usBtn.UseAnimation = true;
  101. }
  102. /// <summary>
  103. /// 注册控件及其子控件及其子控件的子控件...
  104. /// </summary>
  105. /// <param name="ctl"></param>
  106. public void RegisterControlRecursion(Control ctl, List<Control> filtedCtls = null)
  107. {
  108. RegisterControl(ctl);
  109. //递归结束:1)ctl是被过滤类型的;2)ctl.Controls中控件数为0;
  110. if (GetDefaultFiltedControlTypes().Contains(ctl.GetType()))
  111. return;
  112. foreach (Control childCtl in ctl.Controls)
  113. {
  114. if(filtedCtls == null || !filtedCtls.Contains(childCtl))
  115. RegisterControlRecursion(childCtl);
  116. }
  117. }
  118. private List<Type> GetDefaultFiltedControlTypes()
  119. {
  120. return new List<Type>
  121. {
  122. typeof(UIRadioButtonGroup),
  123. };
  124. }
  125. void DeregisterControl(Control ctl)
  126. {
  127. _styleControls.Remove(ctl);
  128. }
  129. }
  130. }