Wpf 画刷

画刷类

1、SolidColorBrush 使用单一的连续颜色绘制区域。

2、LinearGradientBrush 使用简便填充绘制区域,渐变的阴影填充从一种颜色变化到另一种颜色。

3、RadialGradientBrush 使用径向简便填充绘制区域,除了是在圆形模式中从中心点向外部辐射渐变之外,这种画刷和线性检变化刷类似。

4、ImageBrush 使用可以被拉伸、缩放或平铺的图像绘制区域。

5、DrawingBrush 使用一个Drawing对象绘制区域。该对象可以包含已经定义的形状和位图。

6、VisualBrush 使用一个Visual对象绘制区域。因为所有WPF元素都继承自Visual类,所以可以使用该画刷将部分用户几面(如按钮的表面)复制到另外一个区域。当创建特殊效果时,如部分反射效果,该画刷特别有用。

7、BitmapCacheBrush 使用从一个Visual 对象缓存的内容绘制区域。这种画刷和VisualBrush类似,但是如果需要在多个地方重用图形内容或者频繁的重绘图形内容,这种画刷更高效。

SolidColorBrush 

在大多数控件中,设置Foreground属性绘制文本颜色,并设置Background属性绘制文本后面的空间。形状使用类似但不同的属性:Stroke属性用于绘制形状的边框并且Fill属性用于绘制形状的内部。

cmd.Background = new SolidColorBrush(Colors.Red);

cmd.Background = SystemColors.ControlBrush;

int Red =0; int green =255;int blue =0;

cmd.Foreground = new SolidColorBrush(Color.FromRgb(Red,green,blue);

 

LinearGradientBrush 

通过LinearGradientBrush画刷可以创建从一种颜色变化到另一种颜色的混合填充。

<Rectangle Width="150" Height="100">

<Rectangel.Fill>

  <LinearGradientBrush>

    <GradientStop Color="Blue" Offset="0"/>

    <GradientStop Color="White" Offset="1"/>

  </LinearGradientBrush>

</Rectangel.Fill>

</Rectangle >

 

RadialgradientBrush 

需要设定圆心坐标和X坐标和Y坐标的值就可以画一个圆形渐变,在wpf中同样需要这三个元素,

分别对应设Center,RadiusX,RadiusY,当然在wpf中还存在一个“梯度原点“:GradientOrigin。

<Rectangle Height="200" HorizontalAlignment="Left" Margin="128,45,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200">
             <Rectangle.Fill>
                <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
                     <RadialGradientBrush.GradientStops>
                         <GradientStop Color="Yellow" Offset="0"/>
                         <GradientStop Color="Red" Offset="0.25"/>
                        <GradientStop Color="Blue" Offset="0.75"/>
                         <GradientStop Color="LimeGreen" Offset="1"/>
                    </RadialGradientBrush.GradientStops>
                </RadialGradientBrush>16           
  </Rectangle.Fill>
 </Rectangle>

 

ImageBrush(图像画刷)

      这种画刷也是很有意思的,有时我们在炫时需要用图片做装饰,那么此时ImageBrush就可以祝你一臂之力。

 <Grid.Background>
           <ImageBrush x:Name="landBrush" ImageSource="C:\Users\Administrator\Desktop\weibo\64512.gif"/>
 </Grid.Background>

 

VisualBrush(控件画刷)
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <VisualBrush x:Key="test" TileMode="Tile" Opacity="0.8">
            <VisualBrush.Visual>
                <StackPanel>
                    <TextBlock Foreground="Gold">
                        唧唧复唧唧
                    </TextBlock>
                    <TextBlock Foreground="LightBlue">
                       木兰开飞机
                    </TextBlock>
                    <TextBlock Foreground="LightGray">
                       开的什么机
                    </TextBlock>
                    <TextBlock Foreground="Pink">
                       波音747
                    </TextBlock>
                </StackPanel>
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    <Grid>
        <Button Content="我是超大按钮" Height="213" HorizontalAlignment="Left" Margin="32,34,0,0" Name="button1" 
                VerticalAlignment="Top" Width="414" Background="{StaticResource ResourceKey=test}"/>
    </Grid>
</Window>

 

 

DrawingBrush(自定义画刷)

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DrawingBrush x:Key="test">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing>
                            <!-- 绘制矩形 -->
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry RadiusX="0.2" RadiusY="0.5"
                                                       Rect="0.02,0.02,0.96,0.96" />
                            </GeometryDrawing.Geometry>
                            <!-- 矩形填充色 -->
                            <GeometryDrawing.Brush>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="Green" Offset="0" />
                                    <GradientStop Color="Red" Offset="1" />
                                </LinearGradientBrush>
                            </GeometryDrawing.Brush>
                            <!-- 矩形边框 -->
                            <GeometryDrawing.Pen>
                                <Pen Thickness="0.02">
                                    <Pen.Brush>
                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                            <GradientStop Color="AliceBlue" Offset="0" />
                                            <GradientStop Color="Black" Offset="1" />
                                        </LinearGradientBrush>
                                    </Pen.Brush>
                                </Pen>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid>
        <Button Background="{StaticResource ResourceKey=test}" FontSize="40" Content="Button" Height="113" HorizontalAlignment="Left" Margin="89,80,0,0" Name="button1" VerticalAlignment="Top" Width="292" />
    </Grid>
</Window>

 

 

 
posted @ 2014-10-08 11:15  坚持Net  阅读(515)  评论(0编辑  收藏  举报