您的位置:首页 > 其它

记录窗口位置与大小

2008-06-06 22:24 246 查看
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指向一个注册表项。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: