WPF Aero Glass DPI 设置不匹配
-
22-07-2019 - |
题
我有一个使用 Aero Glass 的 WPF 应用程序。在 120dpi 设置下使用应用程序时,我的 UI 中的边距与我传递给 DwmExtendFrameIntoClientArea API 调用的边距不匹配。
如何获取 .NET 3.0 中的系统 DPI 设置,以便可以更正传递给 DwmExtendFrameIntoClientArea API 调用的边距?
本质上,WPF UI 使用与设备无关的单位,而 DwmExtendFrameIntoClientArea API 调用使用像素。
谢谢
解决方案
好的,类似下面的内容可以解决这个问题:
Public Shared Function GetDpiAdjustedMargins(ByVal WindowHandle As IntPtr, ByVal Left As Integer, ByVal Right As Integer, ByVal Top As Integer, ByVal Bottom As Integer) As Margins
'
Dim Graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(WindowHandle)
Dim DesktopDPIx As Single = Graphics.DpiX
Dim DesktopDPIy As Single = Graphics.DpiY
Dim Margins As Margins = New Margins
Margins.Left = Left * (DesktopDPIx / 96)
Margins.Right = Right * (DesktopDPIx / 96)
Margins.Top = Top * (DesktopDPIx / 96)
Margins.Bottom = Bottom * (DesktopDPIx / 96)
Return Margins
'
End Function
不隶属于 StackOverflow