微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight开发历程—(利用C#代码制作取色器)

一个例子是,利用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();
        }
    }

运行结果:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