方法1(需要donet4.5及以上):
System.Windows.SystemParameters.StaticPropertyChanged += SystemParameters_StaticPropertyChanged;
private void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "HighContrast")
{
if (SystemParameters.HighContrast)
{
//open
}
}
}
方法2:
Microsoft.Win32.SystemEvents.UserPreferenceChanging += SystemEvents_UserPreferenceChanging;
private void SystemEvents_UserPreferenceChanging(object sender, UserPreferenceChangingEventArgs e)
{
// high contrast events
if (e.Category == UserPreferenceCategory.Accessibility)
{
if (SystemParameters.HighContrast)
{
//true
}
}
}
未开启高对比时,颜色自定义的解决方案
可以在监听到高对比度事件发送后,更改资源字典,比如:
private string stylePath1 = @"Pack://application:,,,/View/Themes/BaseColorHighContrastStyles.xaml";
private string stylePath2 = @"Pack://application:,,,/View/Themes/BaseColorStyles.xaml";
if (SystemParameters.HighContrast)
{
if (!stylePath1.Equals(Application.Current.Resources.MergedDictionaries[0].Source.OriginalString))
Application.Current.Resources.MergedDictionaries[0].Source = new Uri(stylePath1);
}
else
{
if (!stylePath2.Equals(Application.Current.Resources.MergedDictionaries[0].Source.OriginalString))
Application.Current.Resources.MergedDictionaries[0].Source = new Uri(stylePath2);
}
}
开启高对比度前(baseColorStyles.xaml中的代码):
<SolidColorBrush x:Key="ForegroudWhiteColor" Color="White"/>
<SolidColorBrush x:Key="BackgroundColor" Color="#18181817"/>
<SolidColorBrush x:Key="ApplicationForeground" Color="{x:Static SystemColors.DesktopColor}"/>
<SolidColorBrush x:Key="PopupBackGroundColor" Color="#333134"/>
<SolidColorBrush x:Key="PopupDescColor" Color="Gray"/>
开启高对比度后(baseColorHihtContrastStyles.xaml中的代码):
<SolidColorBrush x:Key="ForegroudWhiteColor" Color="{x:Static SystemColors.WindowTextColor}"/>
<SolidColorBrush x:Key="BackgroundColor" Color="{x:Static SystemColors.DesktopColor}"/>
<SolidColorBrush x:Key="ApplicationForeground" Color="{x:Static SystemColors.DesktopColor}"/>
<SolidColorBrush x:Key="PopupBackGroundColor" Color="#333134"/>
<SolidColorBrush x:Key="PopupDescColor" Color="{x:Static SystemColors.GrayTextColor}"/>