C#缩放用户控件的内容相匹配的用户DPI /字体大小用户、缩放、字体大小、控件

2023-09-02 10:31:15 作者:﹀我旳高傲是我旳低微。

我如何得到我的OwnerDrawn用户控件,以尊重用户的DPI(96/120 / XXX)和/或字体大小(正常,大,特大)?

How do I get my OwnerDrawn UserControl to respect the users dpi (96/120/xxx) and/or font-size (normal, large, extra large)?

有人建议到图形对象上使用DpiX和DpiY性能,但好像没有任何事情在我的控制(即他们始终设置为96,无论哪个字体大小或DPI的我选择)

Some people suggest to use the DpiX and DpiY properties on a Graphics object, but that doesn't seem to to anything in my control (i.e. they are always set to 96, regardless of which font-size or dpi I choose).

还有这里是计算器它建议使用自动定标性质的另一个类似的问题,但建议的解决方案并不真正无论做任何事情。

There is another similar question here on StackOverflow where it suggests to use the AutoScale properties, but the suggested solutions don't really do anything either.

请问有没有办法,除了为依托WPF在.NET这样做的?

Is there no way of doing this in .NET except for relying on WPF?

推荐答案

您需要的用户控件的AutoScaleMode属性设置为AutoScaleMode.Dpi,和不可以设置自动调整属性设置为true。如果你这样做,它会重置AutoScaleMode回无。自动缩放特性已废弃,只存在向后兼容性(请参阅重要提示的在此的 MSDN文章)。

You would need to set the AutoScaleMode property of the UserControl to AutoScaleMode.Dpi, and not set the AutoScale property to true. If you do, it will reset the AutoScaleMode back to None. The AutoScale property is obsolete and is there only for backwards compatibility (see the Important Note in this MSDN article).

此外,在Windows Vista / 7,除非你明确指定你的应用程序是DPI感知,Windows将模拟默认新闻部环境,以使您的应用程序与96 DPI渲染,然后缩放生成位图到合适的大小。为了避免这种情况,你可以改变你的应用程序清单,以通知Windows您实际上是新闻部知道 - 看的使用清单申报新闻部认识的中的这篇文章。

Also, in Windows Vista/7, unless you explicitly specify that your application is DPI-aware, Windows will emulate a default DPI environment so that your application renders with 96 DPI, then scale the resulting bitmap to the appropriate size. To avoid that, you can alter your application manifest to inform Windows that you are in fact DPI aware - see the Using manifest to declare DPI awareness section in this article.