我可以为 DPI 自动缩放表单而不会变得模糊吗? [英] Can I automatically scale a form for DPI without it getting blurry?

查看:39
本文介绍了我可以为 DPI 自动缩放表单而不会变得模糊吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发一个 WinForms 应用程序.它使用面板来绘制 Mandelbrot 分形的图像.在该程序的手册中以及每当我在某处发布它时,我建议人们将其缩放设置设置为 100%,否则图像看起来会不好看.这是因为在其他设置下,图像在绘制后被放大,变得模糊.所有其他控件也很模糊.

I am developing a WinForms application in C#. It uses a panel to draw an image of the Mandelbrot fractal. In the manual for the program and whenever I post about it somewhere, I recommend people to set their scaling setting to 100%, as otherwise the images won't look nice. This is because on other settings, the image is scaled up after drawing it, and it becomes blurry. All other controls are blurry too.

例如:我的面板是 500x500.我的 Windows 中的缩放比例设置为 125%.当我运行程序时,面板内部仍然是 500x500,但它显示为 625x625,模糊.
相反,当程序运行时,我希望面板在内部调整为 625x625,并显示为 625x625.

For example: my panel is 500x500. The scaling in my Windows is set to 125%. When I run the program, the panel is internally still 500x500, but it appears as 625x625, blurry.
Instead, when the program is run, I want the panel to internally resize to 625x625, and appear as 625x625 too.

我找到了以下解决方案:我发现了 SetProcessDPIAware()(来自 此处).使窗口不缩放的设置(看起来好像是 100% 缩放设置),但文本可以(并且不会变得模糊).然后,我可以在程序开始时计算适当的乘数 (dpi = DpiX/96) 并将其提供给一个包含诸如

I have found the following solution: I found out about SetProcessDPIAware() (from here). Setting that makes the window not scale (it appears as if it was at the 100% scale setting), but the text does (and without becoming blurry). I can then, at the start of the program, calculate the appropriate multiplier (dpi = DpiX / 96) and give that to a huge method that includes commands like

xentrylabel.Location = new Point((int)(xentrylabel.Location.X * dpi),(int)(xentrylabel.Location.Y * dpi));
xentry.Location = new Point((int)(xentry.Location.X * dpi), (int)(xentry.Location.Y * dpi));
xentry.Size = new Size((int)(xentry.Width * dpi), (int)(xentry.Height * dpi));

可能需要更新的每个控件属性都有一个.在写这个问题的时候,我有了这个想法并开始了它.但是,我意识到这将需要很多行代码,所以我想知道是否没有内置的方法来做到这一点.这似乎是许多人想要的选择,而不是他们的应用程序在高 dpi 的屏幕上变得模糊或难以阅读.

One for every control property that might need to be updated. While writing this question, I got this idea and got started with it. However, I realised that this will need very many lines of code, so I wonder if there isn't a built-in way to do this. It seems like an option that many would like to go for, rather than their applications becoming blurry or hard to read on screens with high dpi.

这种手动更正位置和大小的方式是可行的,还是有一种内置的方法可以通过实际缩放表单中的所有内容而不是缩放最终结果来缩放其他 DPI 设置的表单?

Is this way of manually correcting positions and sizes the way to go, or is there a built-in way to scale the form for other DPI settings by actually scaling everything in the form, instead of scaling the end result?

从一些评论看来,似乎只有 SetProcessDPIAware() 才能放大所有内容.但根据我的经验,事实并非如此.以下是一些截图:100% 比例设置的应用:https://i.stack.imgur.com/pws9B.png

From some comments it seems as if SetProcessDPIAware() alone should scale up everything. But in my experience, it doesn't. Here are some screenshots: Application on 100% scale setting: https://i.stack.imgur.com/pws9B.png

在没有 SetProcessDPIAware() 的 125% 比例设置上的应用:https://i.stack.imgur.com/cE4Wx.png

Application on 125% scale setting without SetProcessDPIAware(): https://i.stack.imgur.com/cE4Wx.png

使用 SetProcessDPIAware() 在 125% 比例设置上的应用:https://i.stack.imgur.com/oVA8W.png

Application on 125% scale setting with SetProcessDPIAware(): https://i.stack.imgur.com/oVA8W.png

推荐答案

我们通过创建托管在 ElementHost 控件中的 WPF 用户控件,解决了在 Chem4Word 中缩放大于 100% 的高 DPI 屏幕的渲染控件问题,设置为填写表单.

We solved the issue of rendering controls for high DPI screens with scaling greater than 100% in Chem4Word by creating a WPF user control which is hosted in an ElementHost control, which is set to fill the form.

WinForm+- 元素主机+- WPFUserControl

WinForm +- ElementHost +- WPFUserControl

看看你上传的图片,这正是使用 WPF 用户控件解决的问题.

Looking at your uploaded images, that's exactly the problem which is solved by using a WPF user control.

这篇关于我可以为 DPI 自动缩放表单而不会变得模糊吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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