Skip to main content

DiagramControl.CustomDrawBackground Event

Occurs before the Canvas background is rendered.

Namespace: DevExpress.XtraDiagram

Assembly: DevExpress.XtraDiagram.v24.1.dll

NuGet Package: DevExpress.Win.Diagram

Declaration

[DiagramCategory(DiagramCategory.DiagramPaint)]
public event EventHandler<CustomDrawBackgroundEventArgs> CustomDrawBackground

Event Data

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

Property Description
ContentBounds Gets the rectangle which encompasses the background area excluding margins.
Context Gets whether the item is to be drawn on the canvas, toolbox, in the print or export output or as the drag preview.
Graphics Returns an object that provides painting facilities.
GraphicsCache Returns an object that provides painting facilities.
PageMargin
PagesInfo Gets the list of diagram pages.
PrintBounds Gets the rectangle which encompasses the total background area when printing the diagram.
PrintClientBounds Gets the rectangle which encompasses the background area excluding margins when printing the diagram.
PrintIndex Gets the zero-based index of the page that is currently being rendered when printing the diagram.
TotalBounds Gets the rectangle which encompasses the total background area.
ViewportBounds Gets 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:

private void DiagramControl_CustomDrawBackground(object sender, CustomDrawBackgroundEventArgs e) {
    if (e.Context != DiagramDrawingContext.Print)
        e.GraphicsCache.DrawImage(Image.FromFile("Images\\background.jpg"), e.TotalBounds);
}

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

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);
    }
}

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