0

我无法让我的应用程序感知 DPI。在 app.manifest 我未注释:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>

在 App.config 我添加:

<appSettings>
    <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

我正在关注 使 vb.net 应用程序 DPI 感知https://www.telerik.com/blogs/winforms-scaling-at-large-dpi-settings-is-it-even-possible-中的问题和回答

我的应用程序有一个带有单个用户控件的表单。在每次我尝试使用 AutoScaleMode 运行应用程序到各种设置:无、Dpi、字体、继承(它们默认为字体)。我在笔记本电脑上使用的是原厂原装显示器。

在每种情况下,e.graphics.dpix 和 e.graphics.dpiy(其中 e 是 PaintEventArgs)都是 96.0。它应该是 128.0 = 1920 像素 / 15 英寸和 128.0 = 1080 / 8.4375 英寸。

我错过了什么?

4

1 回答 1

0

这是部分解决方案。

要在屏幕上绘画,请设置 Graphics.PageUnit = GraphicsUnit.Point(默认为 GraphicsUnit.Display)。

(我还没有弄清楚如何在不混淆 DPI 的情况下自动调整我正在绘制的 UserControl。)

对于打印,使用 Graphics.PageUnit = GraphicsUnit.Pixel。

' printing
dim gs as Drawing2D.GraphicsState = e.Graphics.Save
Try
    e.Graphics.PageUnit = GraphicsUnit.Pixel
    dim DpiX as Single = e.Graphics.DpiX
    dim DpiY as Single = e.Graphics.DpiY
    DoPrinting(e.Graphics, DpiX, DpiY)  ' this is where you implement the code to draw your page
Catch ex as Exception
Finally
    e.Graphics.Restore(gs)
End Try

' painting to screen
dim gs as Drawing2D.GraphicsState = e.Graphics.Point
Try
    e.Graphics.PageUnit = GraphicsUnit.Pixel
    dim DpiX as Single = 128.0!    ' your value may vary; to find out, divide
    dim DpiY as Single = 128.0!    ' physical size of screen by screen resolution
    DoPrinting(e.Graphics, DpiX, DpiY)  ' this is where you implement the code to paint to screen
Catch ex as Exception
Finally
    e.Graphics.Restore(gs)
End Try
于 2020-10-08T21:42:01.110 回答