Permalink
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (81 sloc) 4.49 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Overriding the OnPaint Method
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
Paint event, handling in Windows Forms custom control
OnPaint method, overriding in Windows Forms custom controls
e9ca2723-0107-4540-bb21-4f5ffb4a9906
12
dotnet-bot
dotnetcontent
wpickett

Overriding the OnPaint Method

The basic steps for overriding any event defined in the [!INCLUDEdnprdnshort] are identical and are summarized in the following list.

To override an inherited event

  1. Override the protected OnEventName method.

  2. Call the OnEventName method of the base class from the overridden OnEventName method, so that registered delegates receive the event.

The xref:System.Windows.Forms.Control.Paint event is discussed in detail here because every Windows Forms control must override the xref:System.Windows.Forms.Control.Paint event that it inherits from xref:System.Windows.Forms.Control. The base xref:System.Windows.Forms.Control class does not know how a derived control needs to be drawn and does not provide any painting logic in the xref:System.Windows.Forms.Control.OnPaint%2A method. The xref:System.Windows.Forms.Control.OnPaint%2A method of xref:System.Windows.Forms.Control simply dispatches the xref:System.Windows.Forms.Control.Paint event to registered event receivers.

If you worked through the sample in How to: Develop a Simple Windows Forms Control, you have seen an example of overriding the xref:System.Windows.Forms.Control.OnPaint%2A method. The following code fragment is taken from that sample.

Public Class FirstControl  
   Inherits Control  
  
   Public Sub New()  
   End Sub  
  
   Protected Overrides Sub OnPaint(e As PaintEventArgs)  
      ' Call the OnPaint method of the base class.  
      MyBase.OnPaint(e)  
      ' Call methods of the System.Drawing.Graphics object.  
      e.Graphics.DrawString(Text, Font, New SolidBrush(ForeColor), RectangleF.op_Implicit(ClientRectangle))  
   End Sub  
End Class   
public class FirstControl : Control{  
   public FirstControl() {}  
   protected override void OnPaint(PaintEventArgs e) {  
      // Call the OnPaint method of the base class.  
      base.OnPaint(e);  
      // Call methods of the System.Drawing.Graphics object.  
      e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle);  
   }   
}   

The xref:System.Windows.Forms.PaintEventArgs class contains data for the xref:System.Windows.Forms.Control.Paint event. It has two properties, as shown in the following code.

Public Class PaintEventArgs  
   Inherits EventArgs  
   ...  
   Public ReadOnly Property ClipRectangle() As System.Drawing.Rectangle  
      ...  
   End Property  
  
   Public ReadOnly Property Graphics() As System.Drawing.Graphics  
      ...  
   End Property   
   ...  
End Class  
public class PaintEventArgs : EventArgs {  
...  
    public System.Drawing.Rectangle ClipRectangle {}  
    public System.Drawing.Graphics Graphics {}  
...  
}  

xref:System.Windows.Forms.PaintEventArgs.ClipRectangle%2A is the rectangle to be painted, and the xref:System.Windows.Forms.PaintEventArgs.Graphics%2A property refers to a xref:System.Drawing.Graphics object. The classes in the xref:System.Drawing?displayProperty=nameWithType namespace are managed classes that provide access to the functionality of [!INCLUDEndptecgdiplus], the new Windows graphics library. The xref:System.Drawing.Graphics object has methods to draw points, strings, lines, arcs, ellipses, and many other shapes.

A control invokes its xref:System.Windows.Forms.Control.OnPaint%2A method whenever it needs to change its visual display. This method in turn raises the xref:System.Windows.Forms.Control.Paint event.

See Also

Events
Rendering a Windows Forms Control
Defining an Event