第一个例子是,利用C#代码绘制画刷,例子很简单直接上代码:
<StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical">
<Ellipse x:Name="ellipse" Width="260" Height="260" Fill="GreenYellow"/>
<TextBlock x:Name="txb_txb" FontSize="30" Text="使用C#绘制Brush" />
</StackPanel>
C#:
public partial class DrawBruseWithCSharp : UserControl
{
public DrawBruseWithCSharp()
{
InitializeComponent();
//绘制单色填充,
ellipse.Stroke = new SolidColorBrush(Colors.Black);
ellipse.StrokeThickness = 3;
//绘制渐变填充
LinearGradientBrush lgb = new LinearGradientBrush();
lgb.GradientStops.Add(new GradientStop() { Color=Colors.Green, Offset=0});
lgb.GradientStops.Add(new GradientStop() { Color=Colors.Yellow,Offset=1});
txb_txb.Foreground = lgb;
ellipse.Fill = lgb;
}
}
运行结果:
第二个例子。利用C#代码绘制取色器
XAML:
<Grid x:Name="LayoutRoot" Background="White">
<!-- 添加圆角边框-->
<Border BorderBrush="Black" Margin="5" BorderThickness="3" Background="AliceBlue" CornerRadius="10">
<!--添加网络边矩-->
<Grid Margin="5" ShowGridLines="False">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--添加颜色滚动条-->
<Slider x:Name="sliderR" Maximum="255" Value="255" Grid.Row="0" Grid.Column="1" />
<Slider x:Name="sliderG" Maximum="255" Value="255" Grid.Row="1" Grid.Column="1" />
<Slider x:Name="sliderB" Maximum="255" Value="255" Grid.Row="2" Grid.Column="1" />
<Slider x:Name="sliderA" Maximum="255" Value="255" Grid.Row="3" Grid.Column="1" />
<!--添加说明文字-->
<TextBlock x:Name="txb_R" Text="R:" Width="50" Height="20" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBlock x:Name="txb_G" Text="G:" Width="50" Height="20" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBlock x:Name="txb_B" Text="B:" Width="50" Height="20" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBlock x:Name="txb_A" Text="透明度:" Width="50" Height="20" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
<!--显示颜色-->
<Rectangle x:Name="rect" Fill="Green" Stroke="Black" Width="150" Height="120" Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" />
<!--显示颜色值-->
<TextBlock x:Name="txb_Color" Width="140" Height="26" Grid.Row="3" Grid.Column="0" />
<!--标题说明文字-->
<TextBlock Text="色值" Grid.Row="3" Grid.Column="0" />
<TextBlock Text="Silverlight取色器" FontSize="20" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0" Height="30" />
</Grid>
</Border>
</Grid>
C#:
public partial class DrawGetColorControl : UserControl
{
public DrawGetColorControl()
{
InitializeComponent();
//设置颜色
setColor();
sliderA.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderValueChanged);
sliderB.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderValueChanged);
sliderR.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderValueChanged);
sliderG.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SliderValueChanged);
}
void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
setColor();
}
private void setColor()
{
SolidColorBrush scb = new SolidColorBrush(
Color.FromArgb(
(byte)sliderA.Value,
(byte)sliderR.Value,
(byte)sliderG.Value,
(byte)sliderB.Value));
rect.Fill = scb;
txb_Color.Text = scb.Color.ToString();
}
}
运行结果: