Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to react to user's dpi settings in a .NET application

Problem description:

I am currently developing a Visual Basic .NET application. It turns out that my application does not get displayed properly on systems that run on a different dpi-setting than "standard" (100% = 96dpi under windows xp).

When I change the system's dpi settings my GUI looks slightly messed up as you can see in the following screenshots (since this is a scaling issue it should not matter that the text on the screenshots is in German; sorry for this):

Windows XP, 100% scale, 96 dpi -> Everything looks like I expect it to. Windows XP, 100% scale, 96 dpi

Windows XP, 125% scale, 120 dpi -> The buttons at the bottom of the window do no longer fit inside the window. Windows XP, 125% scale, 120 dpi

What I want to achieve:

In case the display scale is set to anything else than 100% I would like to show the user a warning dialog box as soon as the application starts.

Question:

Is there any possibility to read the system's dpi value? If so, how would that be? There seems to be a DisplayProperties-class as well as DisplayInformation-class, but as far as I can see it is only available for Windows-Store-Apps.

like image 503
Christian Avatar asked Jan 16 '14 15:01

Christian


People also ask

How do I know my DPI scaling?

Select Display > Change the size of text, apps, and other items, and then adjust the slider for each monitor. Right-click the application, select Properties, select the Compatibility tab, and then select the Disable display scaling on high DPI settings check box.

What is DPI C#?

The dpi value indicates how many pixels there are in a inch (or ppi - pixels per inch).

How do you apply DPI?

Click on the ''Devices'' option in the settings menu. Click on the ''Mouse'' option and click on “Additional mouse” options. A window will open. Now, click on the ''Pointer'' option and move the slider to make changes in the DPI.

What does enable high DPI Scaling do?

High-DPI scaling improvements on Windows 10 The new option is called "System (Enhanced)," and when enabled the text and interface will look crispier and elements will be sized correctly. Though, some parts of the app may continue to look a little blurry, but it's still a significant improvement.


1 Answers

The DpiX and DpiY properties of the Graphics class contain this information:

Using g = Me.CreateGraphics()
    Dim dpiX = g.DpiX
    Dim dpiY = g.DpiY
End Using

Since you are using Windows Forms, you can create a Graphics object with Form.CreateGraphics.


Of course, the best course of action would be to make your application look great on any DPI setting. If you cannot move to WPF, the following SO question contains a lot of hints on how to make this work with WinForms:

  • Creating a DPI-Aware Application
like image 154
Heinzi Avatar answered Oct 13 '22 22:10

Heinzi