重写 OnPaint 方法Overriding the OnPaint Method

重写任何.NET Framework 中定义的事件的基本步骤相同,并且在以下列表总结了。The basic steps for overriding any event defined in the .NET Framework are identical and are summarized in the following list.

若要重写继承的事件To override an inherited event

  1. 重写受保护On EventName方法。Override the protected OnEventName method.

  2. 调用On EventName方法重写从基类On EventName方法,因此已注册的委托接收事件。Call the OnEventName method of the base class from the overridden OnEventName method, so that registered delegates receive the event.

Paint在此处详细论述事件,因为每个 Windows 窗体控件必须重写Paint它所继承的事件ControlThe Paint event is discussed in detail here because every Windows Forms control must override the Paint event that it inherits from Control. Control类不知道如何派生的控件需要绘制时,它不提供任何绘制逻辑中的OnPaint方法。The base Control class does not know how a derived control needs to be drawn and does not provide any painting logic in the OnPaint method. OnPaint方法Control只需将调度Paint到已注册的事件接收器的事件。The OnPaint method of Control simply dispatches the Paint event to registered event receivers.

如果您已完成中的示例如何:开发简单的 Windows 窗体控件,你已了解的重写示例OnPaint方法。If you worked through the sample in How to: Develop a Simple Windows Forms Control, you have seen an example of overriding the OnPaint 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);  
   }   
}   

PaintEventArgs类包含用于数据Paint事件。The PaintEventArgs class contains data for the 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 {}  
...  
}  

ClipRectangle 是否要绘制的矩形和Graphics属性是指Graphics对象。ClipRectangle is the rectangle to be painted, and the Graphics property refers to a Graphics object. 中的类System.Drawing管理命名空间提供的功能的 GDI + 中,新的 Windows 图形库的访问权限的类。The classes in the System.Drawing namespace are managed classes that provide access to the functionality of GDI+, the new Windows graphics library. Graphics对象具有用于绘制点、 字符串、 线条、 弧线、 椭圆和许多其他形状的方法。The Graphics object has methods to draw points, strings, lines, arcs, ellipses, and many other shapes.

控件调用其OnPaint方法需要更改其可视显示时。A control invokes its OnPaint method whenever it needs to change its visual display. 此方法反过来引发Paint事件。This method in turn raises the Paint event.

请参阅See also