How to make .NET 6 Windows Forms ignore DPI scaling?

993 views Asked by At

I am using Windows Forms to host DirectX-rendered content using SharpDX RenderForm. The issue is that when I set the form Size, it scales it by the scaling factor of my display, which is not what I need because I do pixel-based rendering.

I've tried to set form's AutoScaleMode to None, which appears to have no effect.

I also played with DpiAwareness in System.Windows.Forms.ApplicationConfigurationSection and the corresponding setting in the application manifest.

My goal is to do everything in pixels.

2

There are 2 answers

0
LOST On

This line run before creating the main form worked for me:

Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

The reason is that by explicitly declaring awareness of scaling you opt out from Windows Forms handling it for you by implicitly multiplying all sizes.

AutoScaleMode turned out not to be needed.

According to SetHighDpiMode documentation, the manifest method should have worked too, but I suspect some fluke prevented it from being picked up when launching the project from JetBrains Rider.

0
Mario On

I have the same issues.

I have a project in .Net6.0 with lot of controls inherited from the existing controls or a custom-maded one.

Below is what I have found.

  1. Application.SetHighDpiMode(HighDpiMode.PerMonitorV2) do not help in my project. The controls still resize based on the DPI scaling.

  2. Application.SetHighDpiMode(HighDpiMode.DpiUnaware) can prevent your winforms from DPI scaling. But the rendering becomes too blur to be accepted by the clients

  3. Changing the configuration in app.config is no uses. In .Net 6.0, adding the following configuration in app.config will cause ConfigurationErrorsException.

<System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

After a long time of research, I finally found solutions to ignore DPI scaling in .NET 6.0 for Winforms:

  1. Set AutoScaleMode to None for all your forms and controls.

  2. DO NOT USE POINT FOR FONT SIZE, Use Pixel instead. If you specify your font size in Point, it will scale-up based on your DPI setting. The scaling is ugly as your control may not scale in proportion to the font size, making words too large to fit in a control (e.g. a button, or a datagridview). In constrast, if you specify your font size in Pixel, the words do not scale based on your DPI setting.

  3. As a good practice, use layout controls (e.g. tablelayout, flowlayout), paddings and margins to position your controls. Although your controls do not scale, your main window does. Those give you relative positioning between controls, which triggers auto positioning onces users resize the window.