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

        }

    }

  • 相关阅读:
    HDU 5316——Magician——————【线段树区间合并区间最值】
    HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】
    BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
    BNU 20860——Forwarding Emails——————【强连通图缩点+记忆化搜索】
    日期
    HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
    HDU 5288——OO’s Sequence——————【技巧题】
    c++ 中. 和 ->,波浪号 ~ 符号怎么用 ————很重要
    c++缓冲区std::wstringbuf
    Arduino读取写入电压值
  • 原文地址:https://www.cnblogs.com/furenjun/p/387596.html
Copyright © 2011-2022 走看看