開発環境
- Visual Studio 2017
- WPF 4.5.1
概要
サンプルコード(該当コードのみ)
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// OnSourceInitialized
/// </summary>
/// <param name="e">EventArgs</param>
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
LoadWindowPlacement();
}
/// <summary>
/// OnClosing
/// </summary>
/// <param name="e">CancelEventArgs</param>
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (!e.Cancel)
{
SaveWindowPlacement();
}
}
/// <summary>
/// Window位置の復元
/// </summary>
void LoadWindowPlacement()
{
Properties.Settings.Default.Reload();
var bounds = Properties.Settings.Default.Bounds;
Left = bounds.Left;
Top = bounds.Top;
Width = bounds.Width;
Height = bounds.Height;
WindowState = Properties.Settings.Default.WindowState;
}
/// <summary>
/// Window位置の保存
/// </summary>
void SaveWindowPlacement()
{
Properties.Settings.Default.WindowState = WindowState == WindowState.Minimized ? WindowState.Normal : WindowState; // 最小化は保存しない
Properties.Settings.Default.Bounds = RestoreBounds;
Properties.Settings.Default.Save();
}
}