19 using System.Collections.Generic;
23 using System.Windows.Forms;
24 using System.Windows.Interop;
25 using System.Windows.Media;
27 namespace Bovender.Mvvm.Views.Settings
35 #region Attached property "Save" 37 public static readonly DependencyProperty SaveProperty = DependencyProperty.RegisterAttached(
38 "Save", typeof(
bool), typeof(
WindowState),
new PropertyMetadata(OnSavePropertyChanged));
40 [AttachedPropertyBrowsableForType(typeof(Window))]
41 public static bool GetSave(UIElement element)
43 return (
bool)element.GetValue(SaveProperty);
46 public static void SetSave(UIElement element,
bool value)
48 element.SetValue(SaveProperty, value);
51 private static void OnSavePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
53 if (e.Property == SaveProperty)
55 Window window = obj as Window;
58 if ((
bool)e.NewValue ==
true)
60 window.Closed += SaveWindowGeometry;
61 window.Initialized += LoadWindowGeometry;
65 window.Closed -= SaveWindowGeometry;
66 window.Initialized -= LoadWindowGeometry;
74 #region Attached property "CenterScreen" 76 public static readonly DependencyProperty CenterScreenProperty = DependencyProperty.RegisterAttached(
77 "CenterScreen", typeof(
bool), typeof(
WindowState),
new PropertyMetadata(OnCenterScreenPropertyChanged));
87 [AttachedPropertyBrowsableForType(typeof(Window))]
90 return (
bool)element.GetValue(CenterScreenProperty);
93 public static void SetCenterScreen(UIElement element,
bool value)
95 element.SetValue(CenterScreenProperty, value);
98 private static void OnCenterScreenPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
100 if (e.Property == CenterScreenProperty)
102 Window window = obj as Window;
105 if ((
bool)e.NewValue ==
true)
109 window.Loaded += CenterWindow;
115 private static void CenterWindow(
object sender, EventArgs e)
117 Window w = sender as Window;
118 Screen screen = Screen.FromHandle(
new WindowInteropHelper(w).Handle);
119 PresentationSource ps = PresentationSource.FromVisual(w);
120 CompositionTarget ct = ps.CompositionTarget;
121 Matrix t = ct.TransformFromDevice;
122 Point screenGeo = t.Transform(
new Point(screen.Bounds.Width, screen.Bounds.Height));
123 double screenWidth = screenGeo.X;
124 double screenHeight = screenGeo.Y;
125 w.Left = Math.Max((screenWidth - w.ActualWidth) / 2, 0);
126 w.Top = Math.Max((screenHeight - w.ActualHeight) / 2, 0);
131 #region Event handlers 133 static void LoadWindowGeometry(
object sender, EventArgs e)
136 Window w = sender as Window;
138 if (s.
Rect != Rect.Empty)
140 w.Left = s.
Rect.Left;
142 w.Width = s.
Rect.Width;
143 w.Height = s.
Rect.Height;
144 SanitizeWindowGeometry(w);
151 w.Loaded -= CenterWindow;
153 w.WindowState = s.State;
156 static void SaveWindowGeometry(
object sender, EventArgs e)
159 Window w = sender as Window;
160 s.
Rect =
new Rect(w.Left, w.Top, w.Width, w.Height);
161 s.State = w.WindowState;
162 s.Screen = Screen.FromHandle(
new WindowInteropHelper(w).Handle);
168 #region Private methods 172 Window w = obj as Window;
179 throw new InvalidOperationException(
"The WindowState.Save property must be attached to Windows only.");
187 static void SanitizeWindowGeometry(Window window)
190 double vleft = SystemParameters.VirtualScreenLeft;
191 double vright = vleft + SystemParameters.VirtualScreenWidth;
192 double vtop = SystemParameters.VirtualScreenTop;
193 double vbottom = vtop + SystemParameters.VirtualScreenHeight;
204 if (w.Left > vright - w.Width)
206 w.Left = vright - w.Width;
212 if (w.Top > vbottom - w.Height)
214 w.Top = vbottom - w.Height;
Provides an attached property to enable Windows to save and restore their screen position and state...
static bool GetCenterScreen(UIElement element)
Centers the Window that the property is attached to on the screen that the window is currently being ...
Rect Rect
The rectangle describing the window's coordinates.