WPF监听高对比度改变

方法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}"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值