<Windowx:class= "Demo." MainWindow "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525"> <StackPanelMargin= "5"> <ButtonMargin= "2"Click= "ShowWindow">Show Window</Button> <ButtonMargin= "2"Click= "Button_Click">Close Me</Button> <ButtonMargin= "2"Click= "Button_click_1">Center Me</Button> <ButtonMargin= "2"Click= "Button_click_2">Save Position and Size (configuration file mode)</Button> <ButtonMargin= "2"Click= "Button_click_3">Restore Position and Size (registry mode)</Button> </StackPanel></Window>
MainWindow (Form Class)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespacedemo{/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); } Private voidShowWindow (Objectsender, RoutedEventArgs e) {Taskwindow Wintask=NewTaskwindow (); //ShowDialog () means open in modal mode, you must close this window before you can manipulate other WindowsWintask.showdialog (); } Private voidButton_Click (Objectsender, RoutedEventArgs e) { This. Close (); } //Set the window position to the center of the workspace Private voidButton_click_1 (Objectsender, RoutedEventArgs e) { DoubleScreenHeight =SystemParameters.WorkArea.Height; DoubleScreenWidth =SystemParameters.WorkArea.Width; This. Top = (ScreenHeight- This. Height)/2; This. left = (ScreenWidth- This. Width)/2; } Private voidButton_click_2 (Objectsender, RoutedEventArgs e) { //Windowpositionhelper (Registry way to save the form's location and size)//Windowpositionhelperconfig (the location and size of the form where the configuration file is saved)Windowpositionhelper.savesize ( This); } Private voidButton_click_3 (Objectsender, RoutedEventArgs e) { //Windowpositionhelper (Registry way to save the form's location and size)//Windowpositionhelperconfig (the location and size of the form where the configuration file is saved)Windowpositionhelper.setsize ( This); } }}
Windowpositionhelper
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingMicrosoft.Win32;namespacedemo{//Windowpositionhelper (Registry way to save the form's location and size) Public classWindowpositionhelper { Public Static stringRegpath =@"Software\myapp"; Public Static voidSavesize (Window win) {RegistryKey key; Key= Registry.CurrentUser.CreateSubKey (Regpath +win. Name); Key. SetValue ("Bounds", win. Restorebounds.tostring (System.Globalization.CultureInfo.InvariantCulture)); } Public Static voidSetSize (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 (the location and size of the form where the configuration file is saved) Public classWindowpositionhelperconfig { Public Static voidSavesize (Window win) {Properties.Settings.Default.WindowPosition=win. RestoreBounds; Properties.Settings.Default.Save (); } Public Static voidSetSize (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; } } }}
D23_01_window class RegistryKey Rect