Detect Windows font size (100%, 125%, and 150%)

77,774

Solution 1

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.

Solution 2

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

Solution 3

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;

Share:
77,774

Related videos on Youtube

Landin Martens
Author by

Landin Martens

Updated on July 09, 2022

Comments

  • Landin Martens
    Landin Martens almost 2 years

    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.

    • Landin Martens
      Landin Martens about 12 years
      I was typing really fast and made a few spelling mistakes. I meant to say "DPI" not "API". Sorry
    • Yahia
      Yahia about 12 years
      What you describe sounds much more like a problem with the video driver and/or installed fonts...
    • Tigran
      Tigran about 12 years
      Just suggession: if you have seriouse ammount of chinese clients, it worths to invest some time to create at least VirtualMachine for VirtualBox or VmWare, so you can test your app on it with as closest fedelity to the real os, as it is possible.
    • Landin Martens
      Landin Martens about 12 years
      The problem is I can't find a chinese version of windows 7 to use for the 30 day trial period. If you could link me I may be able to fix this.
    • Tigran
      Tigran about 12 years
      You can use your own version of windows, instal chinese language pack, set regional settings to your clients location and test. This will take one day or more
    • David Heffernan
      David Heffernan about 12 years
      Why is 150% denoted by 96dpi and why do you think there are only 3 dpi settings? Users can set lots of other values.
    • dsum
      dsum almost 9 years
      I just want to say that this situation also happen to an English app that I work on. The user set the font scale to 125% and Windows 7 somehow hides controls (dropdown, checkbox, label) in the middle section of the WinForm. The dev team thought there was a configuration issue that dynamically hides the middle section, but it turns out it was the font scale causing the user not able to see the control on a fixed height WinForm.
  • Landin Martens
    Landin Martens about 12 years
    I tried doing that, but the problem is my application messes up with that
  • Cody Gray
    Cody Gray about 12 years
    @user What does "messes up" mean? What exactly goes wrong? How have you tried debugging it? Do you use relative positioning for your controls, like by placing them in containers and setting the Anchor and Dock properties? Absolute positioning will never work, even when the user changes their default font face/size, much less when DPI gets involved.
  • Landin Martens
    Landin Martens about 12 years
    using AutoScaleMode.none fixed my problem!
  • harry
    harry almost 12 years
    @Landin AutoScaleMode.None is exactly what you shouldn't be using.
  • TaW
    TaW over 9 years
    using AutoScaleMode.None fixed my problem, too. In theory no promising, in reality it helped against problems with 125% font enlargement.
  • airstrike
    airstrike over 9 years
    Here is another bit of reference technet.microsoft.com/en-us/library/dn528846.aspx
  • ouah
    ouah almost 8 years
    While that's a great suggestion for native controls it's not ideal if you're integrating with some other components that may not respect those settings. A Web Browser control for example. For that use case you may still want to know the actual scale ratio so you can adjust the browser display font size and scaling mode.
  • Dave Cousineau
    Dave Cousineau over 2 years
    I've tried changing these values and they do nothing. I can tell that the various parts of my code that set absolute sizes are where things need changing. If I just had a scale factor to multiply by I could correct the sizes, but AutoScaleFactor is always (1, 1), and AutoScaleMode doesn't do anything no matter what I set it to.