Understanding the GDI+ Paint Event in VB.NET

In this article you will learn how to Understand the Paint Event in GDI+.
  • 4824

In previous articles, you learned how to draw graphics shapes, curves, and images. In all of these cases, the Graphics object is responsible for the drawing. When we're drawing graphics objects from within a menu or button click event handler, a call to the Invalidate method becomes imperative. I we don't call this method, the form will not paint itself, but if we write the same code on a form's OnPaint or paint event handler, there is no need to invalidate the form. In this section we will find out why that's so.

Understanding the Paint Event

Paint event functionality is defined in System.Windows.Forms.Control class, which is the base class for Windows Forms controls such as Label, ListBox, DataGrid, and TreeView. A paint event is fired when a control is redrawn. The Form class itself is inherited from the Control class. Figure 13.1 shows the Form class hierarchy.

The PaintEventArgs class provides data for the paint event. It provides two read-only properties: ClipRectangle and Graphics.ClipRectangle indicates the rectangle in which to paint, and the Graphics property indicates the Graphics object associated with the paint event of a particular control (including the form itself). Always be careful when you're dealing with the paint event because it is unpredictable and called automatically.


The Control class also provides OnPaint methods, which can be overridden in the derived classes to fire the paint event. The signature of the OnPaint method is defined as follows 

Protected Overridable Sub OnPaint(e As PaintEventArgs)
End Sub


Figure-13_1.gif


FIGURE 13.1: The Form class hierarchy

As this definition shows, OnPaint takes a PaintEventArgs object as its only argument. The Graphics property of PaintEventArgs is used to get the Graphics object associated with a control - including the form.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.