码迷,mamicode.com
首页 > Windows程序 > 详细

D23_01_Window类 RegistryKey Rect

时间:2014-11-02 13:49:33      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   strong   

bubuko.com,布布扣

<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;
            }
        }
    }
}

D23_01_Window类 RegistryKey Rect

标签:style   blog   http   io   color   ar   os   sp   strong   

原文地址:http://www.cnblogs.com/xiepengtest/p/4068994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!