活动
如何画出轮廓形状
此示例在窗体上绘制空心椭圆和矩形。
private:
void DrawEllipse()
{
System::Drawing::Pen^ myPen =
gcnew System::Drawing::Pen(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->DrawEllipse(myPen, Rectangle(0, 0, 200, 300));
delete myPen;
delete formGraphics;
}
private:
void DrawRectangle()
{
System::Drawing::Pen^ myPen =
gcnew System::Drawing::Pen(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 300));
delete myPen;
delete formGraphics;
}
C#
private void DrawEllipse()
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 200, 300));
myPen.Dispose();
formGraphics.Dispose();
}
private void DrawRectangle()
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 200, 300));
myPen.Dispose();
formGraphics.Dispose();
}
Private Sub DrawEllipse()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(0, 0, 200, 300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
无法在 Load 事件处理程序中调用此方法。 如果窗体被调整大小或被另一个窗体遮盖,则已绘制的内容不会被重绘。 若要自动重绘内容,应重写 OnPaint 方法。
始终都应对创建的消耗系统资源的任何对象调用 Dispose。 在前面的示例中,Pen 和 Graphics 对象已创建然后被销毁。
其他资源
文档
-
如何:在 Windows 窗体上绘制填充矩形 - Windows Forms .NET Framework
了解如何以编程方式在 Windows 窗体上绘制填充的矩形。 另请了解如何编译代码。
-
图形和绘图 - Windows Forms .NET Framework
了解图形、笔、画笔和颜色对象,以及如何执行图形形状、绘图文本或显示 Windows 窗体中的图像等任务。
-
图形编程入门 - Windows Forms .NET Framework
开始在 Windows 窗体应用程序中使用 GDI+。 了解如何完成多个 GDI+ 任务,例如绘制和填充形状和文本。