记录窗口位置与大小
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指向一个注册表项。
/// <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指向一个注册表项。
相关文章推荐
- 记录窗口的位置和大小
- 一个 结构体保存和窗口位置大小记录的类
- CreateProcess StartupInfo 改变外部程序窗口的起始位置和大小
- Qt学习---窗口的位置和大小
- Javascript获取窗口(容器)的大小及位置
- GetPageSize和GetPageScroll:获取页面大小、窗口大小和滚动条位置
- MFC 控件随窗口尺寸变化自动调整位置及大小
- 改变窗口大小后跟踪滚动条位置
- 改变浏览器窗口的大小和位置
- 猎豹MFC--窗口操作--最大化 最小化还原 关闭 大小位置 居中显示 图标 标题
- 浏览器窗口位置及窗口大小
- WinAPI: MoveWindow - 改变窗口的位置与大小
- 控件的大小和位置随窗口变化
- 利用Preferences类记录界面的位置和大小
- GridView弹出新页面/弹出制定大小位置新窗口
- MFC中设置窗口大小位置
- 13.GridView弹出新页面/弹出制定大小位置新窗口:
- Javascript实现获取窗口的大小和位置代码分享
- Jquery 随窗口改变大小不会影响浏览位置,DIV可以在绝对的位置不变.菜单相对位置不变
- 使用MoveWindow()函数来移动指定句柄的窗口在屏幕中的显示位置及这个窗口的大小