Back to Devexpress

SchedulerControl.CustomDrawAppointmentBackground Event

windowsforms-devexpress-dot-xtrascheduler-dot-schedulercontrol-e940ea70.md

latest10.7 KB
Original Source

SchedulerControl.CustomDrawAppointmentBackground Event

Enables the backgrounds of appointments to be painted manually.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.dll

NuGet Package : DevExpress.Win.Scheduler

Declaration

csharp
public event CustomDrawObjectEventHandler CustomDrawAppointmentBackground
vb
Public Event CustomDrawAppointmentBackground As CustomDrawObjectEventHandler

Event Data

The CustomDrawAppointmentBackground event's data class is CustomDrawObjectEventArgs. The following properties provide information specific to this event:

PropertyDescription
BoundsReturns the bounding rectangle of the drawing area.
CacheGets an object which specifies the storage for the pens, fonts and brushes. Use it for custom painting in Scheduler Reports.
GraphicsGets an object used for painting.
HandledGets or sets whether an event was handled. If it was handled, the default actions are not required.
ObjectInfoGets information on the painted element.

The event data class exposes the following methods:

MethodDescription
DrawDefault()Renders the element using the default drawing mechanism.
DrawHtml(HtmlTemplate, DxHtmlPainterContext, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event. The context parameter allows you to assign an object that transfers mouse events to template elements.
DrawHtml(HtmlTemplate, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event.
GetDisplayValue(String)
GetValue(String)

Remarks

The CustomDrawAppointmentBackground event is raised before the background of an appointment is painted. The event parameter’s CustomDrawObjectEventArgs.ObjectInfo property provides all the information necessary to paint an appointment’s background. The return value of this property should be typecast to the AppointmentViewInfo type.

Set the CustomDrawObjectEventArgs.Handled property to true to prohibit default appointment painting.

Example

This example illustrates how to partially fill the appointment background in gray. The color coverage is dependent on an individually calculated parameter for each appointment.

The colored rectangle is drawn using the GraphicsCache.FillRectangle method.

csharp
public static void scheduler_CustomDrawAppointmentBackground(object sender, CustomDrawObjectEventArgs e)
{
    AppointmentViewInfo viewInfo = e.ObjectInfo as AppointmentViewInfo;
    // Specify the ratio of a completed task to the entire task.
    double completenessRatio = 0.25 * ((int)(viewInfo.Appointment.ResourceId) % 4);
    // Draw an appointment as usual.
    e.DrawDefault();
    // Draw a background rectangle.
    Rectangle bounds = CalculateEntireAppointmentBounds(viewInfo);
    DrawBackGroundCore(e.Cache, bounds, completenessRatio);
    // Indicate that no default drawing is required.
    e.Handled = true;
}
static Rectangle CalculateEntireAppointmentBounds(AppointmentViewInfo viewInfo)
{
    int leftOffset = 0;
    int rightOffset = 0;
    double scale = viewInfo.Bounds.Width / viewInfo.Interval.Duration.TotalMilliseconds;
    if (!viewInfo.HasLeftBorder)
    {
        double hidden = (viewInfo.Interval.Start - viewInfo.AppointmentInterval.Start).TotalMilliseconds;
        leftOffset = (int)(hidden * scale);
    }
    if (!viewInfo.HasRightBorder)
    {
        double hidden = (viewInfo.AppointmentInterval.End - viewInfo.Interval.End).TotalMilliseconds;
        rightOffset = (int)(hidden * scale);
    }
    Rectangle bounds = viewInfo.InnerBounds;
    return Rectangle.FromLTRB(bounds.Left - leftOffset, bounds.Y, bounds.Right + rightOffset, bounds.Bottom);
}
static void DrawBackGroundCore(DevExpress.Utils.Drawing.GraphicsCache cache, Rectangle bounds, double completenessRatio)
{
   cache.FillRectangle(Brushes.LightGray, new Rectangle(bounds.X, bounds.Y, (int)(bounds.Width * completenessRatio), bounds.Height));
}
vb
Public Shared Sub scheduler_CustomDrawAppointmentBackground(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs)
    Dim viewInfo As AppointmentViewInfo = TryCast(e.ObjectInfo, AppointmentViewInfo)
    ' Specify the ratio of a completed task to the entire task.
    Dim completenessRatio As Double = 0.25 * (CInt((viewInfo.Appointment.ResourceId)) Mod 4)
    ' Draw an appointment as usual.
    e.DrawDefault()
    ' Draw a background rectangle.
    Dim bounds As Rectangle = CalculateEntireAppointmentBounds(viewInfo)
    DrawBackGroundCore(e.Cache, bounds, completenessRatio)
    ' Indicate that no default drawing is required.
    e.Handled = True
End Sub
Private Shared Function CalculateEntireAppointmentBounds(ByVal viewInfo As AppointmentViewInfo) As Rectangle
    Dim leftOffset As Integer = 0
    Dim rightOffset As Integer = 0
    Dim scale As Double = viewInfo.Bounds.Width / viewInfo.Interval.Duration.TotalMilliseconds
    If Not viewInfo.HasLeftBorder Then
        Dim hidden As Double = (viewInfo.Interval.Start - viewInfo.AppointmentInterval.Start).TotalMilliseconds
        leftOffset = CInt((hidden * scale))
    End If
    If Not viewInfo.HasRightBorder Then
        Dim hidden As Double = (viewInfo.AppointmentInterval.End - viewInfo.Interval.End).TotalMilliseconds
        rightOffset = CInt((hidden * scale))
    End If
    Dim bounds As Rectangle = viewInfo.InnerBounds
    Return Rectangle.FromLTRB(bounds.Left - leftOffset, bounds.Y, bounds.Right + rightOffset, bounds.Bottom)
End Function
Private Shared Sub DrawBackGroundCore(ByVal cache As DevExpress.Utils.Drawing.GraphicsCache, ByVal bounds As Rectangle, ByVal completenessRatio As Double)
    cache.FillRectangle(Brushes.LightGray, New Rectangle(bounds.X, bounds.Y, CInt((bounds.Width * completenessRatio)), bounds.Height))
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomDrawAppointmentBackground event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-scheduler-resolve-appointment-conflicts/CS/DXApplication1/Form1.cs#L47

csharp
schedulerControl1.AllowAppointmentConflicts += SchedulerControl1_AllowAppointmentConflicts;
schedulerControl1.CustomDrawAppointmentBackground += SchedulerControl1_CustomDrawAppointmentBackground;

winforms-scheduler-resolve-appointment-conflicts/VB/DXApplication1/Form1.vb#L38

vb
AddHandler schedulerControl1.AllowAppointmentConflicts, AddressOf SchedulerControl1_AllowAppointmentConflicts
AddHandler schedulerControl1.CustomDrawAppointmentBackground, AddressOf SchedulerControl1_CustomDrawAppointmentBackground
schedulerControl1.DataStorage.Appointments.Clear()

See Also

AppointmentViewInfoCustomizing

CustomDrawAppointment

DrawDefault()

How to: Display Custom Images for Appointments

How to: Hide the Reminder (Bell) Icon for Outdated Appointments in a Series

How to: Custom Paint Day Headers

How to: Hide Grid Lines in the View

SchedulerControl Class

SchedulerControl Members

DevExpress.XtraScheduler Namespace