is there an easy way to hook to an event that is triggered on change of global screen resolution?
4 Answers
Handle the following event:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged
You may refer to this page for more details.
You may also wanna see the msdn article on SystemEvents class.
- 33,538
- 22
- 81
- 114
There are two events - SystemEvents.DisplaySettingsChanged and SystemEvents.DisplayedSettingsChanging which you can handle.
Note that both events are static and you will need to detach your handlers before exiting from your program.
- 66,413
- 54
- 311
- 354
- 984
- 9
- 21
-
7"you will need to detach your handlers before exiting from your program": of course not! Once the process stops it doesn't matter if you unsubscribed or not... It's just that objects that are subscribed to the event won't be eligible for GC and will remain in memory – Thomas Levesque May 11 '11 at 08:33
-
You need to detach your handlers before the handler's object will be GCed. If the process exits, doesn't matter. If your app continues running after you've stopped caring about display changes, you should unsubscribe at that point so GC will work properly. – user169771 Apr 07 '16 at 15:16
-
5Documentation says: "Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.". https://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.displaysettingschanged.aspx?f=255&MSPPError=-2147217396 – CLS Dec 08 '16 at 09:00
Sure you don't have to unsubscribe to static events (or any events) if your program (process) is dying. The OS will take care of releasing all memory of your process to the OS. However, if you subscribe to a static event or to any event pointing to an object with a longer lifetime than your object subscribing, and you want that object to be eligible for GC - you need to unsubscribe (-=) to the event.
AND it is always good practice to always unsubscribe to all events. You never know when the lifetime of your objects are changed (by someone else) during the lifespan of your source code / product...
- 41
- 1
try this simple code
using Microsoft.Win32;
SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
MessageBox.Show("Resolution Change.");
}
and don't forget this line using Microsoft.Win32;
- 2,064
- 2
- 17
- 37