Skip to content

Instantly share code, notes, and snippets.

@Grabacr07
Last active January 4, 2016 12:09
Show Gist options
  • Save Grabacr07/8619526 to your computer and use it in GitHub Desktop.
Save Grabacr07/8619526 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
namespace RestorableWindowSample
{
public class RestorableWindow : Window
{
#region WindowSettings 依存関係プロパティ
public IWindowSettings WindowSettings
{
get { return (IWindowSettings)this.GetValue(WindowSettingsProperty); }
set { this.SetValue(WindowSettingsProperty, value); }
}
public static readonly DependencyProperty WindowSettingsProperty =
DependencyProperty.Register("WindowSettings", typeof(IWindowSettings), typeof(RestorableWindow), new UIPropertyMetadata(null));
#endregion
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// 外部からウィンドウ設定の保存・復元クラスが与えられていない場合は、既定実装を使用する
if (this.WindowSettings == null)
{
this.WindowSettings = new WindowSettings(this);
}
this.WindowSettings.Reload();
if (this.WindowSettings.Placement.HasValue)
{
var hwnd = new WindowInteropHelper(this).Handle;
var placement = this.WindowSettings.Placement.Value;
placement.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
placement.flags = 0;
placement.showCmd = (placement.showCmd == SW.SHOWMINIMIZED ? SW.SHOWNORMAL : placement.showCmd);
NativeMethods.SetWindowPlacement(hwnd, ref placement);
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (!e.Cancel)
{
WINDOWPLACEMENT placement;
var hwnd = new WindowInteropHelper(this).Handle;
NativeMethods.GetWindowPlacement(hwnd, out placement);
this.WindowSettings.Placement = placement;
this.WindowSettings.Save();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment