2

In my .NET 4.7 Windows Forms application, I configured high DPI support by following the instructions here.

I configure my Windows like this:

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; // I also tried with AutoScaleMode.Dpi

I layout my form with TableLayoutPanel and leave the default font settings on the form and all containers. Only my controls (Label, Button) have custom fonts set.

When I change the scale factor of the display, the form, layout and controls are all scaled accordingly. However font sizes are not scaled; the size of the text remains the same.

What is needed to make WinForms autoscale fonts alongside everything else?

100% scaling:

100% scaling

150% scaling - control is resized but font size remains constant:

150% scaling - control is resized but font size remains constant

For reference, here is my app manifest:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
  </compatibility>
</assembly>

and my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
  </System.Windows.Forms.ApplicationConfigurationSection>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.9.0" newVersion="3.1.9.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.9.0" newVersion="3.1.9.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.9.0" newVersion="3.1.9.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
TheNextman
  • 12,428
  • 2
  • 36
  • 75
  • I also have a WinForm project on .NET 4.7 and I've completely dropped the ball on High DPI. There is tons of exception / control / style that doesn't work. I can't understand even why where is not a simple way to just disable it. – Zyo Mar 15 '21 at 17:41
  • Can you show what you have set in `app.config` (or `app.manifest`)? Note that your Form is scaling based on the Font size, not DPI (when the app is DpiAware, the System won't *virtualize* the app's dimensions, so it's up to you). Did you add some elements that inherit the Font from the Form (or another container that inherits from the Form)? @Zyo It's disabled by default. You need to opt in. – Jimi Mar 15 '21 at 17:47
  • @Jimi my manifest and app.config are exactly as the linked MSDN article (`PerMonitorV2` and Windows 10 compatibility. Visual styles are enabled). Nothing is inheriting the font, the only place I specify a font is on controls. However - even leaving default font *everywhere*, text is the only thing **not** scaled... – TheNextman Mar 15 '21 at 17:52
  • It could be useful to see it anyway (also for other *readers*). Note that the `AutoScaleDimensions` is hard-coded to the 96 Dpi representation. – Jimi Mar 15 '21 at 18:03
  • @Jimi I added both to the question – TheNextman Mar 15 '21 at 18:11
  • Does this answer your question? [How to write WinForms code that auto-scales to system font and dpi settings?](https://stackoverflow.com/questions/22735174/how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings) – Peter Duniho Mar 16 '21 at 03:11
  • Does this answer your question? [How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?](https://stackoverflow.com/questions/13228185/how-to-configure-an-app-to-run-correctly-on-a-machine-with-a-high-dpi-setting-e) – Peter Duniho Mar 16 '21 at 03:12
  • Does this answer your question? [How to control the font DPI in .NET WinForms app](https://stackoverflow.com/questions/185804/how-to-control-the-font-dpi-in-net-winforms-app) – Peter Duniho Mar 16 '21 at 03:12
  • See proposed duplicates for how to actually do this correctly. Note that you still need to set the font scaling on individual controls, and some controls don't implement font scaling correctly, or don't do so except in specific configurations. If you still need help, post a question that includes a proper [mcve], along with a detailed explanation of what you've tried already and what specifically you need help with. – Peter Duniho Mar 16 '21 at 03:13
  • @PeterDuniho > "Note that you still need to set the font scaling on individual controls" What does this mean, exactly? Most (all) of the controls I looked at don't have a font scaling property? – TheNextman Mar 16 '21 at 03:24
  • Most don't, yes. See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.autoscalemode, and of course https://learn.microsoft.com/en-us/dotnet/framework/winforms/automatic-scaling-in-windows-forms – Peter Duniho Mar 16 '21 at 03:31
  • Not really helpful. I understand the "RTFM" sentiment; so [here](https://stackoverflow.com/questions/66648826/textbox-not-scaled-on-net-4-7-2-and-windows-10) is a follow-up question that contains a link to a minimal, reproducible example of a specific issue I'm facing with high DPI support in modern .NET / Windows. – TheNextman Mar 16 '21 at 03:48

0 Answers0