From e7e5a163abdbcd24ec83a5a87e8665c22f9bee61 Mon Sep 17 00:00:00 2001 From: Peter Gallop Date: Thu, 30 Jan 2025 00:26:11 +0000 Subject: [PATCH] Panel will now recentre on main screen if it is not visible. Quick Observation window changed to centre on panel just like all other EAACP dialogs. --- EAACP.csproj | 1 + Form1.Designer.cs | 2 +- Form1.cs | 11 ++++++++--- QuickObs.Designer.cs | 2 +- QuickObs.cs | 24 ------------------------ WindowCentre.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 WindowCentre.cs diff --git a/EAACP.csproj b/EAACP.csproj index 801ba0b..7c585ae 100644 --- a/EAACP.csproj +++ b/EAACP.csproj @@ -137,6 +137,7 @@ StellariumControl.cs + Config.cs diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 824d8b5..6adbd6f 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -222,7 +222,7 @@ private void InitializeComponent() this.MinimumSize = new System.Drawing.Size(168, 88); this.Name = "frmCP"; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; - this.Text = "EAA CP (0.5b2)"; + this.Text = "EAA CP (0.5b3)"; this.TopMost = true; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmCP_FormClosing); this.flpMain.ResumeLayout(false); diff --git a/Form1.cs b/Form1.cs index 0cbb47b..582659c 100644 --- a/Form1.cs +++ b/Form1.cs @@ -97,8 +97,13 @@ public frmCP() { this.Width = Properties.Settings.Default.frmWidth; this.Height = Properties.Settings.Default.frmHeight; + this.Left = Properties.Settings.Default.XPos; + this.Top = Properties.Settings.Default.YPos; - if (Screen.PrimaryScreen.Bounds.Width > Properties.Settings.Default.XPos + this.Width) + // Center the form on the screen, if the form is larger off the screen then center the form on the screen + WindowHelper.EnsureWindowVisible(this); + + /* if (Screen.PrimaryScreen.Bounds.Width > Properties.Settings.Default.XPos + this.Width) { this.Left = Properties.Settings.Default.XPos; if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height > Properties.Settings.Default.YPos + this.Height) @@ -113,7 +118,7 @@ public frmCP() else { this.CenterToScreen(); - } + } */ } SetTimer(); @@ -472,7 +477,7 @@ private void btnQuickObs_Click(object sender, EventArgs e) using (QuickObs formObs = new QuickObs()) { - formObs.TopMost = true; + formObs.TopMost = true; // Associated objects formObs.Controls["chkAssociated"].Visible = bAssociated; diff --git a/QuickObs.Designer.cs b/QuickObs.Designer.cs index 3f41886..5216491 100644 --- a/QuickObs.Designer.cs +++ b/QuickObs.Designer.cs @@ -112,7 +112,7 @@ private void InitializeComponent() this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "QuickObs"; - this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Quick Observation"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.QuickObs_FormClosing); this.ResumeLayout(false); diff --git a/QuickObs.cs b/QuickObs.cs index 7e3ae45..110b4c5 100644 --- a/QuickObs.cs +++ b/QuickObs.cs @@ -15,30 +15,6 @@ public partial class QuickObs : Form public QuickObs() { InitializeComponent(); - - if (Properties.Settings.Default.QOYPos == -1) - { - this.CenterToParent(); - } - else - { - if (Screen.PrimaryScreen.Bounds.Width > Properties.Settings.Default.QOXPos + this.Width) - { - this.Left = Properties.Settings.Default.QOXPos; - if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height > Properties.Settings.Default.QOYPos + this.Height) - { - this.Top = Properties.Settings.Default.QOYPos; - } - else - { - this.CenterToParent(); - } - } - else - { - this.CenterToParent(); - } - } } private void btnCancel_Click(object sender, EventArgs e) diff --git a/WindowCentre.cs b/WindowCentre.cs new file mode 100644 index 0000000..e72a010 --- /dev/null +++ b/WindowCentre.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace EAACP +{ + internal class WindowHelper + { + public static void EnsureWindowVisible(Form form, Form parent = null) + { + bool isVisible = false; + + // Check if the window is at least partially visible on any screen + foreach (Screen screen in Screen.AllScreens) + { + if (screen.WorkingArea.IntersectsWith(form.Bounds)) + { + isVisible = true; + return; + } + } + + // If the window is not visible on any screen, center it on the primary screen + if (!isVisible) + { + if (parent != null) + { + form.StartPosition = FormStartPosition.CenterParent; + return; + } + + var primary = Screen.PrimaryScreen; + var primaryWorkingArea = primary.WorkingArea; + int primaryCenterX = (primaryWorkingArea.Right - form.Width) / 2; + int primaryCenterY = (primaryWorkingArea.Bottom - form.Height) / 2; + form.Location = new Point(primaryCenterX, primaryCenterY); + } + } + } +}