如何使我的 WinForms 应用程序支持 DPI
我在一家公司工作,该公司的旗舰产品是 Winforms 应用程序。 我们最近重新设计了品牌和用户界面。我们注意到,表单现在无法正确显示文本,并且某些控件未对齐或已从边缘消失。
由于我们没有时间,我是否可以通过最少的重构来使该应用程序上的表单具有 DPI 感知能力。
I'm working in a company that has a Winforms application as our flagship product.
we recently reworked the branding and UI. We have noticed that the Forms now do not display the text properly and some controls are out of alignment or have disappeared off the edge.
Is it possible for me to make the forms on this application DPI-Aware with the least amount of re-factoring as we don't have time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重要的是不要指定像素坐标和大小。这排除了使用
Control.Top
和Control.Left
的可能性,而这正是设计者在“仅将”控件“放置”在窗体上时所做的事情。为了获得适用于不同 DPI 设置的 UI,几乎所有内容都必须动态调整大小。控件应具有
Control.AutoSize< /code>
已启用。但仅仅启用
AutoSize
就会完全搞乱你的布局,因为控制位置仍然是静态的。要动态定位控件,您可以使用容器控件,例如
FlowLayoutPanel
和TableLayoutPanel
(尺寸设置为AutoSize
)。然后,其中的正常控件将根据自动确定的大小在表单周围移动。正如您所看到的,这并不简单,需要一点经验才能正确完成,并且需要大量的测试(具有不同 DPI 设置的虚拟机效果很好)。但我认为绝对应该这样做,因为如果我的笔记本电脑上出现一些看起来愚蠢且有缺陷的东西,我总是会很恼火。
It's essential that you don't specify pixel coordinates and sizes. This rules out using
Control.Top
andControl.Left
, which is what the designer does, when you "just place" the controls on a form.To get an UI that works with different DPI settings, pretty much everything has to be dynamically sized. The controls should have
Control.AutoSize
enabled. But just enablingAutoSize
would totally screw up your layout, since the control position would still be static.To get dynamically position the controls, you can use container controls, like the
FlowLayoutPanel
and theTableLayoutPanel
(with sizes set toAutoSize
). The normal controls inside of those would then just move arround the form, according to automatically determined sizes.As you can see, this isn't simple, requires a bit of experience to get it right and needs a huge amount of testing (virtual machines with different DPI settings work great). But I think it should definitely be done, since I'm always annoyed if something looks stupid and buggy on my laptop.
最快的方法是重写 OnPaint 并使用 Graphics.DpiX 和 DpiY 属性。请参阅 http://msdn.microsoft.com/en- us/library/ms701681(VS.85).aspx 唯一的建议是使用清单文件而不是 SetDpiAware
The quickest way is to override OnPaint and use the Graphics.DpiX and DpiY properties. Refer to http://msdn.microsoft.com/en-us/library/ms701681(VS.85).aspx only recommendation is to use a manifest file instead of SetDpiAware