The basic steps for overriding any inherited event are the same. and involve:
The paint event is discussed in detail because every Win Forms control that you develop must override the paint logic provided by RichControl. This is because RichControl does not know how a control wants to be drawn, and cannot provide implementation that your control can use. The OnPaint method of RichControl simply dispatches the event to registered event receivers.
If you worked through the Walkthrough: Developing a Simple Win Forms Control, you have seen an example of overriding the OnPaint method. The code fragment from the walkthrough is reproduced below.
[C#] public class FirstControl:RichControl{ public FirstControl() {…} protected override void OnPaint(PaintEventArgs e) { //call the base class's OnPaint method base.OnPaint(e); //call methods of the System.Drawing.Graphics object e.Graphics.DrawString(Text, Font, new SolidBrush (ForeColor), ClientRectangle); } }
The PaintEventArgs class contains data for the Paint event. It has two properties as shown below.
[C#] public class PaintEventArgs : EventArgs { … public System.Drawing.Rectangle ClipRectangle {…} public System.Drawing.Graphics Graphics {…} … }
ClipRectangle is the rectangle where painting is to be done, and the Graphics property refers to a System.Drawing.Graphics object. The classes in the System.Drawing namespace are managed classes that encapsulate the functionality of the new graphics library, the Graphical Device Interface+ (GDI+). The Graphics object has methods to draw points, strings, lines, arcs, ellipses, and many other shapes.
A paint event is generated whenever the visual display of the control changes; it results in a call to the OnPaint method of your control.