如何:用纯色绘制区域

若要使用纯色绘制区域,可以使用预定义的系统画笔,例如 RedBlue或者,也可以创建一个新 SolidColorBrush 区域,并使用 alpha、红色、绿色和蓝色值来描述该区域 Color 。 在 XAML 中,还可以使用十六进制表示法绘制具有纯色的区域。

以下示例使用这些技术中的每一 Rectangle 种来绘制蓝色。

示例:

使用预定义画笔

在下面的示例中,使用预定义的画笔 Blue 绘制矩形蓝色。

<Rectangle Width="50" Height="50" Fill="Blue" />
// Create a rectangle and paint it with
// a predefined brush.
Rectangle myPredefinedBrushRectangle = new Rectangle();
myPredefinedBrushRectangle.Width = 50;
myPredefinedBrushRectangle.Height = 50;
myPredefinedBrushRectangle.Fill = Brushes.Blue;

使用十六进制表示法

下一个示例使用 8 位十六进制表示法绘制矩形蓝色。

<!-- Note that the first two characters "FF" of the 8-digit
     value is the alpha which controls the transparency of 
     the color. Therefore, to make a completely transparent
     color (invisible), use "00" for those digits (e.g. #000000FF). -->
<Rectangle Width="50" Height="50" Fill="#FF0000FF" />

使用 ARGB 值

下一个示例创建一个 SolidColorBrush 并使用 Color 蓝色 ARGB 值的 ARGB 值。

<Rectangle Width="50" Height="50">
  <Rectangle.Fill>
    <SolidColorBrush>
     <SolidColorBrush.Color>

        <!-- Describes the brush's color using
             RGB values. Each value has a range of 0-255.  
             R is for red, G is for green, and B is for blue.
             A is for alpha which controls transparency of the
             color. Therefore, to make a completely transparent
             color (invisible), use a value of 0 for Alpha. -->
        <Color A="255" R="0" G="0" B="255" />
     </SolidColorBrush.Color>
    </SolidColorBrush>
  </Rectangle.Fill>
</Rectangle>
Rectangle myRgbRectangle = new Rectangle();
myRgbRectangle.Width = 50;
myRgbRectangle.Height = 50;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();

// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myRgbRectangle.Fill = mySolidColorBrush;

有关描述颜色的其他方法,请参阅 Color 结构。

相关主题

有关其他示例的详细信息 SolidColorBrush ,请参阅 “使用纯色和渐变绘制”概述

该代码示例是 SolidColorBrush 类的一个更大示例的一部分。 有关完整示例,请参阅 画笔示例

另请参阅