【问题标题】:WPF change brightnessWPF改变亮度
【发布时间】:2009-03-17 11:40:04
【问题描述】:

我有一个黑白应用程序,我需要一个降低亮度的功能,我该怎么做?所有的白色都来自保存在 ResourceDictionary(Application.xaml) 中的 SolidColorBrush,我目前的解决方案是放一个空窗口,它的不透明度为 80%,但这不允许我使用底层窗口。

【问题讨论】:

    标签: wpf brightness


    【解决方案1】:

    如果您的所有 UI 元素都使用相同的 Brush,为什么不直接修改 Brush 以降低亮度?例如:

    public void ReduceBrightness()
    {
        var brush = Application.Resources("Brush") as SolidColorBrush;
        var color = brush.Color;
        color.R -= 10;
        color.G -= 10;
        color.B -= 10;
        brush.Color = color;
    }
    

    在您对Brush 被冻结发表评论后进行编辑:

    如果您使用的是内置画笔之一(通过Brushes 类),那么它将被冻结。不要使用其中之一,而是声明自己的 Brush 而不冻结它:

    <SolidColorBrush x:Key="Brush">White</SolidColorBrush>
    

    在 Robert 对应用程序级资源发表评论后进行编辑:

    罗伯特是对的。在Application 级别添加的资源如果可冻结,则会自动冻结。即使您明确要求不要冻结它们:

    <SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/>
    

    我可以看到有两种解决方法:

    1. 按照 Robert 的建议,将资源放在资源树中的较低级别。例如,在 WindowResources 集合中。不过,这使得分享变得更加困难。
    2. 将资源放入不可冻结的包装器中。

    作为 #2 的示例,请考虑以下内容。

    App.xaml

    <Application.Resources>
        <FrameworkElement x:Key="ForegroundBrushContainer">
            <FrameworkElement.Tag>
                <SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/>
            </FrameworkElement.Tag>
        </FrameworkElement>
    </Application.Resources>
    

    Window1.xaml

    <StackPanel>
        <Label Foreground="{Binding Tag, Source={StaticResource ForegroundBrushContainer}}">Here is some text in the foreground color.</Label>
        <Button x:Name="_button">Dim</Button>
    </StackPanel>
    

    Window1.xaml.cs

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            _button.Click += _button_Click;
        }
    
        private void _button_Click(object sender, RoutedEventArgs e)
        {
            var brush = (FindResource("ForegroundBrushContainer") as FrameworkElement).Tag as SolidColorBrush;
            var color = brush.Color;
            color.R -= 10;
            color.G -= 10;
            color.B -= 10;
            brush.Color = color;
        }
    }
    

    它不是那么漂亮,但它是我现在能想到的最好的。

    【讨论】:

    【解决方案2】:

    通过更改我的根元素的不透明度而不是尝试修改画笔解决了这个问题,但如果有人告诉我是否可以这样做或不可能做到这一点,那仍然会很好。

    【讨论】:

      【解决方案3】:

      如果将SolidColorBrush 添加到较低级别的资源中,Kent 的解决方案将起作用。 Freezable 在添加到 Application.Resources 时会自动冻结。

      【讨论】:

      • 低级是什么意思?
      • Kent 在他的第二次编辑中回答了这个问题 - 您可以将其添加为主窗口的资源。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2013-04-13
      相关资源
      最近更新 更多