创建 DPI 感知应用程序 [英] Creating a DPI-Aware Application

查看:26
本文介绍了创建 DPI 感知应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 形式的应用程序.当我更改显示器的 DPI 时,所有控件都会移动.我使用了代码this.AutoScaleMode = AutoScaleMode.Dpi,但它并没有避免这个问题.

I have a form application in C#. When I change the monitor's DPI, all the controls move. I used the code this.AutoScaleMode = AutoScaleMode.Dpi, but it didn't avoid the problem.

有人有想法吗?

推荐答案

从 .NET 4.7 开始,Windows 窗体改进了对高 DPI 的支持.在文档上阅读更多相关信息.microsoft.com 它仅适用于 Win 10 Creators Update 及更高版本,因此根据您的用户群,使用它可能尚不可行.

As of .NET 4.7, windows forms has improved support for High DPI. Read more about it on docs.microsoft.com It only works for Win 10 Creators Update and higher though, so it might not be feasible to use this yet depending on your user base.

困难,但并非不可能.您最好的选择当然是改用 WPF,但这可能不可行.

Difficult, but not impossible. Your best option is to move to WPF of course, but that might not be feasible.

我在这个问题上花了很多时间.以下是一些规则/指南,可在没有 FlowLayoutPanel 或 TableLayoutPanel 的情况下使其正常工作:

I've spent A LOT of time with this problem. Here are some rules/guidelines to make it work correctly without a FlowLayoutPanel or TableLayoutPanel:

  • 始终以默认的 96 DPI (100%) 编辑/设计您的应用.如果您以 120DPI (125% f.ex) 进行设计,那么当您稍后回到 96 DPI 使用它时会变得非常糟糕.
  • 我已经成功地使用了 AutoScaleMode.Font,我还没有尝试过 AutoScaleMode.DPI.
  • 确保在所有容器(表单、面板、标签页、用户控件等)上使用默认字体大小.8,25 像素.最好不要在 .Designer.cs 文件中为所有容器设置它,以便它使用容器类中的默认字体.
  • 所有容器必须使用相同的AutoScaleMode
  • 确保所有容器都在 Designer.cs 文件中设置了以下行:

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);//96 DPI 设计

  • 如果您需要在标签/文本框等上设置不同的字体大小,请按控件设置它们,而不是在容器类上设置字体,因为 winforms 使用容器字体设置来缩放其内容,并具有 f.ex 面板与包含表单的字体大小不同肯定会产生问题.如果表单和表单上的所有容器使用相同的字体大小,它可能会起作用,但我还没有尝试过.
  • 使用另一台机器或具有更高 DPI 设置的虚拟 Windows 安装(VMware、Virtual PC、VirtualBox)来立即测试您的设计.只需从 DEV 机器上的/bin/Debug 文件夹运行已编译的 .exe 文件.

我保证,如果您遵循这些指南,您会没事的,即使您放置了带有特定锚点的控件并且不使用流面板.我们有一个以这种方式构建的应用程序部署在数百台具有不同 DPI 设置的机器上,我们不再有任何抱怨.所有表单/容器/网格/按钮/文本字段等大小都按字体正确缩放.图像也能正常工作,但在高 DPI 下它们往往会有点像素化.

I guarantee that if you follow these guidelines you will be ok, even when you have placed controls with specific anchors and don't use a flowpanel. We have an app built this way deployed on hundreds of machines with different DPI setups and we no longer have any complaints. All forms/containers/grids/buttons/textfield etc sizes are scaled correctly as is the font. Images work too, but they tend to get a little pixellated at high DPI.

这个链接有很多很好的信息,特别是如果你选择使用 AutoScaleMode.DPI:相关stackoverflow问题的链接

This link has a lot of good info, especially if you choose to use AutoScaleMode.DPI: link to related stackoverflow question

这篇关于创建 DPI 感知应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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