Search code examples
.netwpfwindows-vistaaero

WPF Aero Glass DPI Setting Mismatch


I have a WPF application using Aero Glass. When using the application under a 120dpi setting the margins within my UI are not matching up to the margins I pass over to the DwmExtendFrameIntoClientArea API call.

How would I get the systems DPI setting within .NET 3.0 so that I can correct the margin that I am passing to the DwmExtendFrameIntoClientArea API call?

Essentially, the WPF UI uses device-independent units whereas the DwmExtendFrameIntoClientArea API call uses pixels.

Thanks


Solution

  • Okay, something like the following will fix the issue:

    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
    



    Source: Pro WPF in C# 2008 By Matthew MacDonald