Changed the way window position is saved and handled. This should now work properly.
authorpaulwerelds
Wed, 06 Oct 2010 11:34:54 +0000
changeset 2149ddbb54998ac
parent 213 d93c927e4fd6
child 215 79e9a77f6a71
Changed the way window position is saved and handled. This should now work properly.
GUI/MainForm.Designer.cs
GUI/MainForm.cs
     1.1 --- a/GUI/MainForm.Designer.cs	Tue Oct 05 19:34:59 2010 +0000
     1.2 +++ b/GUI/MainForm.Designer.cs	Wed Oct 06 11:34:54 2010 +0000
     1.3 @@ -466,6 +466,7 @@
     1.4        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
     1.5        this.Text = "Open Hardware Monitor";
     1.6        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
     1.7 +      this.Load += new System.EventHandler(this.MainForm_Load);
     1.8        this.splitContainer.Panel1.ResumeLayout(false);
     1.9        this.splitContainer.Panel2.ResumeLayout(false);
    1.10        this.splitContainer.ResumeLayout(false);
     2.1 --- a/GUI/MainForm.cs	Tue Oct 05 19:34:59 2010 +0000
     2.2 +++ b/GUI/MainForm.cs	Wed Oct 06 11:34:54 2010 +0000
     2.3 @@ -100,26 +100,6 @@
     2.4        nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
     2.5        nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
     2.6  
     2.7 -      Rectangle newBounds = new Rectangle {
     2.8 -        X = settings.GetValue("mainForm.Location.X", Location.X),
     2.9 -        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
    2.10 -        Width = settings.GetValue("mainForm.Width", 470),
    2.11 -        Height = settings.GetValue("mainForm.Height", 640)
    2.12 -      };
    2.13 -
    2.14 -      Screen[] screens = Screen.AllScreens;
    2.15 -      Rectangle totalWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
    2.16 -        int.MinValue, int.MinValue);
    2.17 -
    2.18 -      foreach(Screen screen in screens)
    2.19 -        totalWorkingArea = Rectangle.Union(totalWorkingArea, screen.Bounds);
    2.20 -
    2.21 -      this.Bounds = newBounds;
    2.22 -
    2.23 -      if (!totalWorkingArea.Contains(newBounds) ||
    2.24 -        !settings.Contains("mainForm.Location.X"))
    2.25 -        this.StartPosition = FormStartPosition.CenterScreen;
    2.26 -
    2.27        foreach (TreeColumn column in treeView.Columns) 
    2.28          column.Width = Math.Max(20, Math.Min(400,
    2.29            settings.GetValue("treeView.Columns." + column.Header + ".Width",
    2.30 @@ -346,10 +326,10 @@
    2.31  
    2.32      private void SaveConfiguration() {
    2.33        if (WindowState != FormWindowState.Minimized) {
    2.34 -        settings.SetValue("mainForm.Location.X", Location.X);
    2.35 -        settings.SetValue("mainForm.Location.Y", Location.Y);
    2.36 -        settings.SetValue("mainForm.Width", ClientSize.Width);
    2.37 -        settings.SetValue("mainForm.Height", ClientSize.Height);
    2.38 +        settings.SetValue("mainForm.Location.X", Bounds.X);
    2.39 +        settings.SetValue("mainForm.Location.Y", Bounds.Y);
    2.40 +        settings.SetValue("mainForm.Width", Bounds.Width);
    2.41 +        settings.SetValue("mainForm.Height", Bounds.Height);
    2.42        }
    2.43  
    2.44        foreach (TreeColumn column in treeView.Columns)
    2.45 @@ -361,13 +341,41 @@
    2.46        try {
    2.47          settings.Save(fileName);
    2.48        } catch (UnauthorizedAccessException) {
    2.49 -        MessageBox.Show("Access to the path '" + fileName + "' is denied. " + 
    2.50 +        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
    2.51            "The current seetings could not be saved.", 
    2.52            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    2.53        }
    2.54      }
    2.55  
    2.56 -   private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
    2.57 +    private void MainForm_Load(object sender, EventArgs e) {
    2.58 +      Rectangle newBounds = new Rectangle {
    2.59 +        X = settings.GetValue("mainForm.Location.X", Location.X),
    2.60 +        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
    2.61 +        Width = settings.GetValue("mainForm.Width", 470),
    2.62 +        Height = settings.GetValue("mainForm.Height", 640)
    2.63 +      };
    2.64 +
    2.65 +      Rectangle totalWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
    2.66 +        int.MinValue, int.MinValue);
    2.67 +
    2.68 +      foreach (Screen screen in Screen.AllScreens)
    2.69 +        totalWorkingArea = Rectangle.Union(totalWorkingArea, screen.Bounds);
    2.70 +
    2.71 +      Rectangle intersection = Rectangle.Intersect(totalWorkingArea, newBounds);
    2.72 +      if (intersection.Width < 20 || intersection.Height < 20 ||
    2.73 +        !settings.Contains("mainForm.Location.X")
    2.74 +      ) {
    2.75 +        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
    2.76 +                      (newBounds.Width/2);
    2.77 +
    2.78 +        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
    2.79 +                      (newBounds.Height / 2);
    2.80 +      }
    2.81 +
    2.82 +      this.Bounds = newBounds;
    2.83 +    }
    2.84 +    
    2.85 +    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
    2.86        Visible = false;
    2.87        SaveConfiguration();
    2.88