Landin Martens
Landin Martens

Reputation: 3333

Detect Windows font size (100%, 125%, and 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 started using my application. The entire application breaks on Chinese Windows 7. From what I can tell (I can't really test it for I only have the English version and installation the language packs does not cause the problem) Chinese characters 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 somehow Chinese versions skip right by this, and the form destroys itself, 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 DPI? I need something solid that will work on all Windows 7 operating systems and languages.

Upvotes: 47

Views: 81669

Answers (4)

mahdi sharbaty
mahdi sharbaty

Reputation: 158

get system DPI scale using this:

Read from registry AppliedDPI dword located in Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics. Then divide it by 96.

try
{
    double scale = 1.0;
    using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop\\WindowMetrics"))
    {
        if (key != null)
        {
            Object o = key.GetValue("AppliedDPI");
            if (o != null)
            {
                int value = (int)o;
                scale = (double)value / 96.0;
            }
        }
    }
}
catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
{
    //react appropriately
}

for 100% --> value is 96 scale is 1.0

for 125% --> value is 120 scale is 1.25

for 150% --> value is 144 scale is 1.5

now you can resize your form and set new font size by this scale automatically;

Upvotes: 2

user11581995
user11581995

Reputation: 1

if your on a newer version of windows I recommend reinstalling your graphics card drivers ( e.g installing a newer version) I had the same problem, my display scale was set to 100% but the font was way off. hope this fixes your problem

Upvotes: -3

nspire
nspire

Reputation: 1721

For C++/Win32 users, here is a good reference: Writing High-DPI Win32 Applications.

Upvotes: 5

Cody Gray
Cody Gray

Reputation: 244782

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.

Upvotes: 19

Related Questions