dotnet / winforms

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

Anchored controls scale incorrectly on multi-monitor setup using HDPI PerMonitorV2 #10958

Open Rsge opened 6 months ago

Rsge commented 6 months ago

.NET version

.NET 8.0

Did it work in .NET Framework?

Not tested/verified

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

On .NET 6 the issue isn't present this severely but present in a different way

Issue description

When creating a WinForms app targeting .NET 8.0 with an auto-scaleable element anchored to at least two opposing sites on a form (e.g. a button or text box) and the project being set to HDPI mode PerMonitorV2, the element will wildly change it's size (up to being completely invisible) when dragged between monitors with different HDPI settings (e.g. one scaled to default 100 %, one to 150 %).

Setup

grafik grafik grafik

.NET 8.0

Loaded on 100 % monitor: grafik Dragged to 150 % monitor first time: grafik Dragged back to 100 % monitor first time: grafik Dragged back to 150 % monitor second time: grafik Dragged back to 100 % monitor second time: grafik Dragged back to 150 % monitor third time: Same as second time.

.NET 6

Loaded on 100 % monitor: Same as in .NET 8. Dragged to 150 % monitor first time: grafik Dragged back to 100 % monitor first time: grafik Dragged to 150 % monitor second time: Same as first time.

PS

I think this issue is different from e.g. #8157 because that one is explicitly about elements in SplitContainers, while mine is about elements directly on forms.

Steps to reproduce

  1. Create a new WinForms project targeting .NET 8.0.
  2. Add a button to the form.
  3. Drag it bigger so it "anchors" to the edges.
  4. Set the "Anchor" property to "Top, Bottom, Left, Right".
  5. Start the program on a 100 % scaled monitor.
  6. Drag it to a higher scaled (e.g. 150 %) monitor.
  7. Drag it back to the 100 % monitor.

Example Project: Test.zip

elachlan commented 6 months ago

@Olina-Zhang could your team please test this?

John-Qiao commented 6 months ago

@elachlan yes, we can repro this issue with .NET 8.0 and latest .NET 9.0.100-preview.3.24127.9 SDK, please check below video.

Launch test app on 100% primary monitor and then move form to 175% secondary monitor.

https://github.com/dotnet/winforms/assets/45864985/e09c4b32-0391-4fa3-8063-35ea34291873

In above video result, I tried to add the runtimeconfig.template.json file with below codes in test project, that can improve most problems, but not solve all issues.

{
  "configProperties": {
    "System.Windows.Forms.AnchorLayoutV2": true
  }
}

Launched on 100% primary monitor 100-addJsonFile

Moved to 175% secondary monitor MoveTo175Secondary-addJsonFile

WolfgangHG commented 2 months ago

Attached is another .NET8 sample where moving the form from screen 1 at 100% to screen 2 at 125% and back causes broken layout. DpiAwarenessHighDpiModePMV2.zip

I tested it with System.Windows.Forms.AnchorLayoutV2 = true and false in "runtimeconfig.template.json":

{
  "configProperties": {
    "System.Windows.Forms.AnchorLayoutV2": true
  }
}

Both modes show different behavior, but the result is broken for both of them. This blocks us from supporting DPI awareness in our application (since our first attempt to do so in 2016 :-( ).

This is the result for AnchorLayoutV2 = true" - start with the top left image, a bit below to the right is the result of moving the form to the 125% screen, then below to the left the form back at 100% and so on: AnchorLayoutV2_true

And this is the result for "AnchorLayoutV2 = false" (old behavior). AnchorLayoutV2_false

I also noted that the form has different sizes sometimes when moving to the 125% screen.

I don't like the fact that this issue has a milestone "Future" - as I wrote above this blocks us from activating dpi awareness. I want to avoid redesigning all forms in our app and replace Anchor layouts with Dock.

Dave-Lowndes commented 1 week ago

I've also encountered this issue. Bug report here closed in favour of this one. The PerMonitorV2 & PerMonitor settings were similarly affected for me. I've resorted to using the SystemAware setting as the best compromise until this is fixed.