Skip to content

Instantly share code, notes, and snippets.

@mmhantea
Created October 21, 2013 05:06
Show Gist options
  • Save mmhantea/7078924 to your computer and use it in GitHub Desktop.
Save mmhantea/7078924 to your computer and use it in GitHub Desktop.
CustomWindow : Preserves windows settings Top, Left, Width, Height on one or extended screen.
using System.ComponentModel;
using System.Windows;
/// <summary>
/// Preserve windows settings: Top, Left, Width, Height on one or extended screen.
/// </summary>
public class BaseWindow : Window
{
#region Private Fields
/// <summary>
/// The _updating window settings
/// </summary>
private bool _updatingWindowSettings;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="BaseWindow" /> class.
/// </summary>
public BaseWindow()
{
Loaded += OnBaseWindowLoaded;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
Closing += OnClosing;
}
#endregion
#region Events
/// <summary>
/// Called when [base window loaded].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
private void OnBaseWindowLoaded(object sender, RoutedEventArgs e)
{
//SetLanguageDictionary();
// refreshes the controls that use text from translation dioctionary
UpdateLayout();
}
private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
UpdateWindowSettings();
}
#endregion
#region WindowSettings (Dependency Property)
/// <summary>
/// The window settings property
/// </summary>
public static readonly DependencyProperty WindowSettingsProperty =
DependencyProperty.Register("WindowSettings", typeof(WindowSettings),
typeof(BaseWindow),
//new UIPropertyMetadata(null, WindowSettingsChangedCallback));
//new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, WindowSettingsChangedCallback));
new FrameworkPropertyMetadata(WindowSettingsChangedCallback));
/// <summary>
/// Gets or sets the window settings.
/// </summary>
/// <value>
/// The window settings.
/// </value>
public WindowSettings WindowSettings
{
get { return (WindowSettings)GetValue(WindowSettingsProperty); }
set { SetValue(WindowSettingsProperty, value); }
}
/// <summary>
/// Windows the settings changed callback.
/// </summary>
/// <param name="dependencyObject">The dependency object.</param>
/// <param name="dependencyPropertyChangedEventArgs">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void WindowSettingsChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var window = dependencyObject as BaseWindow;
if (window != null && !window._updatingWindowSettings)
{
window.WindowSettingsChanged();
}
}
#endregion
#region Private Methods
/// <summary>
/// Updates the window settings.
/// </summary>
private void UpdateWindowSettings()
{
_updatingWindowSettings = true;
WindowSettings = new WindowSettings(this);
_updatingWindowSettings = false;
}
/// <summary>
/// Windows the settings changed.
/// </summary>
private void WindowSettingsChanged()
{
WindowSettings.Apply(this);
}
#endregion
}
/// <summary>
/// Structure to keep the windows settings
/// </summary>
public struct WindowSettings
{
public double Top;
public double Left;
public double Width;
public double Height;
public WindowSettings(Window window)
{
Top = window.Top;
Left = window.Left;
Width = window.Width;
Height = window.Height;
}
/// <summary>
/// Applies the specified window.
/// </summary>
/// <param name="window">The window.</param>
public void Apply(Window window)
{
//Size it to fit the current screen
SizeToFit();
//Move the window at least partially into view
MoveIntoView();
window.Width = Width;
window.Height = Height;
window.Top = Top;
window.Left = Left;
}
/// <summary>
/// If the saved window dimensions are larger than the current screen shrink the
/// window to fit.
/// </summary>
public void SizeToFit()
{
if (Height > SystemParameters.VirtualScreenHeight)
{
Height = SystemParameters.VirtualScreenHeight;
}
if (Width > SystemParameters.VirtualScreenWidth)
{
Width = SystemParameters.VirtualScreenWidth;
}
}
/// <summary>
/// If the window is more than half off of the screen move it up and to the left
/// so half the height and half the width are visible.
/// </summary>
public void MoveIntoView()
{
if (Top + Height / 2 > SystemParameters.VirtualScreenHeight)
{
Top = SystemParameters.VirtualScreenHeight - Height;
}
if (Left + Width / 2 > SystemParameters.VirtualScreenWidth)
{
Left = SystemParameters.VirtualScreenWidth - Width;
}
if (Top < 0)
{
Top = 0;
}
if (Left < 0)
{
Left = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment