How to get DPI with WinUI 3?

Hong 1,246 Reputation points
2023-12-04T22:49:49.7933333+00:00

DisplayInformation no longer has GetForCurrentView() that allows retrieving LogicalDpi. How can we obtain LogicalDpi with Win UI?

Windows development | Windows App SDK
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2023-12-05T02:41:37.56+00:00

    Calling Win32 functions is a straightforward way as there is no such information in DisplayInformation class.

            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            public static extern int GetDpiForWindow(IntPtr hwnd);
    
            public f()
            {
                IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
                int dpi = GetDpiForWindow(hWnd);
            }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.