Bewerken

Delen via


Painting and drawing on controls (Windows Forms .NET)

Custom painting of controls is one of the many complicated tasks made easy by Windows Forms. When authoring a custom control, you have many options available to handle your control's graphical appearance. If you're authoring a custom control, that is, a control that inherits from Control, you must provide code to render its graphical representation.

If you're creating a composite control, that is a control that inherits from UserControl or one of the existing Windows Forms controls, you may override the standard graphical representation and provide your own graphics code.

If you want to provide custom rendering for an existing control without creating a new control, your options become more limited. However, there are still a wide range of graphical possibilities for your controls and applications.

The following elements are involved in control rendering:

  • The drawing functionality provided by the base class System.Windows.Forms.Control.
  • The essential elements of the GDI graphics library.
  • The geometry of the drawing region.
  • The procedure for freeing graphics resources.

Drawing provided by control

The base class Control provides drawing functionality through its Paint event. A control raises the Paint event whenever it needs to update its display. For more information about events in the .NET, see Handling and raising events.

The event data class for the Paint event, PaintEventArgs, holds the data needed for drawing a control - a handle to a graphics object and a rectangle that represents the region to draw in.

public class PaintEventArgs : EventArgs, IDisposable
{

    public System.Drawing.Rectangle ClipRectangle {get;}
    public System.Drawing.Graphics Graphics {get;}

    // Other properties and methods.
}
Public Class PaintEventArgs
    Inherits EventArgs
    Implements IDisposable

    Public ReadOnly Property ClipRectangle As System.Drawing.Rectangle
    Public ReadOnly Property Graphics As System.Drawing.Graphics

    ' Other properties and methods.
End Class

Graphics is a managed class that encapsulates drawing functionality, as described in the discussion of GDI later in this article. The ClipRectangle is an instance of the Rectangle structure and defines the available area in which a control can draw. A control developer can compute the ClipRectangle using the ClipRectangle property of a control, as described in the discussion of geometry later in this article.

OnPaint

A control must provide rendering logic by overriding the OnPaint method that it inherits from Control. OnPaint gets access to a graphics object and a rectangle to draw in through the Graphics and the ClipRectangle properties of the PaintEventArgs instance passed to it.

The following code uses the System.Drawing namespace:

protected override void OnPaint(PaintEventArgs e)
{
    // Call the OnPaint method of the base class.
    base.OnPaint(e);

    // Declare and instantiate a new pen that will be disposed of at the end of the method.
    using var myPen = new Pen(Color.Aqua);

    // Create a rectangle that represents the size of the control, minus 1 pixel.
    var area = new Rectangle(new Point(0, 0), new Size(this.Size.Width - 1, this.Size.Height - 1));

    // Draw an aqua rectangle in the rectangle represented by the control.
    e.Graphics.DrawRectangle(myPen, area);
}
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)

    ' Declare and instantiate a drawing pen.
    Using myPen = New System.Drawing.Pen(Color.Aqua)

        ' Create a rectangle that represents the size of the control, minus 1 pixel.
        Dim area = New Rectangle(New Point(0, 0), New Size(Me.Size.Width - 1, Me.Size.Height - 1))

        ' Draw an aqua rectangle in the rectangle represented by the control.
        e.Graphics.DrawRectangle(myPen, area)

    End Using
End Sub

The OnPaint method of the base Control class doesn't implement any drawing functionality but merely invokes the event delegates that are registered with the Paint event. When you override OnPaint, you should typically invoke the OnPaint method of the base class so that registered delegates receive the Paint event. However, controls that paint their entire surface shouldn't invoke the base class's OnPaint, as this introduces flicker.

Note

Don't invoke OnPaint directly from your control; instead, invoke the Invalidate method (inherited from Control) or some other method that invokes Invalidate. The Invalidate method in turn invokes OnPaint. The Invalidate method is overloaded, and, depending on the arguments supplied to Invalidate e, redraws either some or all of its screen area.

The code in the OnPaint method of your control will execute when the control is first drawn, and whenever it is refreshed. To ensure that your control is redrawn every time it is resized, add the following line to the constructor of your control:

SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.ResizeRedraw, True)

OnPaintBackground

The base Control class defines another method that is useful for drawing, the OnPaintBackground method.

protected virtual void OnPaintBackground(PaintEventArgs e);
Protected Overridable Sub OnPaintBackground(e As PaintEventArgs)

OnPaintBackground paints the background (and in that way, the shape) of the window and is guaranteed to be fast, while OnPaint paints the details and might be slower because individual paint requests are combined into one Paint event that covers all areas that have to be redrawn. You might want to invoke the OnPaintBackground if, for instance, you want to draw a gradient-colored background for your control.

While OnPaintBackground has an event-like nomenclature and takes the same argument as the OnPaint method, OnPaintBackground is not a true event method. There is no PaintBackground event and OnPaintBackground doesn't invoke event delegates. When overriding the OnPaintBackground method, a derived class is not required to invoke the OnPaintBackground method of its base class.

GDI+ Basics

The Graphics class provides methods for drawing various shapes such as circles, triangles, arcs, and ellipses, and methods for displaying text. The System.Drawing namespace contains namespaces and classes that encapsulate graphics elements such as shapes (circles, rectangles, arcs, and others), colors, fonts, brushes, and so on.

Geometry of the Drawing Region

The ClientRectangle property of a control specifies the rectangular region available to the control on the user's screen, while the ClipRectangle property of PaintEventArgs specifies the area that is painted. A control might need to paint only a portion of its available area, as is the case when a small section of the control's display changes. In those situations, a control developer must compute the actual rectangle to draw in and pass that to Invalidate. The overloaded versions of Invalidate that take a Rectangle or Region as an argument use that argument to generate the ClipRectangle property of PaintEventArgs.

Freeing Graphics Resources

Graphics objects are expensive because they use system resources. Such objects include instances of the System.Drawing.Graphics class and instances of System.Drawing.Brush, System.Drawing.Pen, and other graphics classes. It's important that you create a graphics resource only when you need it and release it soon as you're finished using it. If you create an instance of a type that implements the IDisposable interface, call its Dispose method when you're finished with it to free resources.

See also