Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer provided by VVS was a great help! I found two minor issues with it though, so I am reposting the bulk of his code with these revisions:</p> <p>(1) The very first time the application runs, the form is opened in a Normal state but is sized such that it appears as just a title bar. I added a conditional in the constructor to fix this.</p> <p>(2) If the application is closed while minimized or maximized the code in OnClosing fails to remember the dimensions of the window in its Normal state. (The 3 lines of code--which I have now commented out--seems reasonable but for some reason just does not work.) Fortunately I had previously solved this problem and have included that code in a new region at the end of the code to track window state as it happens rather than wait for closing.</p> <hr> <p>With these two fixes in place, I have tested:</p> <p>A. closing in normal state--restores to same size/position and state</p> <p>B. closing in minimized state--restores to normal state with last normal size/position</p> <p>C. closing in maximized state--restores to maximized state and remembers its last size/position when one later adjusts to normal state.</p> <p>D. closing on monitor 2--restores to monitor 2.</p> <p>E. closing on monitor 2 then disconnecting monitor 2--restores to same position on monitor 1</p> <p>David: your code allowed me to achieve points D and E almost effortlessly--not only did you provide a solution for my question, you provided it in a complete program so I had it up and running almost within seconds of pasting it into Visual Studio. So a big thank you for that!</p> <pre><code>public partial class MainForm : Form { bool windowInitialized; public MainForm() { InitializeComponent(); // this is the default this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.WindowsDefaultBounds; // check if the saved bounds are nonzero and visible on any screen if (Settings.Default.WindowPosition != Rectangle.Empty &amp;&amp; IsVisibleOnAnyScreen(Settings.Default.WindowPosition)) { // first set the bounds this.StartPosition = FormStartPosition.Manual; this.DesktopBounds = Settings.Default.WindowPosition; // afterwards set the window state to the saved value (which could be Maximized) this.WindowState = Settings.Default.WindowState; } else { // this resets the upper left corner of the window to windows standards this.StartPosition = FormStartPosition.WindowsDefaultLocation; // we can still apply the saved size // msorens: added gatekeeper, otherwise first time appears as just a title bar! if (Settings.Default.WindowPosition != Rectangle.Empty) { this.Size = Settings.Default.WindowPosition.Size; } } windowInitialized = true; } private bool IsVisibleOnAnyScreen(Rectangle rect) { foreach (Screen screen in Screen.AllScreens) { if (screen.WorkingArea.IntersectsWith(rect)) { return true; } } return false; } protected override void OnClosed(EventArgs e) { base.OnClosed(e); // only save the WindowState if Normal or Maximized switch (this.WindowState) { case FormWindowState.Normal: case FormWindowState.Maximized: Settings.Default.WindowState = this.WindowState; break; default: Settings.Default.WindowState = FormWindowState.Normal; break; } # region msorens: this code does *not* handle minimized/maximized window. // reset window state to normal to get the correct bounds // also make the form invisible to prevent distracting the user //this.Visible = false; //this.WindowState = FormWindowState.Normal; //Settings.Default.WindowPosition = this.DesktopBounds; # endregion Settings.Default.Save(); } # region window size/position // msorens: Added region to handle closing when window is minimized or maximized. protected override void OnResize(EventArgs e) { base.OnResize(e); TrackWindowState(); } protected override void OnMove(EventArgs e) { base.OnMove(e); TrackWindowState(); } // On a move or resize in Normal state, record the new values as they occur. // This solves the problem of closing the app when minimized or maximized. private void TrackWindowState() { // Don't record the window setup, otherwise we lose the persistent values! if (!windowInitialized) { return; } if (WindowState == FormWindowState.Normal) { Settings.Default.WindowPosition = this.DesktopBounds; } } # endregion window size/position } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload