简体   繁体   English

当用户选择125%或150%的OS字体大小时,如何强制Windows窗体C#应用程序忽略?

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

I need a quick way of forcing my C# Windows Forms application to not scale fonts when a user choose a larger or smaller percentage in the OS settings. 当用户在操作系统设置中选择更大或更小的百分比时,我需要一种快速强制我的C#Windows窗体应用程序不缩放字体的方法。

Is this even possible? 这甚至可能吗?

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 lines are copied from my BaseForm.Designer.cs file, but basically I found two easy steps to get "no font scaling": 以上两行是从我的BaseForm.Designer.cs文件中复制的,但基本上我找到了两个简单的步骤来获得“无字体缩放”:

  1. Set AutoScaleMode to None. 将AutoScaleMode设置为None。

  2. Use "Pixel" as the Unit Type for all Fonts, instead of the default Point value. 使用“Pixel”作为所有字体的单位类型,而不是默认的Point值。

As far as if you should let Windows scale your fonts or not, that's up to you. 至于你是否应该让Windows扩展你的字体,这取决于你。 I for one don't like the design, so if I feel my application needs to be scaled, I'll do it myself, with my own options and design. 我不喜欢这个设计,所以如果我觉得我的应用程序需要扩展,我会自己做,有我自己的选择和设计。

Over the years of speaking with actual end-users, I've also found that most of them have no idea about DPI settings, and if they have anything other than the default set, it wasn't because they wanted it that way... and they just never noticed because all they use is the web browser and maybe Excel and Microsoft Word (which use whatever font they set it to). 多年来与实际的最终用户交谈时,我发现大多数人都不知道DPI设置,如果他们有除默认设置以外的任何东西,那不是因为他们想要那样。他们只是从来没有注意到,因为他们使用的只是网络浏览器,可能是Excel和Microsoft Word(使用他们设置的任何字体)。

If my application had respected the system font settings, they wouldn't have liked it as much == less sales, because it would have had this huge ugly font like the system dialogs do (and they don't know how to change it, but they don't care about system dialogs they never use). 如果我的应用程序已经尊重系统字体设置,他们就不会喜欢它= =更少的销售额,因为它会像系统对话框那样拥有这个巨大的丑陋字体(并且他们不知道如何更改它,但他们并不关心他们从不使用的系统对话框。

The problem is that the Font property of a Form or a control specifies the font size in Points. 问题是Form或控件的Font属性指定Points中的字体大小。 That's a measurement that affect the height of the letters when the DPI setting changes. 当DPI设置改变时,这是一个影响字母高度的测量。 One point is 1/72 inches. 一点是1/72英寸。 The default DPI, 96 dots per inch and a font size of 9 points yields a letter that is 9 / 72 x 96 = 12 pixels high. 默认DPI,每英寸96点,字体大小为9磅,产生一个9/72 x 96 = 12像素高的字母。

When the user bumps up the DPI setting to, say, 120 DPI (125%) then the letter becomes 9 / 72 x 120 = 15 pixels high. 当用户将DPI设置提高到120 DPI(125%)时,则字母变为9/72 x 120 = 15像素高。 If you don't let the control get larger then the text won't fit in the control anymore. 如果不让控件变大,则文本将不再适合控件。 Very ugly to look at. 看起来很难看。

The Form.AutoScaleMode property solves this problem. Form.AutoScaleMode属性解决了这个问题。 It checks at which size the form was designed and compares it against the DPI on the machine on which it runs. 它检查表单的设计大小,并将其与运行它的机器上的DPI进行比较。 And resizes and relocates the controls to ensure this kind of clipping won't happen. 并调整控件的大小和重新定位,以确保不会发生这种裁剪。 Very useful, it is completely automatic without you having to do anything about it. 非常有用,它是完全自动的,你无需做任何事情。

The typical problem is the "relocates" bit in the previous paragraph. 典型的问题是前一段中的“重定位”位。 If you give controls their own font size instead of inheriting the size of the form or if the automatic layout of the form isn't kosher then controls may end up in the wrong spot, destroying the organized look of the form. 如果您为控件提供自己的字体大小而不是继承表单的大小,或者如果表单的自动布局不是犹太教,那么控件可能会在错误的位置结束,从而破坏表单的有组织的外观。

You need to fix that, it isn't clear from your question what the source of the problem might be. 您需要解决这个问题,从您的问题中不清楚问题的根源是什么。 Trying to prevent this auto-scaling from doing its job is not sustainable. 试图阻止这种自动扩展工作是不可持续的。 You'll have to iterate all of the controls in the form and change their Font, picking a smaller font size. 您必须迭代表单中的所有控件并更改其字体,选择较小的字体大小。 This is however going to get you into trouble a couple of years from now, if not already. 然而,如果不是这样,这将在几年后让你陷入困境。 Your user is going to complain about having to work with a postage stamp. 您的用户将抱怨必须使用邮票。

