dotnet / dotnet/winforms

Weird Control.Margin values when screen DPI is greater than 100%

Open
#10,402 1 comment 0 reactions 1 assignee Claimed by @LeafShi1 View on GitHub
area-HDPI
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

.NET8.0, .NET6.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+?

No

### Issue description

[TestUpDown.zip](https://github.com/dotnet/winforms/files/13511266/TestUpDown.zip)
I have done a simple test project with NumericUpDown and Button controls.
1. Controls are placed direct to the Form.
2. Controls are placed on a TableLayoutPanel (Parent is the Form)
3. Controls are placed on a Panel (Parent is the Form)
4. Controls are placed on a Panel (Parent is an other Panel which is placed on the Form)

When pressing the Buttons one gets a MessageBox with information about DisplayRectangle and Margin values for the Button and NumericUpDown.
When you run with 100% screen DPI the Margin values are all set to 3. But when you run with a higher DPI the Margin values are weird. For example in test case 4 with DPI=150% I get NumericUpDown Margin values 9, 13, 9, 13 and for the Button I get 4, 5, 4, 5.

In the project I have set ApplicationHighDpiMode to PerMonitorV2. But it seems that the issue is also present in other DpiModes.

### Steps to reproduce

Run the project with different DPI values and press the Buttons and notice the MessageBox values.
Form at 100% DPI
![TestUpDown](https://github.com/dotnet/winforms/assets/28147549/925a5448-ecd9-4010-94eb-8aa560dcf1a1)

MessageBox with DPI=150% when pressing button4
![TestUpDown150MessageBox](https://github.com/dotnet/winforms/assets/28147549/b356ab33-fbf3-4f22-83fb-e99456601f21)

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.