熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中使用Setting保存用戶自定義窗體位置

2022-06-13   來源: .NET編程 

方法
  步驟一打開項目屬性窗口切換到設置(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 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
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.