Share via


如何:绘制空心形状

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

示例

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

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。 前面的示例创建并释放了 PenGraphics 对象。

另请参阅