记录窗口位置与大小


using Microsoft.Win32;


        /// <summary>
        /// 加载时恢复窗口。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMain_Load(object sender, EventArgs e)
        {
            #region 窗口个性化服务
            // 恢复窗口位置和大小。
            RegistryKey rk = EdcjGlobalVar.rk.CreateSubKey("Form");
            object rkValue;

            // 设置当前窗口位置。
            this.Width = (rkValue = rk.GetValue(this.Name + ".Width")) == null ? this.Width : (int)rkValue;
            this.Height = (rkValue = rk.GetValue(this.Name + ".Height")) == null ? this.Height : (int)rkValue;
            this.SetDesktopLocation((rkValue = rk.GetValue(this.Name + ".Location.X")) == null ? this.Location.X : (int)rkValue,
                (rkValue = rk.GetValue(this.Name + ".Location.Y")) == null ? this.Location.Y : (int)rkValue);
            #endregion 窗口个性化服务
        }

 


        /// <summary>
        /// 退出时保存窗口。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            #region 窗口个性化服务
            // 保存窗口位置与大小。
            RegistryKey rk = EdcjGlobalVar.rk.CreateSubKey("Form");

            // 只有当 Form 类的 WindowState 属性不等于 Normal 时,RestoreBounds 属性的值才有效。
            // 正常窗口时保存窗口本身的参数。
            if (this.WindowState == FormWindowState.Normal)
            {
                rk.SetValue(this.Name + ".Width", this.Width, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Height", this.Height, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Location.X", this.Location.X, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Location.Y", this.Location.Y, RegistryValueKind.DWord);
            }
            else
            {
                rk.SetValue(this.Name + ".Width", this.RestoreBounds.Width, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Height", this.RestoreBounds.Height, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Location.X", this.RestoreBounds.Location.X, RegistryValueKind.DWord);
                rk.SetValue(this.Name + ".Location.Y", this.RestoreBounds.Location.Y, RegistryValueKind.DWord);
            }
            #endregion 窗口个性化服务
        }

EdcjGlobalVar.rk指向一个注册表项。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值