diff --git a/src/System.Windows.Forms/System/Windows/Forms/Form.cs b/src/System.Windows.Forms/System/Windows/Forms/Form.cs index c3900d09c6b..70e4e96d6a8 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Form.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Form.cs @@ -4290,7 +4290,7 @@ protected virtual void OnLoad(EventArgs e) // Also, at this time we can now locate the form on the correct area of the screen. // We must do this after applying any autoscaling. - if (GetState(States.Modal)) + if (GetState(States.Modal) || (FormStartPosition)_formState[s_formStateStartPos] == FormStartPosition.CenterParent) { AdjustFormPosition(); } @@ -4564,6 +4564,10 @@ private void WmDpiChanged(ref Message m) DeviceDpiInternal = e.DeviceDpiNew; OnDpiChanged(e); + if (!Visible && (FormStartPosition)_formState[s_formStateStartPos] == FormStartPosition.CenterParent) + { + AdjustFormPosition(); + } } /// diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/FormTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/FormTests.cs index 26574491052..712516b56eb 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/FormTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/FormTests.cs @@ -2763,6 +2763,77 @@ public void Form_DoesNot_DisposeUserIcon() smallIcon.Handle.Should().NotBe(0); } + [WinFormsFact] + public void Form_ShowDialog_WithCenterParent_PositionsCenteredOnParent() + { + using Form parentForm = new() + { + Size = new Size(400, 400), + StartPosition = FormStartPosition.Manual, + Location = new Point(200, 200) + }; + + parentForm.Show(); + using Form childForm = new() + { + Size = new Size(200, 200), + StartPosition = FormStartPosition.CenterParent + }; + + Point capturedLocation = Point.Empty; + childForm.Load += (sender, e) => + { + capturedLocation = childForm.Location; + childForm.Close(); + }; + + childForm.ShowDialog(parentForm); + // Expected center: parentForm center minus half of childForm size. + int expectedX = parentForm.Left + (parentForm.Width - childForm.Width) / 2; + int expectedY = parentForm.Top + (parentForm.Height - childForm.Height) / 2; + int deltaX = SystemInformation.Border3DSize.Width + 1; + int deltaY = SystemInformation.Border3DSize.Height + 1; + + capturedLocation.X.Should().BeInRange(expectedX - deltaX, expectedX + deltaX); + capturedLocation.Y.Should().BeInRange(expectedY - deltaY, expectedY + deltaY); + } + + [WinFormsFact] + public void Form_Show_WithCenterParent_PositionsCenteredOnParent() + { + using Form parentForm = new() + { + Size = new Size(400, 400), + StartPosition = FormStartPosition.Manual, + Location = new Point(200, 200) + }; + + parentForm.Show(); + using Form childForm = new() + { + Size = new Size(200, 200), + StartPosition = FormStartPosition.CenterParent + }; + + Point capturedLocation = Point.Empty; + childForm.Load += (sender, e) => + { + capturedLocation = childForm.Location; + }; + + childForm.Show(parentForm); + childForm.Close(); + + // Expected center: parentForm center minus half of childForm size. + int expectedX = parentForm.Left + (parentForm.Width - childForm.Width) / 2; + int expectedY = parentForm.Top + (parentForm.Height - childForm.Height) / 2; + int deltaX = SystemInformation.Border3DSize.Width + 1; + int deltaY = SystemInformation.Border3DSize.Height + 1; + + capturedLocation.X.Should().BeInRange(expectedX - deltaX, expectedX + deltaX); + capturedLocation.Y.Should().BeInRange(expectedY - deltaY, expectedY + deltaY); + } + public partial class ParentedForm : Form { private ParentingForm _parentForm;