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

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

示例

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;
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();
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()

编译代码

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

可靠编程

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

另请参阅