ColorOverrider.cs 5.0 KB

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