[ACCEPTED]-How do I force a Windows Forms C# application to ignore when a user choose 125% or 150% for the OS font size?-winforms

Accepted answer
Score: 28

Here is what worked for me...

        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
        this.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));

The above two 25 lines are copied from my BaseForm.Designer.cs 24 file, but basically I found two easy steps 23 to get "no font scaling":

  1. Set AutoScaleMode 22 to None.

  2. Use "Pixel" as the Unit Type for 21 all Fonts, instead of the default Point 20 value.

As far as if you should let Windows 19 scale your fonts or not, that's up to you. I 18 for one don't like the design, so if I feel 17 my application needs to be scaled, I'll 16 do it myself, with my own options and design.

Over 15 the years of speaking with actual end-users, I've 14 also found that most of them have no idea 13 about DPI settings, and if they have anything 12 other than the default set, it wasn't because 11 they wanted it that way... and they just 10 never noticed because all they use is the 9 web browser and maybe Excel and Microsoft 8 Word (which use whatever font they set it 7 to).

If my application had respected the 6 system font settings, they wouldn't have 5 liked it as much == less sales, because 4 it would have had this huge ugly font like 3 the system dialogs do (and they don't know 2 how to change it, but they don't care about 1 system dialogs they never use).

Score: 18

The problem is that the Font property of 41 a Form or a control specifies the font size 40 in Points. That's a measurement that affect 39 the height of the letters when the DPI setting 38 changes. One point is 1/72 inches. The 37 default DPI, 96 dots per inch and a font 36 size of 9 points yields a letter that is 35 9 / 72 x 96 = 12 pixels high.

When the user 34 bumps up the DPI setting to, say, 120 DPI 33 (125%) then the letter becomes 9 / 72 x 32 120 = 15 pixels high. If you don't let 31 the control get larger then the text won't 30 fit in the control anymore. Very ugly to 29 look at.

The Form.AutoScaleMode property 28 solves this problem. It checks at which 27 size the form was designed and compares 26 it against the DPI on the machine on which 25 it runs. And resizes and relocates the 24 controls to ensure this kind of clipping 23 won't happen. Very useful, it is completely 22 automatic without you having to do anything 21 about it.

The typical problem is the "relocates" bit 20 in the previous paragraph. If you give 19 controls their own font size instead of 18 inheriting the size of the form or if the 17 automatic layout of the form isn't kosher 16 then controls may end up in the wrong spot, destroying 15 the organized look of the form.

You need 14 to fix that, it isn't clear from your question 13 what the source of the problem might be. Trying 12 to prevent this auto-scaling from doing 11 its job is not sustainable. You'll have 10 to iterate all of the controls in the form 9 and change their Font, picking a smaller 8 font size. This is however going to get 7 you into trouble a couple of years from 6 now, if not already. Your user is going 5 to complain about having to work with a 4 postage stamp.

The easiest way to debug the 3 layout problem, avoiding the pain of constantly 2 changing the DPI size, is to temporarily 1 paste this code into your form class:

    protected override void OnLoad(EventArgs e) {
        this.Font = new Font(this.Font.FontFamily, this.Font.SizeInPoints * 125 / 96);
        base.OnLoad(e);
    }
Score: 6

I found a pretty easy workaround.

this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

This will 2 make the text the same size regardless of 1 the autoscale size.

Score: 2

Going against the user wishes like this 9 is not something MS eagerly accommodates.

Rather 8 than fixing the symptom (your app not scaling 7 correctly), would it not be probably just 6 as quick to fix the problem? When a user 5 selects larger fonts, it's usually because 4 they need the larger letters to be able 3 to read them; so scaling correctly is more 2 important than not scaling and remaining 1 illegible to the user.

Score: 2

No. GDI and Windows Forms are resolution 8 dependent. The best option is to design 7 your layouts in a way that they scale appropriately 6 when a window is resized. This tends to 5 allow font size scaling to work correctly, as 4 well, as the layouts will adjust as needed.

This, by 3 the way, is one of the big improvements 2 in WPF - it's designed to be resolution 1 independent.

Score: 2

I suspect you already tried

yourform.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;

in your forms, which 14 prevents your forms from scaling, but not 13 your fonts. So what you have to do (even 12 if it is not recommended, see the other 11 posts) is setting the font size for all 10 controls in your form to a fixed size:

Here 9 is a code snippet to grab all controls of 8 a Form:

    public List<Control> FindAllControls(Control container)
    {
        List<Control> controlList = new List<Control>();
        FindAllControls(container, controlList);
        return controlList;
    }

    private void FindAllControls(Control container, IList<Control> ctrlList)
    {
        foreach (Control ctrl in container.Controls)
        {
            if (ctrl.Controls.Count == 0)
                ctrlList.Add(ctrl);
            else
                FindAllControls(ctrl, ctrlList);
        }
    }

Under http://www.csharp411.com/change-font-size/ you will find a link how to 7 change the font size of a control, especially 6 when you want to use fixed pixel sized. I 5 think with these tools you can wire it together 4 on your own.

EDIT: Don't forget to say: if 3 you are finally going not to ignore the 2 OS font size and to use AutoScaleMode as 1 intended: this has some quirks, see my previous post here on SO.

Score: 1
  • In Windows 8.0, even if you set the Font Unit as Pixel, with the AutoScaling set to None, it is auto scaling.
  • In Windows 7, the same application is not auto scaling.

So therefore, the only way to not to let 7 your text auto scale on Windows 7 is to 6 set the Font Unit as Pixel and the Window's 5 AutoScaling property as None.

And in Windows 4 8.0, I guess you have no other choice but 3 to set the dpi to the dpi of the developer 2 machine and ask the user to never change 1 it!

More Related questions