D23_01_Window类 RegistryKey Rect
2020-12-13 15:01
标签:style blog http io color ar os sp strong MainWindow(窗体类) WindowPositionHelper D23_01_Window类 RegistryKey Rect 标签:style blog http io color ar os sp strong 原文地址:http://www.cnblogs.com/xiepengtest/p/4068994.htmlWindow 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 WindowButton>
Button Margin="2" Click="Button_Click">Close MeButton>
Button Margin="2" Click="Button_Click_1">Center MeButton>
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>
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
{
///
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;
}
}
}
}
文章标题:D23_01_Window类 RegistryKey Rect
文章链接:http://soscw.com/essay/34731.html