Back to Devexpress

DiagramControl.CustomDrawBackground Event

windowsforms-devexpress-dot-xtradiagram-dot-diagramcontrol-47f1ea79.md

latest7.0 KB
Original Source

DiagramControl.CustomDrawBackground Event

Occurs before the Canvas background is rendered.

Namespace : DevExpress.XtraDiagram

Assembly : DevExpress.XtraDiagram.v25.2.dll

NuGet Package : DevExpress.Win.Diagram

Declaration

csharp
[DiagramCategory(DiagramCategory.DiagramPaint)]
public event EventHandler<CustomDrawBackgroundEventArgs> CustomDrawBackground
vb
<DiagramCategory(DiagramCategory.DiagramPaint)>
Public Event CustomDrawBackground As EventHandler(Of CustomDrawBackgroundEventArgs)

Event Data

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

PropertyDescription
ContentBoundsGets the rectangle which encompasses the background area excluding margins.
ContextGets whether the item is to be drawn on the canvas, toolbox, in the print or export output or as the drag preview.
GraphicsReturns an object that provides painting facilities.
GraphicsCacheReturns an object that provides painting facilities.
PageMargin
PagesInfoGets the list of diagram pages.
PrintBoundsGets the rectangle which encompasses the total background area when printing the diagram.
PrintClientBoundsGets the rectangle which encompasses the background area excluding margins when printing the diagram.
PrintIndexGets the zero-based index of the page that is currently being rendered when printing the diagram.
TotalBoundsGets the rectangle which encompasses the total background area.
ViewportBoundsGets the rectangle which encompasses the viewport area.

Remarks

Use the CustomDrawBackground event to draw a custom Canvas background.

The following code snippet illustrates how to set an image as the diagram background:

csharp
private void DiagramControl_CustomDrawBackground(object sender, CustomDrawBackgroundEventArgs e) {
    if (e.Context != DiagramDrawingContext.Print)
        e.GraphicsCache.DrawImage(Image.FromFile("Images\\background.jpg"), e.TotalBounds);
}
vb
Private Sub DiagramControl_CustomDrawBackground(ByVal sender As Object, ByVal e As CustomDrawBackgroundEventArgs)
    If e.Context <> DiagramDrawingContext.Print Then
        e.GraphicsCache.DrawImage(Image.FromFile("Images\background.jpg"), e.TotalBounds)
    End If
End Sub

The following code snippet illustrates how to display text at the top center of the diagram:

csharp
private void DiagramControl_CustomDrawBackground(object sender, CustomDrawBackgroundEventArgs e) {
    // Specify the font settings.
    using (Font fontStyle = new Font("Segoe UI", 16, FontStyle.Regular, GraphicsUnit.Point)) {
        GraphicsPath path = new GraphicsPath();

        // Position the text bounds at the top of the diagram.
        RectangleF textRect = new RectangleF(0, 10, e.TotalBounds.Width, 30);

        // Position the text in the center of the bounds.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
        StringFormatInfo formatInfo = new StringFormatInfo(stringFormat);

        // Specify the rendering quality.
        e.GraphicsCache.SmoothingMode = SmoothingMode.AntiAlias;
        e.GraphicsCache.CompositingQuality = CompositingQuality.HighQuality;

        // Use the DrawPath and FillPath methods to render high quality antialiased text.
        path.AddString("Diagram Header",
            fontStyle.FontFamily,
            (int) fontStyle.Style,
            e.Graphics.DpiY * fontStyle.Size / 72f,
            textRect,
            stringFormat);
        e.GraphicsCache.DrawPath(new Pen(Color.Blue, 1), path);
        e.GraphicsCache.FillPath(Brushes.Blue, path);
    }
}
vb
Private Sub DiagramControl_CustomDrawBackground(ByVal sender As Object, ByVal e As CustomDrawBackgroundEventArgs)
    ' Specify the font settings.
    Using fontStyle As New Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, GraphicsUnit.Point)
        Dim path As New GraphicsPath()

        ' Position the text bounds at the top of the diagram.
        Dim textRect As New RectangleF(0, 10, e.TotalBounds.Width, 30)

        ' Position the text in the center of the bounds.
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center
        Dim formatInfo As New StringFormatInfo(stringFormat)

        ' Specify the rendering quality.
        e.GraphicsCache.SmoothingMode = SmoothingMode.AntiAlias
        e.GraphicsCache.CompositingQuality = CompositingQuality.HighQuality

        ' Use the DrawPath and FillPath methods to render high quality antialiased text.
        path.AddString("Diagram Header", fontStyle.FontFamily, CInt(fontStyle.Style), e.Graphics.DpiY * fontStyle.Size / 72F, textRect, stringFormat)
        e.GraphicsCache.DrawPath(New Pen(Color.Blue, 1), path)
        e.GraphicsCache.FillPath(Brushes.Blue, path)
    End Using
End Sub

Note

If the font’s Unit property is not set to GraphicsUnit.Pixel , the font size of the text drawn by the Graphics.DrawString method is scaled based on the DPI settings.

See Also

DiagramControl Class

DiagramControl Members

DevExpress.XtraDiagram Namespace