dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.4k stars 979 forks source link

HDPI: Position of a Modal-Form (and Modeless) is not centered to parent #10422

Open Grebnietz opened 10 months ago

Grebnietz commented 10 months ago

.NET version

.NET SDK: Version: 7.0.404 Commit: 22f221a24c

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

The behaviour on .NET 6 is difference to .NET 7, but also not correct.

Issue description

I have a Form that is opened as Modal (.ShowDialog()) with StartPosition = FormStartPosition.CenterParent; But on my Secondary-Screen with 100% scaling, the Modal Dialog is not centered to it's parent. But a Modeless Dialog (.Show()) is centered!

On my Main-Screen with 125% scaling, the Modal Dialog is correctly centered, but the Modeless Dialog is not centered.

However if I call CenterToParent(); in Load-Event of the Form, the form is correctly centered.

Screenshot (74)

Screenshot (76)

The fix of issue #5729 seems not handle this case.

In .NET Framework 4.7.2 it seems that Modless Dialogs never centered.

Steps to reproduce

Create a new Default .NET Winforms App.

Setup the DpiAwareness Mode in Main()-Method

Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

Create a second Form that is called from the Mainform. Second Form is set to StartPosition = FormStartPosition.CenterParent;

...
   // Show as Modal Dialog
    DialogForm dialogForm = new DialogForm();
    dialogForm.ShowDialog(this);           

...

  // Show as Modeless Dialog
    Form dialogForm = new DialogForm();
    dialogForm.Show(this);

...

Dialog_CenterParent.zip

elachlan commented 10 months ago

@Olina-Zhang could your team please verify?

John-Qiao commented 10 months ago

.NET version

9.0.100-alpha.1.23604.8

Did it work in .NET Framework?

Yes, but the result is different with .NET 9.0. The detailed .NET Framework 4.8.1 result is below:

HDPI primary monitor

100% scan secondary monitor

Did it work in any of the earlier releases of .NET Core or .NET 5+?

From .NET 7.0 to the latest .NET 9.0, they are the same result. Only .NET 6.0 result is different and its result is below:

HDPI primary monitor

100% scan secondary monitor

Issue description

When launch project and open form on HDPI primary monitor, the Modal result is correct, but Modeless result is incorrect. Show Modal: correct HDPI-Modal

Show Modeless: incorrect HDPI-Modeless

When move the form to 100% scale secondary monitor, the Modal result is incorrect, but Modeless result is correct. Show Modal: incorrect 100-Modal

Show Modeless: correct 100-Modeless

https://github.com/dotnet/winforms/assets/45864985/1a9d508d-8189-49ec-a71f-3177ad442887

Steps to reproduce

  1. Open the Dialog_CenterParent.zip project on HDPI primary monitor.
  2. Build and run project.
  3. Click Show Modal button and check the position of the opened DialogForm, then close this DialogForm.
  4. Click Show Modeless button and check the position of the opened DialogForm, then close this DialogForm.
  5. Move Form1 to the 100% scale secondary monitor.
  6. Click Show Modal button and check the position of the opened DialogForm, then close this DialogForm.
  7. Click Show Modeless button and check the position of the opened DialogForm.
merriemcgaw commented 10 months ago

Thanks for filing the issue @Grebnietz! This will go on our backlog of HDPI issues. Was I correct in reading that calling CenterToParent() solves the problem for your app for now?

Grebnietz commented 10 months ago

Thanks for filing the issue @Grebnietz! This will go on our backlog of HDPI issues. Was I correct in reading that calling CenterToParent() solves the problem for your app for now?

@merriemcgaw : Yes, correct! Calling CenterToParent "resolvse" the issue.