如何使用纯色填充形状

若要使用纯色填充形状,请创建一个 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 事件处理程序的参数。

另请参阅