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

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.


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


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;