The easiest way to debug the layout problem, avoiding the pain of constantly changing the DPI size, is to temporarily paste this code into your form class: 调试布局问题的最简单方法是避免不断更改DPI大小的痛苦,暂时将此代码粘贴到表单类中:

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

I found a pretty easy workaround. 我找到了一个非常简单的解决方法。

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

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

This will make the text the same size regardless of the autoscale size. 无论自动缩放大小如何,这都将使文本大小相同。

No. GDI and Windows Forms are resolution dependent. 不可以.GDI和Windows Forms依赖于分辨率。 The best option is to design your layouts in a way that they scale appropriately when a window is resized. 最佳选择是以调整窗口大小时适当缩放的方式设计布局。 This tends to allow font size scaling to work correctly, as well, as the layouts will adjust as needed. 这往往允许字体大小缩放正常工作,因为布局将根据需要进行调整。

This, by the way, is one of the big improvements in WPF - it's designed to be resolution independent. 顺便说一句,这是WPF的重大改进之一 - 它的设计独立于分辨率。

I suspect you already tried 我怀疑你已经尝试过了

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

in your forms, which prevents your forms from scaling, but not your fonts. 在您的表单中,这可以防止您的表单缩放,但不能防止您的字体。 So what you have to do (even if it is not recommended, see the other posts) is setting the font size for all controls in your form to a fixed size: 因此,您需要做的事情(即使不推荐,请参阅其他帖子)将表单中所有控件的字体大小设置为固定大小:

Here is a code snippet to grab all controls of 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 change the font size of a control, especially when you want to use fixed pixel sized. http://www.csharp411.com/change-font-size/下,您将找到如何更改控件字体大小的链接,尤其是当您要使用固定像素大小时。 I think with these tools you can wire it together on your own. 我认为使用这些工具,您可以自己连接它们。

EDIT: Don't forget to say: if you are finally going not to ignore the OS font size and to use AutoScaleMode as intended: this has some quirks, see my previous post here on SO . 编辑:不要忘记说:如果你最终不会忽略操作系统字体大小并按预期使用AutoScaleMode:这有一些怪癖,请参阅我之前在SO上的帖子

  • In Windows 8.0, even if you set the Font Unit as Pixel, with the AutoScaling set to None, it is auto scaling . 在Windows 8.0中,即使将字体单位设置为像素,AutoScaling设置为None, 也会自动缩放
  • In Windows 7, the same application is not auto scaling. 在Windows 7中,相同的应用程序不会自动缩放。

So therefore, the only way to not to let your text auto scale on Windows 7 is to set the Font Unit as Pixel and the Window's AutoScaling property as None. 因此,在Windows 7上不让文本自动缩放的唯一方法是将字体单位设置为Pixel,将Window的AutoScaling属性设置为None。

And in Windows 8.0, I guess you have no other choice but to set the dpi to the dpi of the developer machine and ask the user to never change it! 在Windows 8.0中,我猜你别无选择,只能将dpi设置为开发者机器的dpi,并要求用户永远不要更改它!

Going against the user wishes like this is not something MS eagerly accommodates. 违背用户的意愿,这不是MS急切适应的。

Rather than fixing the symptom (your app not scaling correctly), would it not be probably just as quick to fix the problem? 而不是修复症状(您的应用程序没有正确缩放),它可能不是很快解决问题? When a user selects larger fonts, it's usually because they need the larger letters to be able to read them; 当用户选择较大的字体时,通常是因为它们需要较大的字母才能读取它们; so scaling correctly is more important than not scaling and remaining illegible to the user. 所以正确缩放比不缩放和保持用户难以辨认更重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Forms C# 应用程序 - 当用户为操作系统字体大小选择 125% 或 150% 时,设置表单控件的字体大小以适应屏幕? - Windows Forms C# application - Set font size of form controls to fit the screen when a user choose 125% or 150% for the OS font size? 检测 Windows 字体大小(100%、125% 和 150%) - Detect Windows font size (100%, 125%, and 150%) 如何在 C# Windows 窗体应用程序中修复窗体大小而不让用户更改其大小? - How can I fix the form size in a C# Windows Forms application and not to let user change its size? 在 C# Windows Forms 中更改控制状态时,如何忽略触发的简单事件? - How do I ignore simple events firing when changing control states in C# Windows Forms? C#和Windows Forms应用程序表单大小 - C# and Windows Forms application form size 如何在C#Windows应用程序中强制用户响应消息框 - how to force user to respond to message box in c# windows application 使用 Windows Forms 应用程序时,我是否需要“主文件”在 c# 上编码 - Do I need the "main file" to code on c# when working with Windows Forms Application 我如何选择一个应用程序以在 c# 中使用过程控制启动? - How do I choose an application to start in c# with the Process control? 如何实现Windows表单应用程序C#的样式? - How do you implement a Styling for Windows forms Application C#? 如何获得命令控制台在Windows Forms应用程序中写行? (Visual C#) - How Do I Get The Command Console To Write Lines In Windows Forms Application? (Visual c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM