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

杀马特。学长 韩版系。学妹 提交于 2019-12-21 08:09:19

问题


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?


回答1:


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...




回答2:


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.




回答3:


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":

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.



来源:https://stackoverflow.com/questions/5857098/how-to-prevent-wpf-from-scaling-with-the-windows-font-size-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!