方法
步骤一打开项目属性窗口切换到设置(Settings)标签
如下图添加属性
Name Type Scope Value
WindowLocation SystemDrawingPoint User
WindowSize SystemDrawingSize User
步骤二
在要保存状态的窗体代码头部添加
using UserSettingsDemoProperties;
在窗体的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 settingsif (thisWindowState == FormWindowStateNormal){if (thisSizeWidth != && thisSizeHeight != ){SettingsDefaultWindowSize = thisSize;}}else{if (thisRestoreBoundsSizeWidth != && thisRestoreBoundsSizeHeight != ){SettingsDefaultWindowSize = thisRestoreBoundsSize;}}// Save settings if(thisWindowState!=FormWindowStateMinimized)SettingsDefaultSave();}