Override del metodo OnPaintOverriding the OnPaint Method
I passaggi di base per eseguire l'override di qualsiasi evento definito nel .NET Framework sono identici e vengono riepilogati nell'elenco seguente.The basic steps for overriding any event defined in the .NET Framework are identical and are summarized in the following list.
Per eseguire l'override di un evento ereditatoTo override an inherited event
Eseguire l'override del
On
metodo EventName protetto.Override the protectedOn
EventName method.Chiamare il
On
metodo EventName della classe base dal metodo EventName sottoposto a overrideOn
, in modo che i delegati registrati ricevano l'evento.Call theOn
EventName method of the base class from the overriddenOn
EventName method, so that registered delegates receive the event.
L' Paint evento viene descritto in dettaglio in questo articolo perché ogni controllo Windows Forms deve eseguire l'override dell' Paint evento da cui eredita Control .The Paint event is discussed in detail here because every Windows Forms control must override the Paint event that it inherits from Control. La classe base non Control sa come deve essere disegnato un controllo derivato e non fornisce alcuna logica di disegno nel OnPaint metodo.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. Il OnPaint metodo di Control Invia semplicemente l' Paint evento ai ricevitori di eventi registrati.The OnPaint method of Control simply dispatches the Paint event to registered event receivers.
Se è stato usato l'esempio in procedura: sviluppare un semplice controllo di Windows Forms, è stato illustrato un esempio di override del OnPaint metodo.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. Il frammento di codice seguente viene tratto da tale esempio.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);
}
}
La PaintEventArgs classe contiene i dati per l' Paint evento.The PaintEventArgs class contains data for the Paint event. Dispone di due proprietà, come illustrato nel codice seguente.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 è il rettangolo da disegnare e la Graphics proprietà fa riferimento a un Graphics oggetto.ClipRectangle is the rectangle to be painted, and the Graphics property refers to a Graphics object. Le classi nello System.Drawing spazio dei nomi sono classi gestite che forniscono l'accesso alla funzionalità di GDI+, la nuova libreria grafica di Windows.The classes in the System.Drawing namespace are managed classes that provide access to the functionality of GDI+, the new Windows graphics library. L' Graphics oggetto dispone di metodi per creare punti, stringhe, linee, archi, ellissi e molte altre forme.The Graphics object has methods to draw points, strings, lines, arcs, ellipses, and many other shapes.
Un controllo richiama il OnPaint Metodo ogni volta che è necessario modificare la visualizzazione.A control invokes its OnPaint method whenever it needs to change its visual display. Questo metodo genera, a sua volta Paint , l'evento.This method in turn raises the Paint event.