Back to Devexpress

CustomDrawObjectEventArgs.Cache Property

windowsforms-devexpress-dot-xtrascheduler-dot-customdrawobjecteventargs-c6f742d2.md

latest8.9 KB
Original Source

CustomDrawObjectEventArgs.Cache Property

Gets an object which specifies the storage for the pens, fonts and brushes. Use it for custom painting in Scheduler Reports.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.dll

NuGet Package : DevExpress.Win.Scheduler

Declaration

csharp
public virtual GraphicsCache Cache { get; }
vb
Public Overridable ReadOnly Property Cache As GraphicsCache

Property Value

TypeDescription
GraphicsCache

A GraphicsCache object.

|

Remarks

Please refer to the GraphicsCache class description for more information.

The following sample code handles the SchedulerControl.CustomDrawAppointment event to manually paint appointments. The image below shows the result.

View Example

csharp
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
using System.Drawing.Drawing2D;
private void schedulerControl1_CustomDrawAppointment(object sender, CustomDrawObjectEventArgs e) {
    TimeLineAppointmentViewInfo tlvi = e.ObjectInfo as TimeLineAppointmentViewInfo;
    // This code works only for the Timeline View.
    if(tlvi != null) {
        Rectangle r = e.Bounds;
        r.Offset(3, 3);
        string[] s = tlvi.Appointment.Subject.Split(' ');

        for(int i = 0; i < s.Length; i++) {
            using(var foreBrush = new SolidBrush(colorArray[i]))
                e.Cache.DrawString(s[i], tlvi.Appearance.Font, foreBrush,
r, StringFormat.GenericDefault);
            SizeF shift = e.Cache.CalcTextSize(s[i] + " ", tlvi.Appearance.Font);
            r.X += (int)shift.Width;
        }
        e.Handled = true;
    }
}
vb
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Drawing
Imports System.Drawing.Drawing2D
Private Sub schedulerControl1_CustomDrawAppointment(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs) Handles schedulerControl1.CustomDrawAppointment
    Dim tlvi As TimeLineAppointmentViewInfo = TryCast(e.ObjectInfo, TimeLineAppointmentViewInfo)
    ' This code works only for the Timeline View.
    If tlvi IsNot Nothing Then
        Dim r As Rectangle = e.Bounds
        r.Offset(3, 3)
        Dim s() As String = tlvi.Appointment.Subject.Split(" "c)

        For i As Integer = 0 To s.Length - 1
            Using foreBrush = New SolidBrush(colorArray(i))
                e.Cache.DrawString(s(i), tlvi.Appearance.Font, foreBrush, r, StringFormat.GenericDefault)
            End Using
            Dim shift As SizeF = e.Cache.CalcTextSize(s(i) & " ", tlvi.Appearance.Font)
            r.X += CInt(shift.Width)
        Next i
        e.Handled = True
    End If
End Sub

This code snippet uses the GraphicsCache object accessible via the CustomDrawObjectEventArgs.Cache property to paint a colored rectangle containing text and image on the appointment displayed in the DayViewTimeCells visual element of the Scheduler Report.

