SystemParameters2.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using System.Windows;
  8. using System.Windows.Media;
  9. using Standard;
  10. namespace Microsoft.Windows.Shell;
  11. [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
  12. public class SystemParameters2 : INotifyPropertyChanged
  13. {
  14. private void _InitializeIsGlassEnabled()
  15. {
  16. this.IsGlassEnabled = NativeMethods.DwmIsCompositionEnabled();
  17. }
  18. private void _UpdateIsGlassEnabled(IntPtr wParam, IntPtr lParam)
  19. {
  20. this._InitializeIsGlassEnabled();
  21. }
  22. private void _InitializeGlassColor()
  23. {
  24. uint num;
  25. bool flag;
  26. NativeMethods.DwmGetColorizationColor(out num, out flag);
  27. num |= (flag ? 4278190080u : 0u);
  28. this.WindowGlassColor = Utility.ColorFromArgbDword(num);
  29. SolidColorBrush solidColorBrush = new SolidColorBrush(this.WindowGlassColor);
  30. solidColorBrush.Freeze();
  31. this.WindowGlassBrush = solidColorBrush;
  32. }
  33. private void _UpdateGlassColor(IntPtr wParam, IntPtr lParam)
  34. {
  35. bool flag = lParam != IntPtr.Zero;
  36. uint num = (uint) ((int) wParam.ToInt64());
  37. num |= (flag ? 4278190080u : 0u);
  38. this.WindowGlassColor = Utility.ColorFromArgbDword(num);
  39. SolidColorBrush solidColorBrush = new SolidColorBrush(this.WindowGlassColor);
  40. solidColorBrush.Freeze();
  41. this.WindowGlassBrush = solidColorBrush;
  42. }
  43. private void _InitializeCaptionHeight()
  44. {
  45. Point devicePoint = new Point(0.0, (double) NativeMethods.GetSystemMetrics(SM.CYCAPTION));
  46. this.WindowCaptionHeight = DpiHelper.DevicePixelsToLogical(devicePoint).Y;
  47. }
  48. private void _UpdateCaptionHeight(IntPtr wParam, IntPtr lParam)
  49. {
  50. this._InitializeCaptionHeight();
  51. }
  52. private void _InitializeWindowResizeBorderThickness()
  53. {
  54. Size deviceSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXFRAME), (double) NativeMethods.GetSystemMetrics(SM.CYFRAME));
  55. Size size = DpiHelper.DeviceSizeToLogical(deviceSize);
  56. this.WindowResizeBorderThickness = new Thickness(size.Width, size.Height, size.Width, size.Height);
  57. }
  58. private void _UpdateWindowResizeBorderThickness(IntPtr wParam, IntPtr lParam)
  59. {
  60. this._InitializeWindowResizeBorderThickness();
  61. }
  62. private void _InitializeWindowNonClientFrameThickness()
  63. {
  64. Size deviceSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXFRAME), (double) NativeMethods.GetSystemMetrics(SM.CYFRAME));
  65. Size size = DpiHelper.DeviceSizeToLogical(deviceSize);
  66. int systemMetrics = NativeMethods.GetSystemMetrics(SM.CYCAPTION);
  67. double y = DpiHelper.DevicePixelsToLogical(new Point(0.0, (double) systemMetrics)).Y;
  68. this.WindowNonClientFrameThickness = new Thickness(size.Width, size.Height + y, size.Width, size.Height);
  69. }
  70. private void _UpdateWindowNonClientFrameThickness(IntPtr wParam, IntPtr lParam)
  71. {
  72. this._InitializeWindowNonClientFrameThickness();
  73. }
  74. private void _InitializeSmallIconSize()
  75. {
  76. this.SmallIconSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXSMICON), (double) NativeMethods.GetSystemMetrics(SM.CYSMICON));
  77. }
  78. private void _UpdateSmallIconSize(IntPtr wParam, IntPtr lParam)
  79. {
  80. this._InitializeSmallIconSize();
  81. }
  82. private void _LegacyInitializeCaptionButtonLocation()
  83. {
  84. int systemMetrics = NativeMethods.GetSystemMetrics(SM.CXSIZE);
  85. int systemMetrics2 = NativeMethods.GetSystemMetrics(SM.CYSIZE);
  86. int num = NativeMethods.GetSystemMetrics(SM.CXFRAME) + NativeMethods.GetSystemMetrics(SM.CXEDGE);
  87. int num2 = NativeMethods.GetSystemMetrics(SM.CYFRAME) + NativeMethods.GetSystemMetrics(SM.CYEDGE);
  88. Rect windowCaptionButtonsLocation = new Rect(0.0, 0.0, (double) (systemMetrics * 3), (double) systemMetrics2);
  89. windowCaptionButtonsLocation.Offset((double) (-(double) num) - windowCaptionButtonsLocation.Width, (double) num2);
  90. this.WindowCaptionButtonsLocation = windowCaptionButtonsLocation;
  91. }
  92. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  93. private void _InitializeCaptionButtonLocation()
  94. {
  95. if (!Utility.IsOSVistaOrNewer || !NativeMethods.IsThemeActive())
  96. {
  97. this._LegacyInitializeCaptionButtonLocation();
  98. return;
  99. }
  100. TITLEBARINFOEX titlebarinfoex = new TITLEBARINFOEX
  101. {
  102. cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX))
  103. };
  104. IntPtr intPtr = Marshal.AllocHGlobal(titlebarinfoex.cbSize);
  105. try
  106. {
  107. Marshal.StructureToPtr(titlebarinfoex, intPtr, false);
  108. NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.SHOW);
  109. NativeMethods.SendMessage(this._messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, intPtr);
  110. titlebarinfoex = (TITLEBARINFOEX) Marshal.PtrToStructure(intPtr, typeof(TITLEBARINFOEX));
  111. }
  112. finally
  113. {
  114. NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.HIDE);
  115. Utility.SafeFreeHGlobal(ref intPtr);
  116. }
  117. RECT rect = RECT.Union(titlebarinfoex.rgrect_CloseButton, titlebarinfoex.rgrect_MinimizeButton);
  118. RECT windowRect = NativeMethods.GetWindowRect(this._messageHwnd.Handle);
  119. Rect deviceRectangle = new Rect((double) (rect.Left - windowRect.Width - windowRect.Left), (double) (rect.Top - windowRect.Top), (double) rect.Width, (double) rect.Height);
  120. Rect windowCaptionButtonsLocation = DpiHelper.DeviceRectToLogical(deviceRectangle);
  121. this.WindowCaptionButtonsLocation = windowCaptionButtonsLocation;
  122. }
  123. private void _UpdateCaptionButtonLocation(IntPtr wParam, IntPtr lParam)
  124. {
  125. this._InitializeCaptionButtonLocation();
  126. }
  127. private void _InitializeHighContrast()
  128. {
  129. this.HighContrast = ((NativeMethods.SystemParameterInfo_GetHIGHCONTRAST().dwFlags & HCF.HIGHCONTRASTON) != (HCF) 0);
  130. }
  131. private void _UpdateHighContrast(IntPtr wParam, IntPtr lParam)
  132. {
  133. this._InitializeHighContrast();
  134. }
  135. private void _InitializeThemeInfo()
  136. {
  137. if (!NativeMethods.IsThemeActive())
  138. {
  139. this.UxThemeName = "Classic";
  140. this.UxThemeColor = "";
  141. return;
  142. }
  143. string path;
  144. string uxThemeColor;
  145. string text;
  146. NativeMethods.GetCurrentThemeName(out path, out uxThemeColor, out text);
  147. this.UxThemeName = Path.GetFileNameWithoutExtension(path);
  148. this.UxThemeColor = uxThemeColor;
  149. }
  150. private void _UpdateThemeInfo(IntPtr wParam, IntPtr lParam)
  151. {
  152. this._InitializeThemeInfo();
  153. }
  154. private void _InitializeWindowCornerRadius()
  155. {
  156. CornerRadius windowCornerRadius = default(CornerRadius);
  157. string a;
  158. if ((a = this.UxThemeName.ToUpperInvariant()) != null)
  159. {
  160. if (a == "LUNA")
  161. {
  162. windowCornerRadius = new CornerRadius(6.0, 6.0, 0.0, 0.0);
  163. goto IL_E6;
  164. }
  165. if (!(a == "AERO"))
  166. {
  167. if (!(a == "CLASSIC") && !(a == "ZUNE") && !(a == "ROYALE"))
  168. {
  169. }
  170. }
  171. else
  172. {
  173. if (NativeMethods.DwmIsCompositionEnabled())
  174. {
  175. windowCornerRadius = new CornerRadius(8.0);
  176. goto IL_E6;
  177. }
  178. windowCornerRadius = new CornerRadius(6.0, 6.0, 0.0, 0.0);
  179. goto IL_E6;
  180. }
  181. }
  182. windowCornerRadius = new CornerRadius(0.0);
  183. IL_E6:
  184. this.WindowCornerRadius = windowCornerRadius;
  185. }
  186. private void _UpdateWindowCornerRadius(IntPtr wParam, IntPtr lParam)
  187. {
  188. this._InitializeWindowCornerRadius();
  189. }
  190. private SystemParameters2()
  191. {
  192. this._messageHwnd = new MessageWindow((CS) 0u, WS.DISABLED | WS.BORDER | WS.DLGFRAME | WS.SYSMENU | WS.THICKFRAME | WS.GROUP | WS.TABSTOP, WS_EX.None, new Rect(-16000.0, -16000.0, 100.0, 100.0), "", new WndProc(this._WndProc));
  193. this._messageHwnd.Dispatcher.ShutdownStarted += delegate (object sender, EventArgs e)
  194. {
  195. Utility.SafeDispose<MessageWindow>(ref this._messageHwnd);
  196. };
  197. this._InitializeIsGlassEnabled();
  198. this._InitializeGlassColor();
  199. this._InitializeCaptionHeight();
  200. this._InitializeWindowNonClientFrameThickness();
  201. this._InitializeWindowResizeBorderThickness();
  202. this._InitializeCaptionButtonLocation();
  203. this._InitializeSmallIconSize();
  204. this._InitializeHighContrast();
  205. this._InitializeThemeInfo();
  206. this._InitializeWindowCornerRadius();
  207. this._UpdateTable = new Dictionary<WM, List<SystemParameters2._SystemMetricUpdate>>
  208. {
  209. {
  210. WM.THEMECHANGED,
  211. new List<SystemParameters2._SystemMetricUpdate>
  212. {
  213. new SystemParameters2._SystemMetricUpdate(this._UpdateThemeInfo),
  214. new SystemParameters2._SystemMetricUpdate(this._UpdateHighContrast),
  215. new SystemParameters2._SystemMetricUpdate(this._UpdateWindowCornerRadius),
  216. new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionButtonLocation)
  217. }
  218. },
  219. {
  220. WM.WININICHANGE,
  221. new List<SystemParameters2._SystemMetricUpdate>
  222. {
  223. new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionHeight),
  224. new SystemParameters2._SystemMetricUpdate(this._UpdateWindowResizeBorderThickness),
  225. new SystemParameters2._SystemMetricUpdate(this._UpdateSmallIconSize),
  226. new SystemParameters2._SystemMetricUpdate(this._UpdateHighContrast),
  227. new SystemParameters2._SystemMetricUpdate(this._UpdateWindowNonClientFrameThickness),
  228. new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionButtonLocation)
  229. }
  230. },
  231. {
  232. WM.DWMNCRENDERINGCHANGED,
  233. new List<SystemParameters2._SystemMetricUpdate>
  234. {
  235. new SystemParameters2._SystemMetricUpdate(this._UpdateIsGlassEnabled)
  236. }
  237. },
  238. {
  239. WM.DWMCOMPOSITIONCHANGED,
  240. new List<SystemParameters2._SystemMetricUpdate>
  241. {
  242. new SystemParameters2._SystemMetricUpdate(this._UpdateIsGlassEnabled)
  243. }
  244. },
  245. {
  246. WM.DWMCOLORIZATIONCOLORCHANGED,
  247. new List<SystemParameters2._SystemMetricUpdate>
  248. {
  249. new SystemParameters2._SystemMetricUpdate(this._UpdateGlassColor)
  250. }
  251. }
  252. };
  253. }
  254. public static SystemParameters2 Current
  255. {
  256. get
  257. {
  258. if (SystemParameters2._threadLocalSingleton == null)
  259. {
  260. SystemParameters2._threadLocalSingleton = new SystemParameters2();
  261. }
  262. return SystemParameters2._threadLocalSingleton;
  263. }
  264. }
  265. private IntPtr _WndProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam)
  266. {
  267. List<SystemParameters2._SystemMetricUpdate> list;
  268. if (this._UpdateTable != null && this._UpdateTable.TryGetValue(msg, out list))
  269. {
  270. foreach (SystemParameters2._SystemMetricUpdate systemMetricUpdate in list)
  271. {
  272. systemMetricUpdate(wParam, lParam);
  273. }
  274. }
  275. return NativeMethods.DefWindowProc(hwnd, msg, wParam, lParam);
  276. }
  277. public bool IsGlassEnabled
  278. {
  279. get
  280. {
  281. return NativeMethods.DwmIsCompositionEnabled();
  282. }
  283. private set
  284. {
  285. if (value != this._isGlassEnabled)
  286. {
  287. this._isGlassEnabled = value;
  288. this._NotifyPropertyChanged("IsGlassEnabled");
  289. }
  290. }
  291. }
  292. public Color WindowGlassColor
  293. {
  294. get
  295. {
  296. return this._glassColor;
  297. }
  298. private set
  299. {
  300. if (value != this._glassColor)
  301. {
  302. this._glassColor = value;
  303. this._NotifyPropertyChanged("WindowGlassColor");
  304. }
  305. }
  306. }
  307. public SolidColorBrush WindowGlassBrush
  308. {
  309. get
  310. {
  311. return this._glassColorBrush;
  312. }
  313. private set
  314. {
  315. if (this._glassColorBrush == null || value.Color != this._glassColorBrush.Color)
  316. {
  317. this._glassColorBrush = value;
  318. this._NotifyPropertyChanged("WindowGlassBrush");
  319. }
  320. }
  321. }
  322. public Thickness WindowResizeBorderThickness
  323. {
  324. get
  325. {
  326. return this._windowResizeBorderThickness;
  327. }
  328. private set
  329. {
  330. if (value != this._windowResizeBorderThickness)
  331. {
  332. this._windowResizeBorderThickness = value;
  333. this._NotifyPropertyChanged("WindowResizeBorderThickness");
  334. }
  335. }
  336. }
  337. public Thickness WindowNonClientFrameThickness
  338. {
  339. get
  340. {
  341. return this._windowNonClientFrameThickness;
  342. }
  343. private set
  344. {
  345. if (value != this._windowNonClientFrameThickness)
  346. {
  347. this._windowNonClientFrameThickness = value;
  348. this._NotifyPropertyChanged("WindowNonClientFrameThickness");
  349. }
  350. }
  351. }
  352. public double WindowCaptionHeight
  353. {
  354. get
  355. {
  356. return this._captionHeight;
  357. }
  358. private set
  359. {
  360. if (value != this._captionHeight)
  361. {
  362. this._captionHeight = value;
  363. this._NotifyPropertyChanged("WindowCaptionHeight");
  364. }
  365. }
  366. }
  367. public Size SmallIconSize
  368. {
  369. get
  370. {
  371. return new Size(this._smallIconSize.Width, this._smallIconSize.Height);
  372. }
  373. private set
  374. {
  375. if (value != this._smallIconSize)
  376. {
  377. this._smallIconSize = value;
  378. this._NotifyPropertyChanged("SmallIconSize");
  379. }
  380. }
  381. }
  382. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ux")]
  383. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ux")]
  384. public string UxThemeName
  385. {
  386. get
  387. {
  388. return this._uxThemeName;
  389. }
  390. private set
  391. {
  392. if (value != this._uxThemeName)
  393. {
  394. this._uxThemeName = value;
  395. this._NotifyPropertyChanged("UxThemeName");
  396. }
  397. }
  398. }
  399. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ux")]
  400. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ux")]
  401. public string UxThemeColor
  402. {
  403. get
  404. {
  405. return this._uxThemeColor;
  406. }
  407. private set
  408. {
  409. if (value != this._uxThemeColor)
  410. {
  411. this._uxThemeColor = value;
  412. this._NotifyPropertyChanged("UxThemeColor");
  413. }
  414. }
  415. }
  416. public bool HighContrast
  417. {
  418. get
  419. {
  420. return this._isHighContrast;
  421. }
  422. private set
  423. {
  424. if (value != this._isHighContrast)
  425. {
  426. this._isHighContrast = value;
  427. this._NotifyPropertyChanged("HighContrast");
  428. }
  429. }
  430. }
  431. public CornerRadius WindowCornerRadius
  432. {
  433. get
  434. {
  435. return this._windowCornerRadius;
  436. }
  437. private set
  438. {
  439. if (value != this._windowCornerRadius)
  440. {
  441. this._windowCornerRadius = value;
  442. this._NotifyPropertyChanged("WindowCornerRadius");
  443. }
  444. }
  445. }
  446. public Rect WindowCaptionButtonsLocation
  447. {
  448. get
  449. {
  450. return this._captionButtonLocation;
  451. }
  452. private set
  453. {
  454. if (value != this._captionButtonLocation)
  455. {
  456. this._captionButtonLocation = value;
  457. this._NotifyPropertyChanged("WindowCaptionButtonsLocation");
  458. }
  459. }
  460. }
  461. private void _NotifyPropertyChanged(string propertyName)
  462. {
  463. PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
  464. if (propertyChanged != null)
  465. {
  466. propertyChanged(this, new PropertyChangedEventArgs(propertyName));
  467. }
  468. }
  469. public event PropertyChangedEventHandler PropertyChanged;
  470. [ThreadStatic]
  471. private static SystemParameters2 _threadLocalSingleton;
  472. private MessageWindow _messageHwnd;
  473. private bool _isGlassEnabled;
  474. private Color _glassColor;
  475. private SolidColorBrush _glassColorBrush;
  476. private Thickness _windowResizeBorderThickness;
  477. private Thickness _windowNonClientFrameThickness;
  478. private double _captionHeight;
  479. private Size _smallIconSize;
  480. private string _uxThemeName;
  481. private string _uxThemeColor;
  482. private bool _isHighContrast;
  483. private CornerRadius _windowCornerRadius;
  484. private Rect _captionButtonLocation;
  485. private readonly Dictionary<WM, List<SystemParameters2._SystemMetricUpdate>> _UpdateTable;
  486. private delegate void _SystemMetricUpdate(IntPtr wParam, IntPtr lParam);
  487. }