ThemeManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. namespace quick_color_picker
  8. {
  9. internal class ThemeManager
  10. {
  11. public static Color MainColorDark = Color.Black;
  12. public static Color BackColorDark = Color.FromArgb(32, 32, 32);
  13. public static Color SecondColorDark = Color.FromArgb(56, 56, 56);
  14. public static Color AccentColorDark = Color.FromArgb(73, 169, 207);
  15. private enum WindowCompositionAttribute
  16. {
  17. WCA_UNDEFINED = 0,
  18. WCA_NCRENDERING_ENABLED = 1,
  19. WCA_NCRENDERING_POLICY = 2,
  20. WCA_TRANSITIONS_FORCEDISABLED = 3,
  21. WCA_ALLOW_NCPAINT = 4,
  22. WCA_CAPTION_BUTTON_BOUNDS = 5,
  23. WCA_NONCLIENT_RTL_LAYOUT = 6,
  24. WCA_FORCE_ICONIC_REPRESENTATION = 7,
  25. WCA_EXTENDED_FRAME_BOUNDS = 8,
  26. WCA_HAS_ICONIC_BITMAP = 9,
  27. WCA_THEME_ATTRIBUTES = 10,
  28. WCA_NCRENDERING_EXILED = 11,
  29. WCA_NCADORNMENTINFO = 12,
  30. WCA_EXCLUDED_FROM_LIVEPREVIEW = 13,
  31. WCA_VIDEO_OVERLAY_ACTIVE = 14,
  32. WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15,
  33. WCA_DISALLOW_PEEK = 16,
  34. WCA_CLOAK = 17,
  35. WCA_CLOAKED = 18,
  36. WCA_ACCENT_POLICY = 19,
  37. WCA_FREEZE_REPRESENTATION = 20,
  38. WCA_EVER_UNCLOAKED = 21,
  39. WCA_VISUAL_OWNER = 22,
  40. WCA_HOLOGRAPHIC = 23,
  41. WCA_EXCLUDED_FROM_DDA = 24,
  42. WCA_PASSIVEUPDATEMODE = 25,
  43. WCA_USEDARKMODECOLORS = 26,
  44. WCA_LAST = 27
  45. };
  46. private struct WindowCompositionAttribData
  47. {
  48. public WindowCompositionAttribute Attribute;
  49. public IntPtr Data;
  50. public int SizeOfData;
  51. }
  52. [DllImport("uxtheme.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
  53. private static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);
  54. [DllImport("uxtheme.dll", EntryPoint = "#133", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
  55. private static extern bool AllowDarkModeForWindow(IntPtr hWnd, bool allow);
  56. [DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
  57. private static extern bool AllowDarkModeForApp(bool allow);
  58. [DllImport("user32.dll")]
  59. private static extern bool SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttribData data);
  60. private static void enableDarkTitlebar(IntPtr handle, bool dark)
  61. {
  62. AllowDarkModeForWindow(handle, dark);
  63. var sizeOfData = Marshal.SizeOf(dark);
  64. var dataPtr = Marshal.AllocHGlobal(sizeOfData);
  65. var data = new WindowCompositionAttribData
  66. {
  67. Attribute = WindowCompositionAttribute.WCA_USEDARKMODECOLORS,
  68. Data = dataPtr,
  69. SizeOfData = sizeOfData
  70. };
  71. SetWindowCompositionAttribute(handle, ref data);
  72. Marshal.FreeHGlobal(dataPtr);
  73. }
  74. public static void allowDarkModeForApp(bool dark)
  75. {
  76. AllowDarkModeForApp(dark);
  77. }
  78. public static void formHandleCreated(object sender, EventArgs e)
  79. {
  80. Form f = sender as Form;
  81. enableDarkTitlebar(f.Handle, true);
  82. }
  83. public static void setDarkModeToControl(IntPtr handle)
  84. {
  85. SetWindowTheme(handle, "DarkMode_Explorer", null);
  86. }
  87. public static bool isDarkTheme()
  88. {
  89. if (isWindows10())
  90. {
  91. try
  92. {
  93. string root = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  94. string str = Registry.GetValue(root, "AppsUseLightTheme", null).ToString();
  95. return (str == "0");
  96. }
  97. catch
  98. {
  99. return false;
  100. }
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. public static bool isWindows10()
  108. {
  109. try
  110. {
  111. var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
  112. string productName = (string)reg.GetValue("ProductName");
  113. return productName.StartsWith("Windows 10");
  114. }
  115. catch
  116. {
  117. return false;
  118. }
  119. }
  120. public static void PaintDarkGroupBox(object sender, PaintEventArgs p)
  121. {
  122. GroupBox box = (GroupBox)sender;
  123. SolidBrush brush = new SolidBrush(ThemeManager.SecondColorDark);
  124. Pen pen = new Pen(brush, 1);
  125. p.Graphics.Clear(ThemeManager.BackColorDark);
  126. p.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
  127. p.Graphics.DrawString(box.Text, box.Font, Brushes.White, -1, -3);
  128. p.Graphics.DrawLine(pen, 0, 16, 0, box.Height - 2);
  129. p.Graphics.DrawLine(pen, 0, 16, box.Width - 1, 16);
  130. p.Graphics.DrawLine(pen, box.Width - 1, 16, box.Width - 1, box.Height - 2);
  131. p.Graphics.DrawLine(pen, 0, box.Height - 2, box.Width - 1, box.Height - 2);
  132. }
  133. }
  134. }