方法
步驟一
打開項目屬性窗口
切換到設置(Settings)標簽
如下圖添加屬性
Name Type Scope Value
WindowLocation System
Drawing
Point User
WindowSize System
Drawing
Size User
步驟二
在要保存狀態的窗體代碼頭部添加
using UserSettingsDemo
Properties;
在窗體的FormLoad事件中添加以下代碼
private void FormMain_Load(object sender EventArgs e)
{
// Set window location
if (SettingsDefaultWindowLocation != null)
{
thisLocation = SettingsDefaultWindowLocation;
}
// Set window size
if (SettingsDefaultWindowSize != null)
{
thisSize = SettingsDefaultWindowSize;
}
}
步驟三
在窗體的FormClosing事件中添加如下代碼
private void FormMain_FormClosing(object sender FormClosingEventArgs e)
{
// Copy window location to app settings
SettingsDefaultWindowLocation = thisLocation;
// Copy window size to app settings
if (thisWindowState == FormWindowStateNormal)
{
SettingsDefaultWindowSize = thisSize;
}
else
{
SettingsDefaultWindowSize = thisRestoreBoundsSize;
}
// Save settings
SettingsDefaultSave();
}
以上是原作者寫的
窗體最小化後在任務欄右鍵關閉窗體
再次打開窗體會有點問題
以下是不才寫的
private void frmMain_FormClosing(object sender FormClosingEventArgs e)
{
// Copy window location to app settings
SettingsDefaultWindowLocation = thisLocation;
// Copy window size to app settings
if (thisWindowState == FormWindowStateNormal)
{
if (thisSizeWidth != && thisSizeHeight != )
{
SettingsDefaultWindowSize = thisSize;
}
}
else
{
if (thisRestoreBoundsSizeWidth != && thisRestoreBoundsSizeHeight != )
{
SettingsDefaultWindowSize = thisRestoreBoundsSize;
}
}
// Save settings
if(thisWindowState!=FormWindowStateMinimized)
SettingsDefaultSave();
}
From:http://tw.wingwit.com/Article/program/net/201311/12617.html