[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;//添加Microsoft.Win32命名空间引用
namespace StartFormByLastClosePosition
{
public partial class frm_Main : Form
{
public frm_Main()
{
InitializeComponent();
}
private void frm_Main_Load(object sender, EventArgs e)
{
RegistryKey myReg1, myReg2;
myReg1 = Registry.CurrentUser;//获取当前用户注册表项
try
{
myReg2 = myReg1.CreateSubKey(@"Software\MyFormLocation");//在用户注册表Software下创建MyFormLocation子项
this.Location = new Point(Convert.ToInt16(myReg2.GetValue("myX")), Convert.ToInt16(myReg2.GetValue("MyY")));//获取注册表值并设置窗体显示位置
}
catch
{
}
}
private void frm_Main_FormClosed(object sender, FormClosedEventArgs e)
{
RegistryKey myReg1, myReg2;
myReg1 = Registry.CurrentUser;
myReg2 = myReg1.CreateSubKey(@"Software\MyFormLocation");
try
{
myReg2.SetValue("myX", this.Location.X.ToString());//记录X坐标
myReg2.SetValue("myY",this.Location.Y.ToString());//记录Y坐标
}
catch
{
}
}
}
}