-1

In WPF applications VisualTreeHelper.GetDpi() gets the dpi when the application is launched. Subsequent calls return the same value even if the user has meanwhile changed the dpi in Settings -> Display.

How can I "reset" the dpi that VisualTreeHelper.GetDpi() sees without restarting the application?

Some code:

Title = VisualTreeHelper.GetDpi(this).DpiScaleX.ToString();
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Does this answer your question? https://stackoverflow.com/a/1918890/6734762 – Foitn Jul 14 '21 at 14:28
  • You can try to look [source](https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/VisualTreeHelper.cs,108) what it does. – Sinatr Jul 14 '21 at 14:36
  • @mm8 Not for a particular screen. And the Visual I feed it is either the Window itself (`this`) or a `Button` on it. Both fail to get updated. – ispiro Jul 14 '21 at 16:31
  • @ispiro: How do I reproduce your issue, step by step from scratch? – mm8 Jul 14 '21 at 16:33
  • @mm8 You create a WPF app, add a button that sets the Title to `VisualTreeHelper.GetDpi(this).DpiScaleX.ToString()`, run app, change dpi in Settings, and click the button. You have enough rep, though, that I assume you knew that. – ispiro Jul 14 '21 at 16:41
  • 1
    Did you try the native `GetDpiForWindow` API? – mm8 Jul 14 '21 at 17:11
  • 1
    Have you set dpiAwareness in app.manifest? – emoacht Jul 17 '21 at 12:23
  • 1
    The dpi landscape has evolved over years and WPF is somehow stuck in the passed. I wrote a bunch of DPI tools here: https://stackoverflow.com/a/32888078/403671 – Simon Mourier Jul 30 '21 at 07:08

1 Answers1

1

Adding per-monitor dpi-awareness fixes blurriness. It also fixes this. Why, I'm not sure, since this problem exists even when a single monitor is used. But it works.

You will need to add an app.manifest file for that.

ispiro
  • 26,556
  • 38
  • 136
  • 291