如何:用纯色填充形状

若要用纯色填充形状,请创建一个 SolidBrush 对象,然后将该 SolidBrush 对象作为参数传递给 Graphics 类的一个填充方法。 下面的示例演示如何用红色填充椭圆形。

示例

在下面的代码中,SolidBrush 构造函数将 Color 对象用作其唯一参数。 FromArgb 方法使用的值表示颜色的 alpha、红色、绿色和蓝色分量。 每个值必须位于 0 到 255 的范围内。 第一个 255 指示颜色完全不透明,第二个 255 指示红色分量的强度达到最大。 两个零指示绿色和蓝色分量的强度均为 0。

传递给 FillEllipse 方法的四个数字 (0, 0, 100, 60) 指定椭圆形的边框位置和大小。 矩形的左上角为 (0, 0),宽度为 100,高度为 60。

SolidBrush solidBrush = new SolidBrush(
   Color.FromArgb(255, 255, 0, 0));
e.Graphics.FillEllipse(solidBrush, 0, 0, 100, 60);
Dim solidBrush As New SolidBrush( _
   Color.FromArgb(255, 255, 0, 0))
e.Graphics.FillEllipse(solidBrush, 0, 0, 100, 60)

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,后者是 Paint 事件处理程序的参数。

另请参阅