我有一个使用 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



来源: C# 2008 中的 Pro WPF 作者:Matthew MacDonald

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top