0
SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;

and:

void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
    MessageBox.Show("a");
}

Then go to log in as a different user. You don't even need to log in, just to get to the point that you see the other's username as the title. Then go back to your account - The event handler will be called.

Why? No display settings have been changed.

ispiro
  • 26,556
  • 38
  • 136
  • 291

1 Answers1

1

You say "No display settings have been changed." but you are wrong.

The OS display settings have not changed, but with Fast User Switching enabled, when you switch away from a login session, it is placed in a "disconnected" state just like if you disconnect from Remote Desktop.

When you login again, the login session (and all the windows inside) are once again connected to a display. "Disconnected" -> "Display connected" is a rather large change of display format.

Even though the OS doesn't experience a display settings change, the login session and all windows inside do, and that's why the event is triggered.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks. Is there some way my app can discover that, so that It'll know to ignore a case like this? If not, never mind. But it would be nice (i.e. better user experience). – ispiro Aug 02 '21 at 20:29
  • @ispiro: I think you can do so with e.g. `WTSWaitSystemEvent()`, which will tell you when the Fast User Switching connect and disconnect occur. But what is your app *doing* in response to `DisplaySettingsChanged` that causes a poor user experience? – Ben Voigt Aug 02 '21 at 20:33
  • @ispiro This link might help you understand how to call that function from C# -- https://www.codeproject.com/Articles/133372/Monitoring-of-Logon-Logout-in-Terminal-and-Client – Ben Voigt Aug 02 '21 at 20:35
  • What the app is doing is telling the user they can restart the app to prevent it being blurry since [my question about how fix that](https://stackoverflow.com/q/68378522/939213) didn't find favor in the eyes of StackOverflow, apparently, and I didn't succeed in solving it myself. :( – ispiro Aug 02 '21 at 20:38
  • @ispiro: Maybe you want to save the DPI scaling at startup and again in your event handler, and only popup the notification if it has changed. After all, there are many other things that can cause this event other than DPI scaling changes. – Ben Voigt Aug 02 '21 at 20:39
  • @ispiro: Also, check if you see a difference in behavior when marking your app High-DPI aware in its manifest. – Ben Voigt Aug 02 '21 at 20:41
  • :) That would be nice....except that's where we hit my [other question](https://stackoverflow.com/q/68379924/939213) (currently with a bounty I put on it) where I found out that there is no (simple) way of getting the update dpi. – ispiro Aug 02 '21 at 20:42
  • As far as I know WPF apps are automatically dpi aware. There's nothing to add in the manifest. Is that not true? – ispiro Aug 02 '21 at 20:43
  • @ispiro: I thought your problem was that WPF didn't apply the new DPI... to me that doesn't indicate that you can't read the new value. Are you unable to read the new DPI scaling setting? – Ben Voigt Aug 02 '21 at 20:44
  • 1
    There are multiple levels of DPI aware, [you aren't getting them all by default](https://stackoverflow.com/questions/39217355/wpf-application-blurry-on-high-dpi-screen-on-windows-10#comment85923370_39218709) – Ben Voigt Aug 02 '21 at 20:45
  • You were in fact correct that the first problem was different. It's just that there are multiple problems here. a) How do I get the _updated_ dpi. b) How do I fix the blurriness. The dpi returned is always the one which was true when the app was launched. I found no way to get the updated one. In fact, I assume the reason WPF is blurry is _because_ of this bug. – ispiro Aug 02 '21 at 20:46
  • Yes there are multiple levels of dpi awareness, however I'm testing this on one monitor only. – ispiro Aug 02 '21 at 20:47
  • @ispiro Have you tried toggling `DoNotScaleForDpiChanges` ? – Ben Voigt Aug 02 '21 at 20:50
  • I did not. How do I do that? – ispiro Aug 02 '21 at 20:53
  • Thank you for all your help! (I need to log off at the moment.) – ispiro Aug 02 '21 at 20:57
  • Your comment above about multiple dpi awareness [solved the problem](https://stackoverflow.com/a/68631814/939213). Thanks! – ispiro Aug 03 '21 at 07:38