ColorOverrider.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. namespace Optimizer
  9. {
  10. public sealed class ColorOverrider
  11. {
  12. public static event Action SystemColorsChanging;
  13. public static event Action SystemColorsChanged;
  14. private int[] OriginalColors { get; }
  15. private IReadOnlyDictionary<int, int> KnownOriginalColors;
  16. private int[] _colorTable;
  17. private readonly FieldInfo _colorTableField;
  18. private readonly PropertyInfo _threadDataProperty;
  19. public ColorOverrider()
  20. {
  21. // force init color table
  22. byte unused = SystemColors.Window.R;
  23. var systemDrawingAssembly = typeof(Color).Assembly;
  24. string colorTableField = "colorTable";
  25. _colorTableField = systemDrawingAssembly.GetType("System.Drawing.KnownColorTable")
  26. .GetField(colorTableField, BindingFlags.Static | BindingFlags.NonPublic);
  27. _colorTable = readColorTable();
  28. SystemEvents.UserPreferenceChanging += userPreferenceChanging;
  29. OriginalColors = _colorTable.ToArray();
  30. KnownOriginalColors = KnownColors.Cast<int>()
  31. .ToDictionary(i => i, i => OriginalColors[i]);
  32. _threadDataProperty = systemDrawingAssembly.GetType("System.Drawing.SafeNativeMethods")
  33. .GetNestedType("Gdip", BindingFlags.NonPublic)
  34. .GetProperty("ThreadData", BindingFlags.Static | BindingFlags.NonPublic);
  35. string systemBrushesKeyField = "SystemBrushesKey";
  36. SystemBrushesKey = typeof(SystemBrushes)
  37. .GetField(systemBrushesKeyField, BindingFlags.Static | BindingFlags.NonPublic)
  38. ?.GetValue(null);
  39. SystemPensKey = typeof(SystemPens)
  40. .GetField("SystemPensKey", BindingFlags.Static | BindingFlags.NonPublic)
  41. ?.GetValue(null);
  42. }
  43. private void userPreferenceChanging(object sender, UserPreferenceChangingEventArgs e)
  44. {
  45. if (e.Category != UserPreferenceCategory.Color)
  46. return;
  47. _colorTable = readColorTable();
  48. fireColorsChangedEvents();
  49. }
  50. private static void fireColorsChangedEvents()
  51. {
  52. SystemColorsChanging?.Invoke();
  53. SystemColorsChanged?.Invoke();
  54. }
  55. private int[] readColorTable() =>
  56. (int[])_colorTableField.GetValue(null);
  57. public void SetColor(KnownColor knownColor, int argb)
  58. {
  59. setColor(knownColor, argb);
  60. if (SystemBrushesKey != null)
  61. ThreadData[SystemBrushesKey] = null;
  62. if (SystemPensKey != null)
  63. ThreadData[SystemPensKey] = null;
  64. fireColorsChangedEvents();
  65. }
  66. private void setColor(KnownColor knownColor, int argb) =>
  67. _colorTable[(int)knownColor] = argb;
  68. public int GetOriginalColor(KnownColor knownColor) =>
  69. OriginalColors[(int)knownColor];
  70. public int GetColor(KnownColor knownColor)
  71. {
  72. if (!KnownColors.Contains(knownColor))
  73. throw new ArgumentException();
  74. return _colorTable[(int)knownColor];
  75. }
  76. public IReadOnlyDictionary<int, int> Save() =>
  77. KnownColors.Cast<int>()
  78. .ToDictionary(i => i, i => _colorTable[i]);
  79. public void Load(IReadOnlyDictionary<int, int> saved)
  80. {
  81. foreach (var color in KnownColors)
  82. {
  83. int v;
  84. var value = saved.TryGetValue((int)color, out v);
  85. setColor(color, v);
  86. }
  87. if (SystemBrushesKey != null)
  88. ThreadData[SystemBrushesKey] = null;
  89. if (SystemPensKey != null)
  90. ThreadData[SystemPensKey] = null;
  91. fireColorsChangedEvents();
  92. }
  93. public void Reset(KnownColor color) =>
  94. SetColor(color, OriginalColors[(int)color]);
  95. public void ResetAll() =>
  96. Load(KnownOriginalColors);
  97. private IDictionary ThreadData =>
  98. (IDictionary)_threadDataProperty.GetValue(null, null);
  99. private object SystemBrushesKey { get; }
  100. private object SystemPensKey { get; }
  101. public readonly HashSet<KnownColor> KnownColors = new HashSet<KnownColor>(
  102. new[]
  103. {
  104. SystemColors.Control,
  105. SystemColors.ControlText,
  106. SystemColors.ButtonFace, // menu gradient
  107. SystemColors.ButtonShadow, // menu border
  108. SystemColors.Window,
  109. SystemColors.WindowText,
  110. SystemColors.GrayText,
  111. SystemColors.HotTrack,
  112. SystemColors.Highlight,
  113. SystemColors.HighlightText,
  114. SystemColors.ActiveCaption,
  115. SystemColors.GradientActiveCaption,
  116. SystemColors.InactiveCaption,
  117. SystemColors.GradientInactiveCaption,
  118. SystemColors.ActiveBorder
  119. }.Select(_ => _.ToKnownColor())
  120. );
  121. }
  122. }