Each day in the WinCalendar control can be custom painted by either handling the PaintDay event or overriding the OnPaintDay and/or OnPaintDayBackground methods. This topic demonstrates how to override the OnPaintDay method.
Although handling the PaintDay event and overriding the OnPaintDay and/or OnPaintDayBackground methods can have the same end result, they each have their advantages and disadvantages. When overriding the OnPaintDay and/or OnPaintDayBackground methods, a day can either be completely repainted, or partly modified by changing various properties of the DayRenderStyle object received in the event arguments. This, depending on the weight of the code in the override, is the less time-consuming approach (speed-wise). If the PaintDay event is handled, a day's painting must be completely redone over the default paint, resulting in the day being painted twice and is thus more time consuming (speed-wise).
For example, in the image below, an underline was added to the preview days.
The first code snippets demonstrate the code required in the OnPaintDay override to add an underline to the preview days, by modifying the FontStyle property of the DayRenderStyle object received in the event arguments. Of course, an instance of the custom calendar must be created and added to a form first.
The VB.NET or C# example of how to derive from the WinCalendar class to create a custom calendar can be used. Simply replace the code in the OnPaintDay override with the code below.
See example
VB.NET |
Copy Code |
---|---|
Protected Overrides Sub OnPaintDay( ByVal e As PaintDayEventArgs ) If e.RenderStyle.UIState.IsPreviewDay Then e.RenderStyle.FontStyle = FontStyle.Underline End If MyBase.OnPaintDay( e ) End Sub |
C# |
Copy Code |
---|---|
protected override void OnPaintDay( PaintDayEventArgs e ) { if( e.RenderStyle.UIState.IsPreviewDay ) { e.RenderStyle.FontStyle = FontStyle.Underline; } base.OnPaintDay( e ); } |
The following code snippets demonstrate how to handle the PaintDay event to add an underline to the preview days. Contrary to the code above where the OnPaintDay method is overridden and the DayRenderStyle object modified, the days' painting must be completely redone.
This code assumes that you have a WinCalendar named WinCalendar1 located on a form.
See example
VB.NET |
Copy Code |
---|---|
' The following "Imports" must be added after the "Imports" already in the form. Imports Xceed.Editors ' In form's load event, subscribe to the WinCalendar's PaintDay event. AddHandler WinCalendar1.PaintDay, AddressOf Me.Calendar_PaintDay Private Sub Calendar_PaintDay( ByVal sender As Object, ByVal e As PaintDayEventArgs ) If e.RenderStyle.UIState.IsPreviewDay Then Dim brush As New SolidBrush( winCalendar1.BackColor ) e.Graphics.FillRectangle( brush, e.ClipRectangle ) brush.Color = WinCalendar1.PreviewDaysForeColor Dim rect As New RectangleF(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, _ e.ClipRectangle.Height) e.Graphics.DrawString( e.Value.Day.ToString(), New Font( WinCalendar1.Font, _ FontStyle.Underline ), brush, rect ) brush.Dispose() End If End Sub |
C# |
Copy Code |
---|---|
// The following "using" must be added after the "usings" already in the form. using Xceed.Editors; // In the form's load event, subscribe to the WinCalendar's PaintDay event. winCalendar1.PaintDay += new PaintDayEventHandler( this.Calendar_PaintDay ); private void Calendar_PaintDay(object sender, Xceed.Editors.PaintDayEventArgs e) { if( e.RenderStyle.UIState.IsPreviewDay ) { using( SolidBrush brush = new SolidBrush( winCalendar1.BackColor ) ) { e.Graphics.FillRectangle( brush, e.ClipRectangle ); brush.Color = winCalendar1.PreviewDaysForeColor; e.Graphics.DrawString( e.Value.Day.ToString(), new Font( winCalendar1.Font, _ FontStyle.Underline ), brush, e.ClipRectangle ); } } } |
To accomplish more complex painting tasks, it is preferable to override the OnPaintDay and/or OnPaintDayBackground methods rather than handle the PaintDay event. For example, in the picture below, a custom calendar control was created to draw images on certain dates (special occasions) with the option to repeat the occasion at various intervals.
To accomplish the behavior displayed in the image above, a custom calendar control that derives from the WinCalendar class was created, in addition to a SpecialOccasion class and RepeatPattern enumeration. In the custom calendar control, the OnPaintDay and OnPaintDayBackground methods were overridden to paint the "special occasions".
The SpecialOccasion class contains the "special" date, the image to paint, as well as the repetition interval, while the RepeatPattern enumeration specifies at what interval the "special occasion" occurs.
The code below does not contain the code for the custom calendar control. Click here to view the VB.NET version, or here for the C# version of the custom calendar control's (CustomCalendar) code.
See example
VB.NET |
Copy Code |
---|---|
' The following "Import" must be added after the "Imports" already in the form. Imports Xceed.Editors ' This portion of the code is located in a form's Load event. Dim list As New ArrayList list.Add( New SpecialOccasion( New DateTime( 2004, 10, 1 ), "d:\images\sun.gif", _ RepeatPattern.EveryMonth, True ) ) list.Add( New SpecialOccasion( New DateTime( 2004, 12, 25 ), "d:\images\christmastree.gif", _ RepeatPattern.EveryYear, False ) ) list.Add( New SpecialOccasion( New DateTime( 2004, 11, 16 ), "d:\images\gift.gif", _ RepeatPattern.EveryYear, False ) ) list.Add( New SpecialOccasion( New DateTime( 2004, 10, 31 ), "d:\images\pumpkin.gif", _ RepeatPattern.EveryYear, False ) ) Dim calendar As New CustomCalendar( list ) calendar.Location = New Point( 10, 10 ) Me.Controls.Add( calendar ) |
C# |
Copy Code |
---|---|
// The following "using" must be added after the "usings" already in the form. using Xceed.Editors; // This portion of the code is located in a form's Load event. ArrayList list = new ArrayList(); list.Add( new SpecialOccasion( new DateTime( 2004, 10, 1 ), "d:\\images\\sun.gif", RepeatPattern.EveryMonth, true ) ); list.Add( new SpecialOccasion( new DateTime( 2004, 12, 25 ), "d:\\images\\christmastree.gif", RepeatPattern.EveryYear, false ) ); list.Add( new SpecialOccasion( new DateTime( 2004, 11, 16 ), "d:\\images\\gift.gif", RepeatPattern.EveryYear, false ) ); list.Add( new SpecialOccasion( new DateTime( 2004, 10, 31 ), "d:\\images\\pumpkin.gif", RepeatPattern.EveryYear, false ) ); CustomCalendar calendar = new CustomCalendar( list ); calendar.Location = new Point( 10, 10 ); this.Controls.Add( calendar ); |