C# :修改注册表,从上次关闭位置启动窗体

备忘录

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)
            {
                
            }
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
去年年末才看过一本关于ASP.NET 操作注册表的书,可惜那时候就压根没想过写到网上去。 现在想想 写到网上有诸多好处 比如、 1、可以快速回忆相关知识点,还有自己写的参考代码,比别人写的强多了 2、可以丰富自己的博客推广自己 3、加强自己写作能力 贴出代码了 -------------------- 1、引入命名空间 using Microsoft.Win32;//修改注册表所需要用到的命名空间 2、之后方法喽 //修改注册表 设置IE打印背景图片 适用于IE8一下版本,修改为工具选项下的 打印背景颜色与图像 public void IESetupPrint_Background() { RegistryKey key = Registry.CurrentUser; RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main", true); //该项必须已存在 software.SetValue("Print_Background", "yes"); software.Close(); } //设置页眉 页脚为空 public void IESetupPageSetup() { RegistryKey key = Registry.CurrentUser; RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true); //该项必须已存在 software.SetValue("header", ""); software.SetValue("footer", ""); software.Close(); } //设置页边距 设置IE页面设置 打印背景图片 //上下边距具体的值可以 先在IE里面设置再查看注册表实际值 然后在程序里写死 public void IESetupPage() { RegistryKey key = Registry.CurrentUser;//IE8以上版本 修改为页面设置 里页面 打印背景颜色与图像 RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true); software.SetValue("margin_top", 0.55984); software.SetValue("margin_bottom", 0.55984); software.SetValue("margin_left", 0.75433); software.SetValue("margin_right", 0.40000); software.SetValue("Print_Background", "yes"); } //查看注册表相应的键值 运行输入 regedit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值