• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

运行结果:





鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#windows服务发布时间:2022-07-13
下一篇:
C#使用/配置Log4Net发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap