question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Getting the display information on Win32

See original GitHub issue

I am trying to convert an existing UWP project to WinUI/Win32.

The project uses DisplayInformation.GetForCurrentView() in one of the of the controls to get DPI information, which then results in the following error:

Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.

CoreWindow is a UWP thing, so I’m assuming this function is not supported on Win32. What would be the correct way to get the current DPI (and detect changes to the DPI) from a WinUI/Win32 app?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rick-palmsenscommented, Apr 6, 2021

@marb2000 Sorry for the late response, but using the Loaded/Unloaded events is indeed a good way ensure the control is attached to a XamlRoot.

1reaction
thomasclaudiushubercommented, Feb 16, 2021

Hi @rick-palmsens ,

there is currently no API exposed to get DPI information. But you can go via Win32 PInvoke right now.

Steps to try it:

  1. Create a new WinUI in Desktop project
  2. Add a reference to the NuGet package PInvoke.Win32 to your WinUI desktop project.
  3. Implement the MainWindow.xaml.cs file like this:
public sealed partial class MainWindow : Window
{
  public MainWindow()
  {
    this.InitializeComponent();
  }

  private void myButton_Click(object sender, RoutedEventArgs e)
  {
    var dpiScalingFactor = GetDpiScalingFactor();
    var dpiPercent = $"{dpiScalingFactor * 100}%";
    myButton.Content = $"DPI Setting: {dpiPercent}";
  }

  private double GetDpiScalingFactor()
  {
    var windowNative = this.As<IWindowNative>();
    var hwnd = windowNative.WindowHandle;

    var dpi = PInvoke.User32.GetDpiForWindow(hwnd);
    return (float)dpi / 96;
  }
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
  IntPtr WindowHandle { get; }
}
  1. The generic As<T> method that I call on the Window instance is an extension method. You need to add the following using directive to get that extension method:
using WinRT;

To make it complete, these are all using directives that I have in the MainWindow.xaml.cs file:

using Microsoft.UI.Xaml;
using System;
using System.Runtime.InteropServices;
using WinRT;

That’s it.

Take also a look at the WinUI 3 Demos repository from @marb2000 that contains more Window interop scenarios. There is also a center window branch that shows you how to center a WinUI desktop window: https://github.com/microsoft/WinUI-3-Demos/tree/CenterWindow/src/Build2020Demo

Read more comments on GitHub >

github_iconTop Results From Across the Web

retrieve multiple display information using win32/C++
Use the EnumDisplayMonitors() function, passing NULL for the first 2 arguments. Your callback gets the monitors in numeric order with their ...
Read more >
Display Devices Reference - Win32 apps
The DISPLAYCONFIG_MODE_INFO_TYPE enumeration specifies that the information that is contained within the DISPLAYCONFIG_MODE_INFO structure is ...
Read more >
Solved Write a computer program as a Win32 console
You program shall receive information from the user as the user types in two numbers to be processed. Your program shall NOT print...
Read more >
How do Win32 executables display incompatibility ...
Short answer: every EXE file begins with a DOS executable. Long answer: The MS-DOS Header. Every PE file begins with a small MS-DOS® ......
Read more >
Visual Studio Code Frequently Asked Questions
Yes, VS Code is free for private or commercial use. See the product license for details. How to disable telemetry reporting. VS Code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found