Sunday, 12 July 2015

(WPF) Check(events) if the user has switched to High contrast mode in C#

When users use high contrast modes on Windows, hardcoded colors in Windows Presentation Foundation applications result in an inconsistent(often ugly and not very usable) look. Changing colors or themes dynamically when the switch is made is sometimes required(although rarely, if you design your styles well).

For those like me who prefer doing this in C# rather than XAML, the SystemParameters.HighContrast property provides a boolean value that is True or False depending on whether one of the high-contrast modes is enabled. This property can be checked by an event handler that is registered to the SystemParameters.StaticPropertyChanged event.

One caveat is that while the SystemParameters.HighContrast property is available on .NET versions 3 and up, the StaticPropertyChanged .event is only available from version 4.5.
For applications that support .NET versions below 4.5, simply subscribe to the SystemEvents.UserPreferenceChanged event instead. For example usage, see my previous post (lines 47 and 63).

No comments:

Post a Comment