如何:使用纯色绘制区域How to: Paint an Area with a Solid Color

要使用Red纯色绘制区域,可以使用预定义的系统画笔(如 或Blue,)或者可以使用 Alpha、红色、绿色和SolidColorBrush蓝色值创建新Color区域并描述其区域。To paint an area with a solid color, you can use a predefined system brush, such as Red or Blue, or you can create a new SolidColorBrush and describe its Color using alpha, red, green, and blue values. 在 XAML 中,还可以使用十六进制表示法来利用纯色绘制区域。In XAML, you may also paint an area with a solid color by using hexidecimal notation.

以下示例使用以下每种技术来绘制Rectangle蓝色。The following examples uses each of these techniques to paint a Rectangle blue.

示例Example

使用预定义画笔Using a Predefined Brush

在下面的示例中,使用预定义的画笔Blue绘制一个矩形蓝色。In the following example uses the predefined brush Blue to paint a 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;

使用十六进制表示法Using Hexadecimal Notation

下一个示例使用 8 位十六进制表示法绘制一个蓝色矩形。The next example uses 8-digit hexadecimal notation to paint a rectangle blue.

<!-- 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 值Using ARGB Values

下一个示例创建SolidColorBrushColor描述其使用颜色为蓝色的 ARGB 值。The next example creates a SolidColorBrush and describes its Color using the ARGB values for the color blue.

<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请参阅结构。For other ways of describing color, see the Color structure.

相关主题Related Topics

有关相关信息SolidColorBrush和其他示例,请参阅"具有纯色和渐变的绘画概述"。For more information about SolidColorBrush and additional examples, see the Painting with Solid Colors and Gradients Overview overview.

此代码示例是为类提供的更大示例的SolidColorBrush一部分。This code example is part of a larger example provided for the SolidColorBrush class. 有关完整示例,请参阅画笔示例For the complete sample, see the Brushes Sample.

另请参阅See also