D23_01_window class RegistryKey Rect

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.