123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics.CodeAnalysis;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows;
- using System.Windows.Media;
- using Standard;
- namespace Microsoft.Windows.Shell;
- [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
- public class SystemParameters2 : INotifyPropertyChanged
- {
- private void _InitializeIsGlassEnabled()
- {
- this.IsGlassEnabled = NativeMethods.DwmIsCompositionEnabled();
- }
- private void _UpdateIsGlassEnabled(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeIsGlassEnabled();
- }
- private void _InitializeGlassColor()
- {
- uint num;
- bool flag;
- NativeMethods.DwmGetColorizationColor(out num, out flag);
- num |= (flag ? 4278190080u : 0u);
- this.WindowGlassColor = Utility.ColorFromArgbDword(num);
- SolidColorBrush solidColorBrush = new SolidColorBrush(this.WindowGlassColor);
- solidColorBrush.Freeze();
- this.WindowGlassBrush = solidColorBrush;
- }
- private void _UpdateGlassColor(IntPtr wParam, IntPtr lParam)
- {
- bool flag = lParam != IntPtr.Zero;
- uint num = (uint) ((int) wParam.ToInt64());
- num |= (flag ? 4278190080u : 0u);
- this.WindowGlassColor = Utility.ColorFromArgbDword(num);
- SolidColorBrush solidColorBrush = new SolidColorBrush(this.WindowGlassColor);
- solidColorBrush.Freeze();
- this.WindowGlassBrush = solidColorBrush;
- }
- private void _InitializeCaptionHeight()
- {
- Point devicePoint = new Point(0.0, (double) NativeMethods.GetSystemMetrics(SM.CYCAPTION));
- this.WindowCaptionHeight = DpiHelper.DevicePixelsToLogical(devicePoint).Y;
- }
- private void _UpdateCaptionHeight(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeCaptionHeight();
- }
- private void _InitializeWindowResizeBorderThickness()
- {
- Size deviceSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXFRAME), (double) NativeMethods.GetSystemMetrics(SM.CYFRAME));
- Size size = DpiHelper.DeviceSizeToLogical(deviceSize);
- this.WindowResizeBorderThickness = new Thickness(size.Width, size.Height, size.Width, size.Height);
- }
- private void _UpdateWindowResizeBorderThickness(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeWindowResizeBorderThickness();
- }
- private void _InitializeWindowNonClientFrameThickness()
- {
- Size deviceSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXFRAME), (double) NativeMethods.GetSystemMetrics(SM.CYFRAME));
- Size size = DpiHelper.DeviceSizeToLogical(deviceSize);
- int systemMetrics = NativeMethods.GetSystemMetrics(SM.CYCAPTION);
- double y = DpiHelper.DevicePixelsToLogical(new Point(0.0, (double) systemMetrics)).Y;
- this.WindowNonClientFrameThickness = new Thickness(size.Width, size.Height + y, size.Width, size.Height);
- }
- private void _UpdateWindowNonClientFrameThickness(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeWindowNonClientFrameThickness();
- }
- private void _InitializeSmallIconSize()
- {
- this.SmallIconSize = new Size((double) NativeMethods.GetSystemMetrics(SM.CXSMICON), (double) NativeMethods.GetSystemMetrics(SM.CYSMICON));
- }
- private void _UpdateSmallIconSize(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeSmallIconSize();
- }
- private void _LegacyInitializeCaptionButtonLocation()
- {
- int systemMetrics = NativeMethods.GetSystemMetrics(SM.CXSIZE);
- int systemMetrics2 = NativeMethods.GetSystemMetrics(SM.CYSIZE);
- int num = NativeMethods.GetSystemMetrics(SM.CXFRAME) + NativeMethods.GetSystemMetrics(SM.CXEDGE);
- int num2 = NativeMethods.GetSystemMetrics(SM.CYFRAME) + NativeMethods.GetSystemMetrics(SM.CYEDGE);
- Rect windowCaptionButtonsLocation = new Rect(0.0, 0.0, (double) (systemMetrics * 3), (double) systemMetrics2);
- windowCaptionButtonsLocation.Offset((double) (-(double) num) - windowCaptionButtonsLocation.Width, (double) num2);
- this.WindowCaptionButtonsLocation = windowCaptionButtonsLocation;
- }
- [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
- private void _InitializeCaptionButtonLocation()
- {
- if (!Utility.IsOSVistaOrNewer || !NativeMethods.IsThemeActive())
- {
- this._LegacyInitializeCaptionButtonLocation();
- return;
- }
- TITLEBARINFOEX titlebarinfoex = new TITLEBARINFOEX
- {
- cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX))
- };
- IntPtr intPtr = Marshal.AllocHGlobal(titlebarinfoex.cbSize);
- try
- {
- Marshal.StructureToPtr(titlebarinfoex, intPtr, false);
- NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.SHOW);
- NativeMethods.SendMessage(this._messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, intPtr);
- titlebarinfoex = (TITLEBARINFOEX) Marshal.PtrToStructure(intPtr, typeof(TITLEBARINFOEX));
- }
- finally
- {
- NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.HIDE);
- Utility.SafeFreeHGlobal(ref intPtr);
- }
- RECT rect = RECT.Union(titlebarinfoex.rgrect_CloseButton, titlebarinfoex.rgrect_MinimizeButton);
- RECT windowRect = NativeMethods.GetWindowRect(this._messageHwnd.Handle);
- Rect deviceRectangle = new Rect((double) (rect.Left - windowRect.Width - windowRect.Left), (double) (rect.Top - windowRect.Top), (double) rect.Width, (double) rect.Height);
- Rect windowCaptionButtonsLocation = DpiHelper.DeviceRectToLogical(deviceRectangle);
- this.WindowCaptionButtonsLocation = windowCaptionButtonsLocation;
- }
- private void _UpdateCaptionButtonLocation(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeCaptionButtonLocation();
- }
- private void _InitializeHighContrast()
- {
- this.HighContrast = ((NativeMethods.SystemParameterInfo_GetHIGHCONTRAST().dwFlags & HCF.HIGHCONTRASTON) != (HCF) 0);
- }
- private void _UpdateHighContrast(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeHighContrast();
- }
- private void _InitializeThemeInfo()
- {
- if (!NativeMethods.IsThemeActive())
- {
- this.UxThemeName = "Classic";
- this.UxThemeColor = "";
- return;
- }
- string path;
- string uxThemeColor;
- string text;
- NativeMethods.GetCurrentThemeName(out path, out uxThemeColor, out text);
- this.UxThemeName = Path.GetFileNameWithoutExtension(path);
- this.UxThemeColor = uxThemeColor;
- }
- private void _UpdateThemeInfo(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeThemeInfo();
- }
- private void _InitializeWindowCornerRadius()
- {
- CornerRadius windowCornerRadius = default(CornerRadius);
- string a;
- if ((a = this.UxThemeName.ToUpperInvariant()) != null)
- {
- if (a == "LUNA")
- {
- windowCornerRadius = new CornerRadius(6.0, 6.0, 0.0, 0.0);
- goto IL_E6;
- }
- if (!(a == "AERO"))
- {
- if (!(a == "CLASSIC") && !(a == "ZUNE") && !(a == "ROYALE"))
- {
- }
- }
- else
- {
- if (NativeMethods.DwmIsCompositionEnabled())
- {
- windowCornerRadius = new CornerRadius(8.0);
- goto IL_E6;
- }
- windowCornerRadius = new CornerRadius(6.0, 6.0, 0.0, 0.0);
- goto IL_E6;
- }
- }
- windowCornerRadius = new CornerRadius(0.0);
- IL_E6:
- this.WindowCornerRadius = windowCornerRadius;
- }
- private void _UpdateWindowCornerRadius(IntPtr wParam, IntPtr lParam)
- {
- this._InitializeWindowCornerRadius();
- }
- private SystemParameters2()
- {
- 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));
- this._messageHwnd.Dispatcher.ShutdownStarted += delegate (object sender, EventArgs e)
- {
- Utility.SafeDispose<MessageWindow>(ref this._messageHwnd);
- };
- this._InitializeIsGlassEnabled();
- this._InitializeGlassColor();
- this._InitializeCaptionHeight();
- this._InitializeWindowNonClientFrameThickness();
- this._InitializeWindowResizeBorderThickness();
- this._InitializeCaptionButtonLocation();
- this._InitializeSmallIconSize();
- this._InitializeHighContrast();
- this._InitializeThemeInfo();
- this._InitializeWindowCornerRadius();
- this._UpdateTable = new Dictionary<WM, List<SystemParameters2._SystemMetricUpdate>>
- {
- {
- WM.THEMECHANGED,
- new List<SystemParameters2._SystemMetricUpdate>
- {
- new SystemParameters2._SystemMetricUpdate(this._UpdateThemeInfo),
- new SystemParameters2._SystemMetricUpdate(this._UpdateHighContrast),
- new SystemParameters2._SystemMetricUpdate(this._UpdateWindowCornerRadius),
- new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionButtonLocation)
- }
- },
- {
- WM.WININICHANGE,
- new List<SystemParameters2._SystemMetricUpdate>
- {
- new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionHeight),
- new SystemParameters2._SystemMetricUpdate(this._UpdateWindowResizeBorderThickness),
- new SystemParameters2._SystemMetricUpdate(this._UpdateSmallIconSize),
- new SystemParameters2._SystemMetricUpdate(this._UpdateHighContrast),
- new SystemParameters2._SystemMetricUpdate(this._UpdateWindowNonClientFrameThickness),
- new SystemParameters2._SystemMetricUpdate(this._UpdateCaptionButtonLocation)
- }
- },
- {
- WM.DWMNCRENDERINGCHANGED,
- new List<SystemParameters2._SystemMetricUpdate>
- {
- new SystemParameters2._SystemMetricUpdate(this._UpdateIsGlassEnabled)
- }
- },
- {
- WM.DWMCOMPOSITIONCHANGED,
- new List<SystemParameters2._SystemMetricUpdate>
- {
- new SystemParameters2._SystemMetricUpdate(this._UpdateIsGlassEnabled)
- }
- },
- {
- WM.DWMCOLORIZATIONCOLORCHANGED,
- new List<SystemParameters2._SystemMetricUpdate>
- {
- new SystemParameters2._SystemMetricUpdate(this._UpdateGlassColor)
- }
- }
- };
- }
- public static SystemParameters2 Current
- {
- get
- {
- if (SystemParameters2._threadLocalSingleton == null)
- {
- SystemParameters2._threadLocalSingleton = new SystemParameters2();
- }
- return SystemParameters2._threadLocalSingleton;
- }
- }
- private IntPtr _WndProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam)
- {
- List<SystemParameters2._SystemMetricUpdate> list;
- if (this._UpdateTable != null && this._UpdateTable.TryGetValue(msg, out list))
- {
- foreach (SystemParameters2._SystemMetricUpdate systemMetricUpdate in list)
- {
- systemMetricUpdate(wParam, lParam);
- }
- }
- return NativeMethods.DefWindowProc(hwnd, msg, wParam, lParam);
- }
- public bool IsGlassEnabled
- {
- get
- {
- return NativeMethods.DwmIsCompositionEnabled();
- }
- private set
- {
- if (value != this._isGlassEnabled)
- {
- this._isGlassEnabled = value;
- this._NotifyPropertyChanged("IsGlassEnabled");
- }
- }
- }
- public Color WindowGlassColor
- {
- get
- {
- return this._glassColor;
- }
- private set
- {
- if (value != this._glassColor)
- {
- this._glassColor = value;
- this._NotifyPropertyChanged("WindowGlassColor");
- }
- }
- }
- public SolidColorBrush WindowGlassBrush
- {
- get
- {
- return this._glassColorBrush;
- }
- private set
- {
- if (this._glassColorBrush == null || value.Color != this._glassColorBrush.Color)
- {
- this._glassColorBrush = value;
- this._NotifyPropertyChanged("WindowGlassBrush");
- }
- }
- }
- public Thickness WindowResizeBorderThickness
- {
- get
- {
- return this._windowResizeBorderThickness;
- }
- private set
- {
- if (value != this._windowResizeBorderThickness)
- {
- this._windowResizeBorderThickness = value;
- this._NotifyPropertyChanged("WindowResizeBorderThickness");
- }
- }
- }
- public Thickness WindowNonClientFrameThickness
- {
- get
- {
- return this._windowNonClientFrameThickness;
- }
- private set
- {
- if (value != this._windowNonClientFrameThickness)
- {
- this._windowNonClientFrameThickness = value;
- this._NotifyPropertyChanged("WindowNonClientFrameThickness");
- }
- }
- }
- public double WindowCaptionHeight
- {
- get
- {
- return this._captionHeight;
- }
- private set
- {
- if (value != this._captionHeight)
- {
- this._captionHeight = value;
- this._NotifyPropertyChanged("WindowCaptionHeight");
- }
- }
- }
- public Size SmallIconSize
- {
- get
- {
- return new Size(this._smallIconSize.Width, this._smallIconSize.Height);
- }
- private set
- {
- if (value != this._smallIconSize)
- {
- this._smallIconSize = value;
- this._NotifyPropertyChanged("SmallIconSize");
- }
- }
- }
- [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ux")]
- [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ux")]
- public string UxThemeName
- {
- get
- {
- return this._uxThemeName;
- }
- private set
- {
- if (value != this._uxThemeName)
- {
- this._uxThemeName = value;
- this._NotifyPropertyChanged("UxThemeName");
- }
- }
- }
- [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ux")]
- [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ux")]
- public string UxThemeColor
- {
- get
- {
- return this._uxThemeColor;
- }
- private set
- {
- if (value != this._uxThemeColor)
- {
- this._uxThemeColor = value;
- this._NotifyPropertyChanged("UxThemeColor");
- }
- }
- }
- public bool HighContrast
- {
- get
- {
- return this._isHighContrast;
- }
- private set
- {
- if (value != this._isHighContrast)
- {
- this._isHighContrast = value;
- this._NotifyPropertyChanged("HighContrast");
- }
- }
- }
- public CornerRadius WindowCornerRadius
- {
- get
- {
- return this._windowCornerRadius;
- }
- private set
- {
- if (value != this._windowCornerRadius)
- {
- this._windowCornerRadius = value;
- this._NotifyPropertyChanged("WindowCornerRadius");
- }
- }
- }
- public Rect WindowCaptionButtonsLocation
- {
- get
- {
- return this._captionButtonLocation;
- }
- private set
- {
- if (value != this._captionButtonLocation)
- {
- this._captionButtonLocation = value;
- this._NotifyPropertyChanged("WindowCaptionButtonsLocation");
- }
- }
- }
- private void _NotifyPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
- if (propertyChanged != null)
- {
- propertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- [ThreadStatic]
- private static SystemParameters2 _threadLocalSingleton;
- private MessageWindow _messageHwnd;
- private bool _isGlassEnabled;
- private Color _glassColor;
- private SolidColorBrush _glassColorBrush;
- private Thickness _windowResizeBorderThickness;
- private Thickness _windowNonClientFrameThickness;
- private double _captionHeight;
- private Size _smallIconSize;
- private string _uxThemeName;
- private string _uxThemeColor;
- private bool _isHighContrast;
- private CornerRadius _windowCornerRadius;
- private Rect _captionButtonLocation;
- private readonly Dictionary<WM, List<SystemParameters2._SystemMetricUpdate>> _UpdateTable;
- private delegate void _SystemMetricUpdate(IntPtr wParam, IntPtr lParam);
- }
|