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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 /**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
 {
this.button1 = new System.Windows.Forms.Button();
this.chooseColorDialog = new System.Windows.Forms.ColorDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(352, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 326);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form2_Closing);
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

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

|