zoukankan      html  css  js  c++  java
  • 读写注册表来保存窗体当前设置

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Microsoft.Win32;
    namespace RegiestryManager
    {
        
    /// <summary>
        
    /// Form2 的摘要说明。
        
    /// </summary>

        public class Form2 : System.Windows.Forms.Form
        
    {
            
    private System.Windows.Forms.Button button1;
            
    private System.Windows.Forms.ColorDialog chooseColorDialog;
            
    /// <summary>
            
    /// 必需的设计器变量。
            
    /// </summary>

            private System.ComponentModel.Container components = null;

            
    public Form2()
            
    {
                
    //
                
    // Windows 窗体设计器支持所必需的
                
    //
                InitializeComponent();

                
    //
                
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                
    //
                LoadFormPropertySet();
            }


            
    /// <summary>
            
    /// 清理所有正在使用的资源。
            
    /// </summary>

            protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if(components != null)
                    
    {
                        components.Dispose();
                        
                    }

                }

                
    base.Dispose( disposing );
            }


            
    Windows 窗体设计器生成的代码

            
    private void Form2_Load(object sender, System.EventArgs e)
            
    {
            
            }


            
    //在窗体启动时加载上次设置
            private bool LoadFormPropertySet()
            
    {
                
    try
                
    {
                    
    if(ReadSettings(this.Text)==false)
                        
    return false;
                    StartPosition
    =FormStartPosition.Manual;  //设定当前窗体的起始位置为原始起始位置

                }

                
    catch
                
    {
                    
    return false;

                }

                
    return true;
            }


            
    ////在窗体关闭时保存本次设置
            private bool SaveFormPropertySet()
            
    {
                
    try
                
    {
                    SaveSettings(
    this.Text );
                }

                
    catch
                
    {
                    
    return false;
                }

                
    return true;
            }


            
    private  void SaveSettings(string FormName)
            
    {
                RegistryKey softwareKey
    =Registry.LocalMachine.OpenSubKey("Software",true);
                RegistryKey frjKey
    =softwareKey.CreateSubKey(FormName) ;
                RegistryKey selfPlacingWindowKey
    =frjKey.CreateSubKey("SelfPlacingWindow") ;
                selfPlacingWindowKey.SetValue(
    "BackColor",(object)BackColor.ToKnownColor() ) ;
                selfPlacingWindowKey.SetValue(
    "Red",(object)(int)BackColor.R ); 
                selfPlacingWindowKey.SetValue(
    "Green",(object)(int)BackColor.G );
                selfPlacingWindowKey.SetValue(
    "Blue",(object)(int)BackColor.B  );
                selfPlacingWindowKey.SetValue(
    "Width",(object)Width);
                selfPlacingWindowKey.SetValue(
    "Height",(object)Height);
                selfPlacingWindowKey.SetValue(
    "X",(object)DesktopLocation.X );
                selfPlacingWindowKey.SetValue(
    "Y",(object)DesktopLocation.Y);
                selfPlacingWindowKey.SetValue(
    "WindowState",(object)WindowState.ToString() );
            }


            
    private bool ReadSettings(string FormName)
            
    {
            
                RegistryKey softwareKey
    =Registry.LocalMachine.OpenSubKey("Software",true);
                RegistryKey frjKey
    =softwareKey.OpenSubKey(FormName) ;
                
    if(frjKey==null)
                    
    return false;
                RegistryKey selfPlacingWindowKey
    =frjKey.OpenSubKey ("SelfPlacingWindow") ;
                
    if(selfPlacingWindowKey==null)
                    
    return false;
                
    int redComponent=(int)selfPlacingWindowKey.GetValue("Red") ;
                
    int greenComponent=(int)selfPlacingWindowKey.GetValue("Green") ;
                
    int blueComponent=(int)selfPlacingWindowKey.GetValue("Blue") ;
                
    this.BackColor =Color.FromArgb (redComponent,greenComponent,blueComponent);
                
                
    int X=(int)selfPlacingWindowKey.GetValue("X") ;
                
    int Y=(int)selfPlacingWindowKey.GetValue("Y") ;
                
    this.DesktopLocation=new Point(X,Y)  ;
                
                
    this.Height=(int)selfPlacingWindowKey.GetValue("Height") ;
                
    this.Width =(int)selfPlacingWindowKey.GetValue("Width") ;
                
                
    string initialWindowState=(string)selfPlacingWindowKey.GetValue("WindowState") ;
                
                
    this.WindowState =checked((FormWindowState)FormWindowState.Parse(WindowState.GetType(), initialWindowState));
                
    return true;
      
            }


            
    private void button1_Click(object sender, System.EventArgs e)
            
    {
                
    if(this.chooseColorDialog.ShowDialog()==DialogResult.OK  )
                    BackColor
    =chooseColorDialog.Color;
            }


            
    private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            
    {
                SaveFormPropertySet();
            }

        }

    }

  • 相关阅读:
    C#线程类Thread初步
    无限级分类存储过程版
    C#多线程编程实例实战
    数据库里阻塞和死锁情况 看那里死锁的存储过程
    预防按钮的多次点击 恶意刷新
    .net2.0文件压缩/解压缩
    HttpHandler和HttpModule入门
    反射,枚举,绑定下拉框
    在C#里关于定时器类
    判断上传的图片文件格式是否合法不是用后缀做的判断
  • 原文地址:https://www.cnblogs.com/furenjun/p/387596.html
Copyright © 2011-2022 走看看