Skip to content

Fix incorrect ClientSize calculation in PerMonitorV2 mode when form moves between monitors with different DPI - #14277

Merged
LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_12132_PerMonitorV2_autoscale_lead_to_wrong_ClientSize
Aug 31, 2026
Merged

LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_12132_PerMonitorV2_autoscale_lead_to_wrong_ClientSize

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Feb 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #12132

Root Cause

The original code in OnGetDpiScaledSize applied the font-based autoScaleFactor to the entire window Size (including non-client area):

desiredSize.Width = (int)(Size.Width * autoScaleFactor.Width);
desiredSize.Height = (int)(Size.Height * autoScaleFactor.Height);

However, the non-client area (title bar, borders) is scaled linearly by DPI ratio by Windows, not by the font-based auto scale factor. This mismatch caused incorrect ClientSize calculations when forms transitioned between monitors with different DPI settings.

Proposed changes

When calculating the desired window size during DPI transitions in OnGetDpiScaledSize, the non-client area (title bar and window borders) should be excluded from the autoScaleFactor calculation since Windows scales them linearly by DPI ratio, not by the font-based auto scale factor.

The fix:

  1. Uses AdjustWindowRectExForDpi to calculate the non-client area size at both old and new DPI
  2. Subtracts the old DPI's non-client area from the current Size to get the client area
  3. Scales only the client area by autoScaleFactor
  4. Adds the new DPI's non-client area to get the final desired size

Customer Impact

  • After the fix, the ClientSize difference is reduced to 0-1 pixels (the remaining 1 pixel is an inherent rounding error from font measurement in AutoScaleMode.Font).

Regression?

  • No

Risk

  • minimal

Screenshots

Before

image

After

image

Test methodology

  • Manually

Test environment(s)

  • .net 11.0.0-preview.2.26080.101
Microsoft Reviewers: Open in CodeFlow

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a long-standing bug in PerMonitorV2 DPI scaling where forms moving between monitors with different DPI settings would have incorrect ClientSize values. The issue was that the original code applied the font-based autoScaleFactor to the entire window size (including non-client area), but Windows actually scales the non-client area (title bar, borders) linearly by DPI ratio, not by the font-based factor.

Changes:

  • Modified OnGetDpiScaledSize method to separately calculate non-client area sizes at both old and new DPI values
  • Changed calculation to apply autoScaleFactor only to the client area, not the entire window
  • Added Math.Round to minimize rounding errors during DPI transitions

@codecov

codecov Bot commented Feb 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.22222% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.17894%. Comparing base (9b546b7) to head (29f9636).
⚠️ Report is 74 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #14277         +/-   ##
===================================================
- Coverage   77.18395%   77.17894%   -0.00501%     
===================================================
  Files           3279        3279                 
  Lines         645138      645154         +16     
  Branches       47730       47731          +1     
===================================================
- Hits          497943      497923         -20     
- Misses        143503      143539         +36     
  Partials        3692        3692                 
Flag Coverage Δ
Debug 77.17894% <72.22222%> (-0.00501%) ⬇️
integration 18.99167% <72.22222%> (+0.00103%) ⬆️
production 52.05334% <72.22222%> (-0.00986%) ⬇️
test 97.40479% <ø> (ø)
unit 49.48632% <0.00000%> (-0.01631%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Apr 22, 2026
@KlausLoeffelmann

KlausLoeffelmann commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Hey, quick question:

Is this really only in PMV2 or can it also be in SystemAware, namely when I design a Form in 150% and then I start it in 300%?

@Olina-Zhang, @LeafShi1 - can you test this, because I think there is something off between NetFx and .NET, particularly, when the Form's Font is not the standard 9pt but let's say 12pt.

@KlausLoeffelmann KlausLoeffelmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@LeafShi1
LeafShi1 merged commit a882696 into dotnet:main Aug 31, 2026
15 checks passed
@dotnet-policy-service dotnet-policy-service Bot removed the waiting-review This item is waiting on review by one or more members of team label Aug 31, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 1, 2026
abineshPalanisamy pushed a commit to abineshPalanisamy/winforms that referenced this pull request Sep 3, 2026
…oves between monitors with different DPI (dotnet#14277)

## Proposed changes

When calculating the desired window size during DPI transitions in `OnGetDpiScaledSize`, the non-client area (title bar and window borders) should be excluded from the `autoScaleFactor` calculation since Windows scales them linearly by DPI ratio, not by the font-based auto scale factor.

**The fix:**

1. Uses `AdjustWindowRectExForDpi` to calculate the **non-client** area size at both old and new DPI
2. Subtracts the old DPI's non-client area from the current Size to get the client area
3. Scales only the client area by `autoScaleFactor`
4. Adds the new DPI's non-client area to get the final desired size
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Multi-Monitor] PerMonitorV2 autoscale lead to wrong ClientSize.

6 participants