Share via


如何:在 Windows 窗体上绘制实心矩形

此示例在窗体上绘制实心矩形。

示例

Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.FillRectangle(myBrush, New Rectangle(0, 0, 200, 300))
myBrush.Dispose()
formGraphics.Dispose()
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
System::Drawing::SolidBrush^ myBrush =
    gcnew System::Drawing::SolidBrush(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->FillRectangle(myBrush, Rectangle(0, 0, 200, 300));
delete myBrush;
delete formGraphics;

编译代码

不能在 Load 事件处理程序中调用此方法。 如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。 若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

对任何消耗系统资源的对象(如 BrushGraphics 对象)都应调用 Dispose

请参见

参考

FillRectangle

OnPaint

概念

GDI+ 中的画笔和实心形状

其他资源

图形编程入门

Windows 窗体中的图形和绘制

使用钢笔绘制线条和形状