Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent WPF from scaling with the Windows font size options?

Tags:

wpf

highdpi

I don't want my WPF GUI to scale with Windows font size options (DPI). It's not just a matter of specifying a fixed font size on the UserControl because scaling affects images and borders in the UserControl. Is there a property that I can set on the UserControl to turn off scalling? If not, how can I do that?

like image 677
Sylvain Avatar asked May 02 '11 12:05

Sylvain


3 Answers

You could scale down your UserControl based on the current DPI setting. For example, if you wrapped your UserControl with the following DpiDecorator, then it should look the same regardless of the DPI setting:

public class DpiDecorator : Decorator {

    public DpiDecorator() {
        this.Loaded += (s, e) => {
            Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
            ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
            if (dpiTransform.CanFreeze)
                dpiTransform.Freeze();
            this.LayoutTransform = dpiTransform;
        };
    }

}

Or you could move that logic to your UserControl.

The code to obtain the DPI scale factor was from this blog post.

like image 147
CodeNaked Avatar answered Oct 13 '22 10:10

CodeNaked


Windows includes a compatibility helper for buggy applications that fail under high-dpi settings:

Be sure to leave off (or turn off) "Use Windows XP style DPI scaling":

enter image description here

And be sure your application does not have a "dpiAware" entry in its assembly manifest.

Windows will lie to your application, tell it that it's 96dpi, and then the graphics card will scale the entire window up for you.

Everything will be slightly fuzzy, and generally unpleasant to use, but it will work well enough until you can fix the buggy WinForm control.

Note: The dpiAware manifest entry lets your application tell Windows that it wants to opt-out of the dpi-scaling. Applications only add this item if they've been tested at high-dpi.

like image 40
Ian Boyd Avatar answered Oct 13 '22 11:10

Ian Boyd


in addition the the DpiDecorator, you will also need to fix the font size of your items. For example, in Windows XP, if your setting is set to Large Fonts, Menu Item font size is set to 14 and is also scaled up using the DPI setting, so if you don't fix your MenuItem font size or any other UI item font, you will get Window's default value for this item type. Also remember that a user can manually change the font size/font face for other items...

like image 1
Termit Avatar answered Oct 13 '22 11:10

Termit