如何:使用纯色绘制区域

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

下面的示例分别使用上述每种方法绘制一个蓝色的 Rectangle

示例

使用预定义画笔

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

<Rectangle Width="50" Height="50" Fill="Blue" />
            ' Create a rectangle and paint it with
            ' a predefined brush.
            Dim myPredefinedBrushRectangle As New Rectangle()
            myPredefinedBrushRectangle.Width = 50
            myPredefinedBrushRectangle.Height = 50
            myPredefinedBrushRectangle.Fill = Brushes.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 并使用蓝色的 ARGB 值描述其 Color

<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>
            Dim myRgbRectangle As New Rectangle()
            myRgbRectangle.Width = 50
            myRgbRectangle.Height = 50
            Dim mySolidColorBrush As 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
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 类提供的更大示例。 有关完整示例,请参见 Brushes Sample(Brushes 示例)。

请参见

参考

Brushes