How to: Create a Custom Brick Inherited From the Brick Class
- 3 minutes to read
The XtraPrinting Library allows you to create custom bricks and use them in your document. This example demonstrates how to create a Brick class descendant.
Create the Brick Class Descendant
This example demonstrates how to create a custom Brick class descendant. The custom EllipseBrick class draws an elliptical figure filled with a linear color gradient.
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.Drawing;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.BrickExporters;
// ...
[BrickExporter(typeof(EllipseBrickExporter))]
public class EllipseBrick : Brick {
// Set gradient colors for inner and outer ellipse regions.
public Color InnerColor = Color.Transparent;
public Color OuterColor = Color.Peru;
public EllipseBrick() {
}
public EllipseBrick(Color InnerColor, Color OuterColor) {
this.InnerColor = InnerColor;
this.OuterColor = OuterColor;
}
}
public class EllipseBrickExporter : BrickExporter {
EllipseBrick EllipseBrick { get { return (EllipseBrick)Brick; } }
// Fills an ellipse with a linear color gradient.
public override void Draw(IGraphics gr, RectangleF rect) {
using(DXLinearGradientBrush brush = new DXLinearGradientBrush(rect, EllipseBrick.OuterColor, EllipseBrick.InnerColor)) {
DXColorBlend colorBlend = new DXColorBlend();
colorBlend.Positions = new float[] { 0.0f, 0.5f, 1.0f };
colorBlend.Colors = new Color[] { EllipseBrick.OuterColor, EllipseBrick.InnerColor, EllipseBrick.OuterColor };
brush.InterpolationColors = colorBlend;
gr.FillEllipse(brush, rect);
}
}
}
Use the EllipseBrick
When a class is implemented, you can use it in your code. The code below demonstrates the EllipseBrick drawn within a specified rectangle.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
// ...
Brick brick = new EllipseBrick(Color.LightGreen, Color.Blue);
printingSystem1.Begin();
IBrickGraphics graph = printingSystem1.Graph;
// Specify the page area to draw a brick.
printingSystem1.Graph.Modifier = BrickModifier.Detail;
// Add the brick with specified dimensions to the document.
graph.DrawBrick(brick, new RectangleF(0, 0, 150, 100));
printingSystem1.End();
printingSystem1.PreviewFormEx.Show();
The EllipseBrick is displayed in the image below: