在wpf中将背景变为灰度

时间:2010-03-10 04:33:07

标签: wpf colors panel opacitymask

考虑一个窗口,上面装有多个彩色控件。当一些触发器发生在窗体中时,我想把一个面板放在这个上面,这样所有的控件都会失去它的颜色(一切都以灰度显示),除了弹出的面板。有人能帮帮我吗?

2 个答案:

答案 0 :(得分:4)

我会使用您希望灰度级的客户区域的Effect属性。但是,您需要创建自己的像素着色器才能进行灰度转换。

http://windowsclient.net/wpf/wpf35/wpf-35sp1-more-effects.aspx

您可以使用BlurEffect类而不是自定义着色器快速测试您的概念。

<Window x:Class="WpfGrayscaleSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="327" Width="526">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="239*" />
        <RowDefinition Height="50*" />
    </Grid.RowDefinitions>

    <Canvas Name="clientArea" Background="Transparent" Grid.Row="0">
        <!-- Just some controls -->
        <Button Height="31" Name="button1" Width="80" Canvas.Left="30" Canvas.Top="125">Button</Button>
        <Button Height="28" Name="button2" VerticalAlignment="Bottom" Click="button2_Click" HorizontalAlignment="Right" Width="75" Margin="0,0,16,34" Canvas.Left="66" Canvas.Top="54">Button</Button>
        <Rectangle Margin="86,43,0,0" Name="rectangle1" Stroke="Black" Fill="Crimson" Height="59" HorizontalAlignment="Left" VerticalAlignment="Top" Width="109" Canvas.Left="145" Canvas.Top="44" />
    </Canvas>

    <!-- Button to activate the shader effect -->
    <Button Height="23" Margin="15,0,0,21" Name="button3" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Grid.Row="1" Click="button3_Click">Button</Button>
</Grid>

button3的事件处理程序只是

 private void button3_Click(object sender, RoutedEventArgs e)
 {
     clientArea.Effect = new BlurEffect() { Radius = 10.0 };
 }

当然,为灰度缩放连接自定义着色器会有一些工作,但像素着色器的额外好处将是性能。

答案 1 :(得分:1)

在顶级容器(网格等)中,只需在较低的ZIndex中创建一个Rectangle(或者再创建一个嵌套级别)。

当你弹出面板时,交换矩形的ZIndex以适合你的控件和面板。

就灰度而言,使用VisualBrush可能有一些很好的方法,但我认为你可以在Rectangle上使用半透明的SolidColorBrush。但/ / p>