Handling DPI scaling in your .NET application’s forms

This topic isn't at all specific to AutoCAD โ€“ in fact it was encountered by Adam Nagy with the recent Plugin of the Month he developed for Revit โ€“ but it seemed something people might hit. Thanks to Adam for proposing this topic and providing the information to share with everyone. ๐Ÿ™‚

In recent versions of Windows โ€“ at least with Vista and Windows 7, but perhaps versions before that โ€“ it's possible for the user to adjust DPI Scaling options on their system as part of the usability/accessibility options. This can cause issues with how your dialogs are displayed, as sometimes the contents are scaled beyond the size of the dialog itself.

On Vista it's this setting is accessible via Control Panel โ€“> Ease of Access โ€“> Optimize visual display (or Personalization โ€“> Adjust font size (DPI), if in Classic View):

Getting to the settings on Vista

The DPI settings can be modified in the following dialog:

Vista DPI Scaling

On Windows 7, things look a little different (I don't have a Windows 7 machine, so am using a screenshot of Adam's for this):

Win7 DPI Scaling

As mentioned, the problem can potentially arise that DPI scaling leads to dialog content scaling beyond the dialog's limits. When the setting was at 150% Win7, Adam's Revit plugin's main form was cropped at the bottom.

I will admit, despite my efforts to reproduce this behaviour in a standalone application on Windows Vista, I have not been able to. A comparable dialog looks like this at 96 and 120 DPI, respectively:

The dialog at 96 DPI

The dialog at 120 DPI

 

 

 

 

 

Obviously neither are cropped, but presumably scaling further โ€“ and perhaps the fact the plugin is hosted in Revit rather than standalone โ€“ can cause unwanted content cropping.

The solution Adam found was to edit the form's properties, to make sure it a) AutoSized and b) used the Dpi AutoScaleMode:

Form properties

This scenario was new to me, but I do suggest testing your applications with the DPI scaling set to 150%, to make sure it doesn't cause a problem for your GUI.

A quick tip that saved me time during my failed efforts to reproduce the erroneous behaviour: after changing the setting you can simply log out rather than performing a full reboot (as was recommended by Vista, at least).

4 responses to “Handling DPI scaling in your .NET application’s forms”

  1. J. Daniel Smith Avatar
    J. Daniel Smith

    My understanding about the "root cause" of this problem/feature is that--unlike CRTs--LCDs look best at their "native resolution".

  2. Indeed - thanks for mentioning that, Dan.

    Kean

  3. In addition, it seems as though AutoCAD does some setting of fonts through the palette set. Even when using 96dpi font size, my fonts were larger on the palette than in my design view in VS.

    The fonts for all controls need their fonts set manually, so they do not inherit the size the palette wants to use:

    For Each ctrl As Control In myctrl1.Controls
    ctrl.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25)
    Next

  4. I was having a related issue with dot net forms on a windows 7 machine which had default DPI of 125%. In particular datagridviews inside of TabStrips were not sizing correctly to fill the TabStrips page. I tried switching the AutoScaleMode property of the form to Dpi, and it did not help. Surprisingly what fixed the issue was switching the AutoScaleMode to None. (The default AutoScaleMode was Font).

Leave a Reply

Your email address will not be published. Required fields are marked *