1. 【首先引用原文】
C#中使用Setting保存用户自定义窗体位置
C#中使用Setting保存用户自定义窗体位置
2008-11-06
步骤一:打开项目属性窗口,切换到设置(Settings)标签,
如下图添加属性
Name Type Scope Value
WindowLocation System.Drawing.Point User 0,0
WindowSize System.Drawing.Size User 300,300
步骤二:
在要保存状态的窗体代码头部添加
using UserSettingsDemo.Properties;
在窗体的FormLoad事件中添加以下代码:
private void FormMain_Load(object sender, EventArgs e) { // Set window location if (Settings.Default.WindowLocation != null) {this.Location = Settings.Default.WindowLocation;
}
// Set window size
if (Settings.Default.WindowSize != null)
{
this.Size = Settings.Default.WindowSize;
}
}
步骤三:
在窗体的FormClosing事件中添加如下代码:
private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location;
// Copy window size to app settings
if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
// Save settings
Settings.Default.Save();
}
以上是原作者写的,窗体最小化后在任务栏右键关闭窗体,
再次打开窗体会有点问题,以下是不才写的
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location; // Copy window size to app settings if (this.WindowState == FormWindowState.Normal) { if (this.Size.Width != 0 && this.Size.Height != 0) { Settings.Default.WindowSize = this.Size; } } else { if (this.RestoreBounds.Size.Width != 0 && this.RestoreBounds.Size.Height != 0)
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
}
// Save settings
if(this.WindowState!=FormWindowState.Minimized)
Settings.Default.Save();
2008-11-06
步骤一:打开项目属性窗口,切换到设置(Settings)标签,
如下图添加属性
Name Type Scope Value
WindowLocation System.Drawing.Point User 0,0
WindowSize System.Drawing.Size User 300,300
步骤二:
在要保存状态的窗体代码头部添加
using UserSettingsDemo.Properties;
在窗体的FormLoad事件中添加以下代码:
private void FormMain_Load(object sender, EventArgs e) { // Set window location if (Settings.Default.WindowLocation != null) {this.Location = Settings.Default.WindowLocation;
}
// Set window size
if (Settings.Default.WindowSize != null)
{
this.Size = Settings.Default.WindowSize;
}
}
步骤三:
在窗体的FormClosing事件中添加如下代码:
private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location;
// Copy window size to app settings
if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
// Save settings
Settings.Default.Save();
}
以上是原作者写的,窗体最小化后在任务栏右键关闭窗体,
再次打开窗体会有点问题,以下是不才写的
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location; // Copy window size to app settings if (this.WindowState == FormWindowState.Normal) { if (this.Size.Width != 0 && this.Size.Height != 0) { Settings.Default.WindowSize = this.Size; } } else { if (this.RestoreBounds.Size.Width != 0 && this.RestoreBounds.Size.Height != 0)
{
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
}
// Save settings
if(this.WindowState!=FormWindowState.Minimized)
Settings.Default.Save();
2. 【此文在网上流传甚广,其实最后的保存操作走了歧途了,为其更正实现】
文中说的直接用第一作者中的方法可能出错,是因为在窗口最小化到任务栏后,其
Location=(-32000,-32000), Size=([任务栏图表大小])。
既然找到了原因,那么问题就容易改了,比如可以像下面这样做保存操作:
if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.WindowLocation = this.Location;
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowLocation = this.RestoreBounds.Location;
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
Settings.Default.Save();
{
Settings.Default.WindowLocation = this.Location;
Settings.Default.WindowSize = this.Size;
}
else
{
Settings.Default.WindowLocation = this.RestoreBounds.Location;
Settings.Default.WindowSize = this.RestoreBounds.Size;
}
Settings.Default.Save();
Over。