使用 WinForms 设计器时,我是否总是需要将 DPI 设置为 96? [英] When using the WinForms designer, do I always need to have DPI set to 96?

查看:24
本文介绍了使用 WinForms 设计器时,我是否总是需要将 DPI 设置为 96?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我当前的显示器,我更喜欢每英寸 120 像素的 DPI 设置(Windows 建议为默认设置).但是,在设计表单后,它在不使用每英寸 120 像素的系统上经常会出现错误布局.

With my current monitor I prefer a DPI setting of 120 pixels per inch (which windows suggests as the default). However, after designing a form, it often lays out incorrectly on systems that don't use 120 pixels per inch.

我想知道,是否有必要在每次使用设计器时将显示设置设置为每英寸 96 像素?

I'm wondering, is it necessary that I should set my display settings to 96 pixels per inch for whenever I use the designer?

此外,当其他开发人员具有不同的 DPI 时,也会出现一些问题.他们在设计器中打开一个表单并移动诸如文本编辑控件之类的东西,突然发现它也会自动调整自身大小.然后,有一个控件与其他控件的大小不同,我们陷入了混乱.

Also, there are some problems when other developers have different DPIs. They open a form in the designer and move something like a text edit control, and suddenly find that it automatically resizes itself too. Then, there's one control that's a different size to the others and we're in a mess.

附言我看过相关的帖子.他们都很有趣,但没有回答我的问题.

P.S. I've read related posts. They're all interesting, but didn't answer my question.

如何在.NET中控制字体DPIWinForms 应用

C# WinForms 禁用 DPI 缩放

WinForms 不同的 DPI 布局

DPI 未正确缩放

Visual Studio 和 DPI 问题

推荐答案

没有.使用 WinForms 设计器时,您不需要始终将 DPI 设置为 96.

No. You don't need to always have the DPI set to 96 when using the WinForms designer.

如果您将 AutoScaleMode 属性设置为 Dpi,则设计器会将当前系统 DPI 写入表单的 AutoScaleDimensions 属性中的 Designer.cs 文件.当在具有不同 DPI 的系统上使用设计器时,此信息将用于重新缩放表单,并且设计器可以在不同的 DPI 下使用.

If you set the AutoScaleMode property to Dpi then the designer will write the current system DPI into the designer.cs file in the AutoScaleDimensions property for the form. When the designer is used on a system with a different DPI, this information will be used to rescale the form and the designer can be used at a different DPI.

当我尝试其他缩放模式时,这似乎效果不佳.无"意味着控件不会在运行时缩放,字体"似乎会出现舍入错误,并且当显示设置 DPI 更改时,控件大小可能会略有变化,从而导致错误.

When I tried other scaling modes, this didn't seem to work well. 'None' meant that controls wouldn't scale at runtime, 'Font' seemed to suffer from rounding errors and when the display settings DPI changed, the control sizes could change slightly causing errors.

我还发现,对于添加到表单中的 UserControl,最好将其 AutoScaleMode 设置为 Inherit.如果您使用 Dpi,则其上的控件会重新缩放两次,最终会导致布局不正确.

I also found that for UserControls that are added to forms it is best to set their AutoScaleMode to Inherit. If you use Dpi, then the controls on it get re-scaled twice and will end up being laid out incorrectly.

经过几个小时的实验和互联网搜索,我找到了以下两篇文章后,我想出了上述指南:

I came up with the guidelines above after a few hours of experimentation and internet searching where I found the following two articles:

Windows 窗体中的自动缩放

和:

UserControl 上的子控件可能会在具有较低字体 Dpi 的系统中被剪裁

这篇关于使用 WinForms 设计器时,我是否总是需要将 DPI 设置为 96?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