WPF中的brushes


WPF中的brushes  

     颜色

     在WPF中,颜色是由System.Windows.Media下的Color结构体来封装的。Color有三种颜色R,G,B组成。 除了这三个属性,Color还增加了一个alpha管道属性A。它用来表示透明度(Opacity)。值为0时,表示透明(taransparent)。值为255时,表示不透明(opaque)。默认的颜色透明度为opaque。WPF提供了下面的函数来创建颜色。

Color clr = Color.FromRgb(r, g, b)
Color clr = Color.FromArgb(a, r, g, b)

     由RGB创建的颜色被称为标准色sRGB。这对于屏幕输出比较适用,而对于一些特殊情况,如打印输出。颜色要求更丰富,此时可以采用scRGB 颜色。这两种颜色之间有对应关系,改变一种得属性也会影响另一种。

     System.Windows.Media提供了一Colors静态类,包含了141种静态属性色。除了Transparent属性返回的颜色透明度为0。其他140种颜色返回的颜色透明度都是255。 如:Color clr = Colors.PapayaWhip;

     当改变一个区域的背景色(Background)时,我们使用画刷(Brush类)。Brush是一个抽象类。它的类层次关系如下:

Object
    DispatcherObject (abstract)
          DependencyObject
                Freezable (abstract)
                       Animatable (abstract)
                             Brush (abstract)
                                   GradientBrush (abstract)
                                         LinearGradientBrush
                                         RadialGradientBrush
                                   SolidColorBrush
                                   TileBrush (abstract)
                                          DrawingBrush
                                          ImageBrush
                                          VisualBrush

我们在实际应用中使用的是它的实例化继承类。另外,Brushes类也提供了141中静态画刷属性。

     最简单的是SolidColorBrush,它是一个单一色的画刷。使用如下:

Color clr = Color.FromRgb(0, 255, 255);
SolidColorBrush brush = new SolidColorBrush(clr);
Background = brush;
或者:

SolidColorBrush brush = new SolidColorBrush();
brush.Color = Color.FromRgb(128, 0, 128);

渐进画刷(GradientBrush )

线性渐变LinearGradientBrush 。这种画刷需要制定一个起点pt1和终点pt2以及两种对应点的颜色col1和col2。颜色在这两点的范围内进行渐变。点位置的确定以画刷要覆盖的对象为参照的,左上角为(0,0),右下角为(1,1)。如对客户区背景进行绘制:

LinearGradientBrush brush = new LinearGradientBrush(Colors.Red, Colors.Blue,new Point(0,0),new Point(1, 1));
 Background = brush;

放射渐变RadialGradientBrush。这种画刷以一个中心点为圆心,向外逐渐分散。如:

LinearGradientBrush brush = new
LinearGradientBrush();
            brush.StartPoint = new Point(0, 0);
            brush.EndPoint = new Point(1, 0);
            Background = brush;

            // Rainbow mnemonic is the name Roy G.
Biv.
            brush.GradientStops.Add(new
GradientStop(Colors.Red, 0));
            brush.GradientStops.Add(new
GradientStop(Colors.Orange, .17));
            brush.GradientStops.Add(new
GradientStop(Colors.Yellow, .33));
            brush.GradientStops.Add(new
GradientStop(Colors.Green, .5));
            brush.GradientStops.Add(new
GradientStop(Colors.Blue, .67));
            brush.GradientStops.Add(new
GradientStop(Colors.Indigo, .84));
            brush.GradientStops.Add(new
GradientStop(Colors.Violet, 1));
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF ,可以使用 FormattedText 类在后台代码定义并格式化文本,然后将其绘制在画布或其他控件上。以下是在后台代码声明 FormattedText 的示例: ```csharp // 创建一个新的FormattedText对象 FormattedText formattedText = new FormattedText( "Hello World", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 20, Brushes.Red, VisualTreeHelper.GetDpi(this).PixelsPerDip); // 设置文本的对齐方式 formattedText.TextAlignment = TextAlignment.Center; // 设置文本的背景色 formattedText.SetBackgroundBrush(Brushes.Yellow); // 在画布上绘制文本 Canvas canvas = new Canvas(); TextBlock textBlock = new TextBlock(); textBlock.FontFamily = new FontFamily("Arial"); textBlock.FontSize = 20; textBlock.Foreground = Brushes.Red; textBlock.Background = Brushes.Yellow; textBlock.Text = formattedText.Text; // 将 TextBlock 添加到画布 Canvas.SetLeft(textBlock, 10); Canvas.SetTop(textBlock, 10); canvas.Children.Add(textBlock); ``` 在这个示例,我们首先创建一个新的 FormattedText 对象,设置文本、语言环境、文本流动方向、字体类型、字体大小、前景色和 DPI 值。然后,我们可以使用 SetBackgroundBrush 方法设置文本的背景色,并使用 TextAlignment 属性设置对齐方式。 最后,我们在 Canvas 上创建一个 TextBlock 控件,并将其添加到画布。我们可以设置 TextBlock 的属性,如 FontFamily、FontSize、Foreground 和 Background,以匹配我们在 FormattedText 设置的样式。然后,我们将 formattedText.Text 赋值给 TextBlock 的 Text 属性,以便在 TextBlock 显示文本。最后,我们使用 Canvas.SetLeft 和 Canvas.SetTop 方法将 TextBlock 放置在画布上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值