How to get DPI with WinUI 3?

Hong 1,211 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 App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
783 questions
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    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.