csharp
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
using System;
using System.Drawing;
        private void dayViewTimeCells1_CustomDrawAppointment(object sender, CustomDrawObjectEventArgs e)
        {
            AppointmentViewInfo vi = (AppointmentViewInfo)e.ObjectInfo;
            // The DevExpress.XtraScheduler.Native.RectUtils is a helper object for managing rectangles.
            Rectangle imgRect = DevExpress.XtraScheduler.Native.RectUtils.CutFromLeft(vi.InnerBounds, vi.InnerBounds.Width - 18);
            imgRect = DevExpress.XtraScheduler.Native.RectUtils.AlignRectangle(new Rectangle(0, 0, 16, 16), imgRect, ContentAlignment.MiddleCenter);
            // carUsageImages is a collecion of images (DevExpress.Utils.ImageCollection) created from application resources.
            e.Cache.Paint.DrawImage(e.Graphics, carUsageImages.Images[Convert.ToInt32(vi.Appointment.StatusKey)], imgRect);
            Rectangle textRect = DevExpress.XtraScheduler.Native.RectUtils.CutFromRight(vi.InnerBounds, 18);
            using (StringFormat sf = new StringFormat())
            {
                Brush brush = e.Cache.GetSolidBrush(vi.Appearance.ForeColor);
                Font fntBold = e.Cache.GetFont(vi.Appearance.Font, FontStyle.Bold);
                Font fntItalic = e.Cache.GetFont(vi.Appearance.Font, FontStyle.Italic);
                if (vi.Appointment.LongerThanADay)
                {
                    Rectangle[] rowRects = DevExpress.XtraScheduler.Native.RectUtils.SplitHorizontally(textRect, 2);
                    string hours = String.Format(" [{0:F2} h]", vi.AppointmentInterval.Duration.TotalHours);
                    e.Cache.DrawString(vi.DisplayText + hours, fntBold, brush, textRect, sf);
                }
                else
                {
                    Rectangle[] rects = DevExpress.XtraScheduler.Native.RectUtils.SplitVertically(textRect, 3);
                    e.Cache.DrawString(vi.Interval.Start.ToShortTimeString() + " " +
                        vi.Interval.End.ToShortTimeString(), vi.Appearance.Font, brush, rects[0], sf);
                    e.Cache.DrawString(String.Format("{0}", vi.Appointment.Subject), fntBold, brush, rects[1], sf);
                    e.Cache.DrawString(vi.Description, fntItalic, brush, rects[2], sf);
                }
            }
            e.Handled = true;
        }
vb
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Drawing
Imports DevExpress.Utils.Controls
Imports System
Imports System.Drawing
        Private Sub dayViewTimeCells1_CustomDrawAppointment(ByVal sender As Object, ByVal e As CustomDrawObjectEventArgs)
            Dim vi As AppointmentViewInfo = CType(e.ObjectInfo, AppointmentViewInfo)
            ' The DevExpress.XtraScheduler.Native.RectUtils is a helper object for managing rectangles.
            Dim imgRect As Rectangle = DevExpress.XtraScheduler.Native.RectUtils.CutFromLeft(vi.InnerBounds, vi.InnerBounds.Width - 18)
            imgRect = DevExpress.XtraScheduler.Native.RectUtils.AlignRectangle(New Rectangle(0, 0, 16, 16), imgRect, ContentAlignment.MiddleCenter)
            ' carUsageImages is a collecion of images (DevExpress.Utils.ImageCollection) created from application resources.
            e.Cache.Paint.DrawImage(e.Graphics, carUsageImages.Images(Convert.ToInt32(vi.Appointment.StatusKey)), imgRect)
            Dim textRect As Rectangle = DevExpress.XtraScheduler.Native.RectUtils.CutFromRight(vi.InnerBounds, 18)
            Using sf As New StringFormat()
                Dim brush As Brush = e.Cache.GetSolidBrush(vi.Appearance.ForeColor)
                Dim fntBold As Font = e.Cache.GetFont(vi.Appearance.Font, FontStyle.Bold)
                Dim fntItalic As Font = e.Cache.GetFont(vi.Appearance.Font, FontStyle.Italic)
                If vi.Appointment.LongerThanADay Then
                    Dim rowRects() As Rectangle = DevExpress.XtraScheduler.Native.RectUtils.SplitHorizontally(textRect, 2)
                    Dim hours As String = String.Format(" [{0:F2} h]", vi.AppointmentInterval.Duration.TotalHours)
                    e.Cache.DrawString(vi.DisplayText & hours, fntBold, brush, textRect, sf)
                Else
                    Dim rects() As Rectangle = DevExpress.XtraScheduler.Native.RectUtils.SplitVertically(textRect, 3)
                    e.Cache.DrawString(vi.Interval.Start.ToShortTimeString() & " " & vi.Interval.End.ToShortTimeString(), vi.Appearance.Font, brush, rects(0), sf)
                    e.Cache.DrawString(String.Format("{0}", vi.Appointment.Subject), fntBold, brush, rects(1), sf)
                    e.Cache.DrawString(vi.Description, fntItalic, brush, rects(2), sf)
                End If
            End Using
            e.Handled = True
        End Sub

See Also

CustomDrawObjectEventArgs Class

CustomDrawObjectEventArgs Members

DevExpress.XtraScheduler Namespace