备忘录
1.首先,手动添加Winform窗体关闭的事件代码
右击窗体,选择属性:点击右边的小闪电标志
2.双击FormClosing事件
3.完善窗体关闭的代码
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
RegistryKey point1, point2;
//获取注册表的根节点HKEY_CURRENT_USER
point1 = Registry.CurrentUser;
//再获取接下来的节点
point2 = point1.CreateSubKey("Software\\MySoft");
try
{
//将当前窗体的X,Y坐标保存在其中
point2.SetValue("1", this.Location.X.ToString());
point2.SetValue("2", this.Location.Y.ToString());
}
catch (Exception)
{
}
}
上面代码的目的是在窗体关闭的时候,读取一下当前窗体所在的位置,然后保存在注册表中
4.完善窗体加载时的代码(下面代码的目的是在窗体加载的时候,从注册表中读取上面保存的坐标)
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey point1, point2;
//获取注册表的根节点HKEY_CURRENT_USER
point1 = Registry.CurrentUser;
try
{
//再获取接下来的节点
point2 = point1.CreateSubKey("Software\\MySoft");
//读取注册表中存储的信息,并将当前窗体的坐标显示出来
this.Location = new Point(Convert.ToInt16(point2.GetValue("1")), Convert.ToInt16(point2.GetValue("2")));
}
catch (Exception)
{
}
}