dotnet / dotnet/winforms

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

Open
#10,422 4 comments 0 reactions 0 assignees View on GitHub
:beetle: bug :construction: work in progress area-HDPI
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .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)](https://github.com/dotnet/winforms/assets/87719952/95605061-9064-446b-a8af-e2e346b19023)

![Screenshot (76)](https://github.com/dotnet/winforms/assets/87719952/f87d5baa-130c-4f10-b91d-e73bfa8510b5)

The fix of issue [#5729](https://github.com/dotnet/winforms/issues/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](https://github.com/dotnet/winforms/files/13546509/Dialog_CenterParent.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.