dotnet / dotnet/winforms

Font autoscaling doesn't function in .NET Core as it does in .NET Framework 4.8.

Open
#11,214 30 comments 0 reactions 0 assignees View on GitHub
area-HDPI
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### Description

I've just migrated a Winform project from .NET Framework 4.8 to .NET Core 9, following the porting instructions provided in the Windows documentation. However, I've encountered an issue with font autoscaling. When I adjust the font for the entire application, the controls on the form don't scale appropriately.

### Reproduction Steps

1. Create small Multi-windows winform application (an app which can open multiple forms in the same app) based on .NET Framework 4.8 having a single form with basic ui control like labels, buttons, textboxes.
2. Create a form of changing font of entire application in the application.
3. Port above .NET 4.8 project to using .NET core with latest version (Also, keep copy of .Net 4.8 version)
4. Run both project, open a form, observe controls properties before and after changing font size.
5. For reference, .NET 4.8 uses default 'Microsoft san serif' with 8.25 Small font size and .Net core uses 'Segoe UI' with 9 small font size.

### Expected behavior

In a NET Core project, after changing the font, the properties of the controls like SizeX, SizeY, LocationX, and LocationY should be set to the same values as in the NET 4.8 project during events like onLoad, onShown, and FontChanged.

I have attached my application expected screenshot.
![Correct](https://github.com/dotnet/runtime/assets/165039085/fc572dcb-892e-47cb-b355-e75fbbefe206)

### Actual behavior

In a NET Core project these properties are not getting set correctly, which ultimately causing winform UI to set weirdly.

I have attached my application actual screenshot.
![weird](https://github.com/dotnet/runtime/assets/165039085/6efbbeaf-84ca-4039-939b-8a42ffc7dcc4)

### Regression?

_No response_

### Known Workarounds

![4 8 code](https://github.com/dotnet/runtime/assets/165039085/7807b628-d56d-4c1a-8de5-06d65dfa3d7b)

Above is NET 4.8 code from System.Windows.Forms->Systems->Windows->Forms->Layout->Containers->ContainerControls.cs

and below is the same code from NET Core

![Core code](https://github.com/dotnet/runtime/assets/165039085/869cf2e4-ed38-45ec-a764-e05e2b7eb64d)

Look at difference, because of base.IsHandleCreated NET Core doesn't allow to take effect of FontChanged.
if we remove that ` base.IsHandleCreated` from code and test the application it works fine.

### Configuration

.NET version- 4.8 and .NET Core
Windows 10
x64 Architecture

### Other information

_No response_

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.