得到的WinForms问题与DPI正确缩放 [英] Problems getting WinForms to scale correctly with DPI

查看:2185
本文介绍了得到的WinForms问题与DPI正确缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,让WinForms应用程序在高DPI设置下正确显示。我检查了各种网站,并且WinForms都有正确的AutoScaleMode。我已经尝试将此设置为DPI和字体。但是,当使用高DPI设置(例如125%)时,窗体总是在底部附近切断。



我添加了一些代码来检查,如果我将AutoScaleMode设置为DPI,当表单加载时,我看到AutoScaleDimensions是120,120时,窗体加载,CurrentAutoScaleDimensions也是120,120。在Form.designer.cs文件中,有一行将AutoScaleDimension设置为96,96。



如果我将AutoScaleMode设置为Font,那么我可以在设计器中看到AutoScaleDimension已正确设置为新的System.Drawing.SizeF(6F,13F),但是当窗体加载时,AutoScaleDimension和CurrentAutoScaleDimension都设置为8F,16F。



应用程序将一些WPF与WinForms混合,并且WPF屏幕首先显示。因此,基于 .Net中的DPI缩放3.5在混合WinForms和WPF应用程序我试图设置的应用程序的TextFormattingMode,并为首先显示的WPF屏幕,显示,但它没有什么区别。



我坦率地说,这是什么造成的。我想我可以添加代码来通过检测DPI在运行时编程方式调整大小的东西,但我不应该这样做。 AutoScaleMode(和相关)属性应该使这相当自动。那么我还需要检查什么可能会导致这个问题?

解决方案

我几天前有一个类似的问题。经过几个小时的研究,我终于找到了一个非常简单的解决方案 - 添加< dpiAware> 到应用程序清单。以下是 Microsoft网站的示例

 < assembly xmlns =urn:schemas-microsoft-com:asm.v1manifestVersion = 1.0xmlns:asmv3 =urn:schemas-microsoft-com:asm.v3> 
< asmv3:application>
< asmv3:windowsSettings xmlns =http://schemas.microsoft.com/SMI/2005/WindowsSettings>
< dpiAware> True< / dpiAware>
< / asmv3:windowsSettings>
< / asmv3:application>
< / assembly>

对于我的case,我需要设置< dpiAware> code>到Per-monitor,使其正常工作。也就是说,将中间的行更改为< dpiAware> Per-monitor< / dpiAware>



下面列出了每个值之间的差异(这些是来自MSDN):




  • False - 将应用程序设置为不支持DPI。 / li>
  • True - 将应用程序设置为可识别系统DPI。

  • 每个监视器 - 在Windows 8.1上,将应用程序设置为每个监视器DPI知道的。在Windows Vista到Windows 8上,将应用程序设置为不能识别DPI。

  • True / PM - 在Windows 8.1上,将应用程序设置为每个监视器DPI感知。在Windows Vista到Windows 8上,将应用程序设置为可识别系统DPI。


I'm running into problems with getting a WinForms app to display correctly at high DPI settings. I've checked various web sites, and the WinForms all have the correct AutoScaleMode. I've tried setting this to both DPI and Font. However, the forms always get cut off near the bottom when using high DPI settings (e.g. 125%).

I added some code to check, and if I set AutoScaleMode to DPI, when the form loads, I see that AutoScaleDimensions is 120,120 when the form loads, and CurrentAutoScaleDimensions is also 120,120. In the Form.designer.cs file, there is a line to set AutoScaleDimension to 96,96.

If I set AutoScaleMode to Font, then I can see in designer that AutoScaleDimension is correctly set to new System.Drawing.SizeF(6F, 13F), but when the form loads, both AutoScaleDimension and CurrentAutoScaleDimension are set to 8F,16F.

This app mixes some WPF with WinForms, and the WPF screens appear first. So based on DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application I tried setting the TextFormattingMode for the application, and for the WPF screens that show first, to "Display", but it makes no difference.

I am, frankly, at a loss as to what's causing this. I suppose I could add code to resize things programmatically by detecting the DPI at runtime, but I shouldn't have to do that. The AutoScaleMode (and related) properties are supposed to make this fairly automatic. So what else should I be checking for that might cause this problem?

解决方案

I had a similar problem just a few days ago. After a couple of hours research, I finally found a very simple solution -- adding <dpiAware> to the application manifest. Here is an example from Microsoft's website.

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>True</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
</assembly>

For my case, I need to set the <dpiAware> to Per-monitor to make it work normally. That is, change the line in the middle to <dpiAware>Per-monitor</dpiAware>.

The differences between each value are listed below(These are from MSDN):

  • False -- Sets the application to not DPI-aware.
  • True -- Sets the application to system DPI–aware.
  • Per-monitor -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to not DPI–aware.
  • True/PM -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to system-DPI aware.

这篇关于得到的WinForms问题与DPI正确缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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