9

I'm running into problems with getting a WinForms app to display correctly at high DPI settings. I've checked various web sites, and the WinForms all have the correct AutoScaleMode. I've tried setting this to both DPI and Font. However, the forms always get cut off near the bottom when using high DPI settings (e.g. 125%).

I added some code to check, and if I set AutoScaleMode to DPI, when the form loads, I see that AutoScaleDimensions is 120,120 when the form loads, and CurrentAutoScaleDimensions is also 120,120. In the Form.designer.cs file, there is a line to set AutoScaleDimension to 96,96.

If I set AutoScaleMode to Font, then I can see in designer that AutoScaleDimension is correctly set to new System.Drawing.SizeF(6F, 13F), but when the form loads, both AutoScaleDimension and CurrentAutoScaleDimension are set to 8F,16F.

This app mixes some WPF with WinForms, and the WPF screens appear first. So based on DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application I tried setting the TextFormattingMode for the application, and for the WPF screens that show first, to "Display", but it makes no difference.

I am, frankly, at a loss as to what's causing this. I suppose I could add code to resize things programmatically by detecting the DPI at runtime, but I shouldn't have to do that. The AutoScaleMode (and related) properties are supposed to make this fairly automatic. So what else should I be checking for that might cause this problem?

Community
  • 1
  • 1
WarnerYoung
  • 233
  • 1
  • 3
  • 10
  • 125% is not "High DPI". WPF definitely has an impact, because it sets the High DPI aware flag at runtime, even if it isn't set in the manifest. WinForms might be confused about whether it is supposed to do the scaling or let the OS do so automatically. – Ben Voigt Jul 01 '14 at 16:54
  • Ben, sorry, I suppose "higher DPI" might be more accurate. I need to look at this in more detail, because the problem persisted even after I changed the app so we don't display the WPF screens first. – WarnerYoung Jul 02 '14 at 17:23
  • The modern %based settings make the whole higher/lower thing confusing. "Dots Per Inch" is lower for larger fonts. Old school DPI settings a lower number was used for higher fonts, newer versions of Windows assume a standard DPI and use a font size percentage so a higher value is used for higher fonts. It's generally less confusing to use actual numbers rather than higher/lower. – Denise Skidmore Jul 10 '14 at 21:53
  • Was this problem ever solved? I am experiencing something very similar which persists even after adding the code to the application manifest as specified below. – Anya Hope Jan 20 '16 at 14:17
  • No, at least I was never able to fix it, and I had to move on to other things. In my case, I think it was because I had a mix of WinForms and WPF stuff mixed together. Eventually, if I can get that cleaned up, I'll take another look. Sorry I don't have a better answer. – WarnerYoung Jan 21 '16 at 16:59
  • [this solution worked for me](https://stackoverflow.com/a/36344413/4542599) Add "[assembly: DisableDpiAwareness]" to AssemblyInfo.cs – Maged Ragheb Mar 13 '19 at 14:55

2 Answers2

10

I had a similar problem just a few days ago. After a couple of hours research, I finally found a very simple solution -- adding <dpiAware> to the application manifest. Here is an example from Microsoft's website.

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>True</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
</assembly>

For my case, I need to set the <dpiAware> to Per-monitor to make it work normally. That is, change the line in the middle to <dpiAware>Per-monitor</dpiAware>.

The differences between each value are listed below(These are from MSDN):

  • False -- Sets the application to not DPI-aware.
  • True -- Sets the application to system DPI–aware.
  • Per-monitor -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to not DPI–aware.
  • True/PM -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to system-DPI aware.
Lucenty
  • 664
  • 1
  • 7
  • 18
Anthony Huang
  • 566
  • 6
  • 7
0

Is it possible that with the monitor size and the higher DPI setting, that the screen is simply no longer big enough to display your entire form? I say that because I'm developing a 1024 x 768 winforms app, and playing around with user DPI settings. If I set the DPI to 150% I can no longer see the bottom portion of the form on my monitor, yet the app is scaling correctly.

Jeff
  • 739
  • 12
  • 31
  • Good point, Jeff, but no, that's not what's happening here. The forms are still small enough to fit on screen, just not sized correctly. – WarnerYoung Jul 02 '14 at 17:24