<Window x:Class="demo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel Margin="5"> <Button Margin="2" Click="showWindow">Show Window</Button> <Button Margin="2" Click="Button_Click">Close Me</Button> <Button Margin="2" Click="Button_Click_1">Center Me</Button> <Button Margin="2" Click="Button_Click_2">Save Position and Size(配置文件方式)</Button> <Button Margin="2" Click="Button_Click_3">Restore Position and Size(注册表方式)</Button> </StackPanel> </Window>
MainWindow(窗体类)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace demo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void showWindow(object sender, RoutedEventArgs e) { TaskWindow winTask = new TaskWindow(); //ShowDialog()表示以模态方式打开,必须关闭此窗口后才能操作其他窗口 winTask.ShowDialog(); } private void Button_Click(object sender, RoutedEventArgs e) { this.Close(); } //设置窗口位置为工作区的* private void Button_Click_1(object sender, RoutedEventArgs e) { double screenHeight = SystemParameters.WorkArea.Height; double screenWidth = SystemParameters.WorkArea.Width; this.Top = (screenHeight - this.Height) / 2; this.Left = (screenWidth - this.Width) / 2; } private void Button_Click_2(object sender, RoutedEventArgs e) { //WindowPositionHelper(注册表的方式保存窗体的位置和大小) //WindowPositionHelperConfig(配置文件的方式保存窗体的位置和大小) WindowPositionHelper.SaveSize(this); } private void Button_Click_3(object sender, RoutedEventArgs e) { //WindowPositionHelper(注册表的方式保存窗体的位置和大小) //WindowPositionHelperConfig(配置文件的方式保存窗体的位置和大小) WindowPositionHelper.SetSize(this); } } }
WindowPositionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using Microsoft.Win32; namespace demo { //WindowPositionHelper(注册表的方式保存窗体的位置和大小) public class WindowPositionHelper { public static string RegPath = @"Software\MyApp"; public static void SaveSize(Window win) { RegistryKey key; key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name); key.SetValue("Bounds", win.RestoreBounds.ToString(System.Globalization.CultureInfo.InvariantCulture)); } public static void SetSize(Window win) { RegistryKey key; key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name); if (key != null) { Rect rect = Rect.Parse(key.GetValue("Bounds").ToString()); win.Top = rect.Top; win.Left = rect.Left; if (win.SizeToContent == SizeToContent.Manual) { win.Width = rect.Width; win.Height = rect.Height; } } } } //WindowPositionHelperConfig(配置文件的方式保存窗体的位置和大小) public class WindowPositionHelperConfig { public static void SaveSize(Window win) { Properties.Settings.Default.WindowPosition = win.RestoreBounds; Properties.Settings.Default.Save(); } public static void SetSize(Window win) { Rect rect = Properties.Settings.Default.WindowPosition; win.Top = rect.Top; win.Left = rect.Left; if (win.SizeToContent == SizeToContent.Manual) { win.Width = rect.Width; win.Height = rect.Height; } } } }