使用英语阅读

通过


如何画出轮廓形状

此示例在窗体上绘制空心椭圆和矩形。

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

编译代码

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

可靠的编程

始终都应对创建的消耗系统资源的任何对象调用 Dispose。 在前面的示例中,PenGraphics 对象已创建然后被销毁。

另请参阅