Landin Martens Landin Martens - 1 year ago 478
C# Question

Detect windows font size (100%, 125%, 150%)

I created an application that works perfectly until the user selects 125% or 150%. It would break my application. I later found a way to find the font size by detecting the DPI.

This was working great until people with Chinese versions of windows 7 starting using my application. The entire application breaks on Chinese windows 7. From what I can tell (I cant really test it for I only have English version and installation the language packs does not cause the problem) Chinese chars are causing a weird DPI that breaks my application.

my current code works like this:

if (dpi.DpiX == 120) //For 125% fonts
//Resize form and set default font to correct problems
else if (dpi.DpiX == 96) //For 100 and 150% fonts
//Resize form and set default font to correct problems

On English versions of windows 7 that works great, but some how Chinese versions skip right by this, and the form destroys it self, with controls not even showing up, font extremely large and pushing past the problem, picture boxes being moved around.

So what is a good way to detect the windows font scale (100%,125% and 150%) without detecting API. I need something solid that will work on all windows 7 operating systems and languages.

Answer Source

The correct way of handling variable DPI settings is not to detect them and adjust your controls' sizes manually in a switch statement (for starters, there are far more possibilities than those you show in your sample if statement).

Instead, you should set the AutoScaleMode property of your form to AutoScaleMode.Dpi and let the framework take care of this for you.

Add the following code to your form's constructor (or set this property at design time):

this.AutoScaleMode = AutoScaleMode.Dpi;

Although you might prefer to use AutoScaleMode.Font. For more information on automatic scaling, see the MSDN documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download