dotnet / dotnet/winforms

PageSetupDialog incorrectly converts Margin units from pageSetting.Margins to dialogSettings.rtMargin

Open
#14,482 6 comments 0 reactions 1 assignee Claimed by @LeafShi1 View on GitHub
needs-area-label
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

10.0.201

### Did it work in .NET Framework?

Yes

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

Did not test.

### Issue description

Method `PageSetupDialog.RunDialog(IntPtr hwndOwner)` incorrectly detects `toUnit` based on Windows regional settings.

In Windows for my user account, `Language & region > Number settings, Measurement System` is set to `Metric`. When I open `PageSetupDialog` from my .Net application, it displays Margins group with caption "Margins (millimeters)".
Every time I open this dialog, don't change anything, and close it - margin values are reduced until they become 0.

After analyzing PageSetupDialog.cs and PrinterUnitConvert.cs, checking values of `PrintDocument.DefaultPageSettings.Margins` and what is shown in the dialog fields, and doing manual calculations, I see that:
1. In method `RunDialog` original values from `pageSettings.Margins` converted using `toUnit` == `PrinterUnit.ThousandthsOfAnInch`.
2. In method `UpdateSettings` new values converted to `pageSettings.Margins` using `fromUnit` == `PrinterUnit.HundredthsOfAMillimeter`.

Comparing with source code for .Net Framework 4.8, I see that there is difference in getting locale info:
- .Net Framework 4.8 checks `NativeMethods.LOCALE_USER_DEFAULT` - user level;
- .Net 10 checks `PInvoke.LOCALE_NAME_SYSTEM_DEFAULT` - system level;

I suspect the issues is here - logic of PageSetupDialog checks system level locale settings, but dialog itself when run by Windows uses user level locale settings (and sets flag PSD_INHUNDREDTHSOFMILLIMETERS to not 0), so it results in display discrepancy and different conversion units used with original and new values.

### Steps to reproduce

Set `Language & region > Number settings, Measurement System` on system level to `U.S.`,
and on user level to `Metric`.

Additional note: I set `PageSetupDialog.EnableMetric = true;`, but it does not work with both `true` and `false`.

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.