mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-08 07:56:07 +02:00
Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
---
|
||||
title: "Overriding the OnPaint Method"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "Paint event [Windows Forms], handling in Windows Forms custom control"
|
||||
- "OnPaint method [Windows Forms], overriding in Windows Forms custom controls"
|
||||
ms.assetid: e9ca2723-0107-4540-bb21-4f5ffb4a9906
|
||||
---
|
||||
# Overriding the OnPaint Method
|
||||
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. Override the protected `On`*EventName* method.
|
||||
|
||||
2. Call the `On`*EventName* method of the base class from the overridden `On`*EventName* 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](how-to-develop-a-simple-windows-forms-control.md), 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.
|
||||
|
||||
```vb
|
||||
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
|
||||
```
|
||||
|
||||
```csharp
|
||||
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.
|
||||
|
||||
```vb
|
||||
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
|
||||
```
|
||||
|
||||
```csharp
|
||||
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 GDI+, 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](https://docs.microsoft.com/dotnet/standard/events/index)
|
||||
- [Rendering a Windows Forms Control](rendering-a-windows-forms-control.md)
|
||||
- [Defining an Event](defining-an-event-in-windows-forms-controls.md)
|
||||
Reference in New Issue
Block a user