Skip to main content

How to: Set Background Color for the Line Number Column

  • 4 minutes to read

This code sample illustrates how to handle the RichEditControl.BeforePagePaint event to draw a color rectangle on the page canvas. The drawn rectangle is the line number column background.

To perform a custom draw, create a custom PagePainter descendant which implements the methods required to draw the column background and the line numbers themselves. Specify an instance of the custom painter using the BeforePagePaintEventArgs.Painter property. To check whether the page is rendered for printing, use the BeforePagePaintEventArgs.CanvasOwnerType property.

The MyPagePainter class uses the Line Number document style to draw line numbers. This is done to make them look like the line numbers drawn in the usual way as described in the Line Numbering topic.

View Example

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Layout;
using DevExpress.XtraRichEdit.API.Native;
//...

private void RichEditControl1_BeforePagePaint(object sender, DevExpress.XtraRichEdit.BeforePagePaintEventArgs e) {
    if (e.CanvasOwnerType == DevExpress.XtraRichEdit.API.Layout.CanvasOwnerType.Printer) {
        return;
    }
    DevExpress.XtraRichEdit.API.Native.CharacterStyle style = richEditControl1.Document.CharacterStyles["Line Number"];
    MyPagePainter customPagePainter = new MyPagePainter(richEditControl1, SystemColors.Info, style);
    customPagePainter.LineNumberPadding = 60;
    e.Painter = customPagePainter;
}

public class MyPagePainter : PagePainter {
    RichEditControl richEditControl;
    int previousColumnIndex = -1;
    Font lineNumberFont;

    public MyPagePainter(RichEditControl richEdit)
        : base() {
        richEditControl = richEdit;
    }

    public MyPagePainter(RichEditControl richEdit, Color backColor, CharacterStyle style)
        : base() {
        richEditControl = richEdit;
        NumberingHighlightColor = backColor;
        NumberingFontName = style.FontName;
        NumberingFontSize = style.FontSize ?? 10F;
        NumberingFontColor = style.ForeColor ?? Color.Black;
    }

    public string NumberingFontName { get; set; }
    public float NumberingFontSize { get; set; }
    public Color NumberingFontColor { get; set; }
    public Color NumberingHighlightColor { get; set; }
    public int LineNumberPadding { get; set; }

    public override void DrawPage(LayoutPage page) {
        lineNumberFont = new Font(NumberingFontName, NumberingFontSize, FontStyle.Regular);
        base.DrawPage(page);
        lineNumberFont.Dispose();
    }

    public override void DrawPageArea(LayoutPageArea pageArea) {
        Rectangle lineNumberBounds = new Rectangle(new Point(-LineNumberPadding, 0), new Size(LineNumberPadding, pageArea.Bounds.Height));
        Canvas.FillRectangle(new RichEditBrush(NumberingHighlightColor), lineNumberBounds);
        base.DrawPageArea(pageArea);
        previousColumnIndex = -1;
    }

    public override void DrawColumn(LayoutColumn column) {
        LayoutPageArea pageArea = column.GetParentByType<LayoutPageArea>();
        if (pageArea != null) {
            int leftBoundary = 0;
            if (previousColumnIndex >= 0)
            {
                leftBoundary = pageArea.Columns[previousColumnIndex].Bounds.Right;
            }
            if (column.LineNumbers.Count > 0)
            {
                HighlightLineNumberingArea(column, leftBoundary);
            }
            previousColumnIndex++;
        }
        base.DrawColumn(column);
    }

    public override void DrawLineNumberBox(LineNumberBox lineNumberBox) {
        Canvas.DrawString(lineNumberBox.Text, lineNumberFont, new RichEditBrush(NumberingFontColor), lineNumberBox.Bounds, this.richEditControl.LayoutUnit);
    }

    void HighlightLineNumberingArea(LayoutColumn column, int leftBoundary) {
        LayoutPage page = column.GetParentByType<LayoutPage>();
        Rectangle marginBounds = new Rectangle(new Point(leftBoundary, 0), new Size(column.Bounds.X - leftBoundary, page.Bounds.Height));
        Canvas.FillRectangle(new RichEditBrush(NumberingHighlightColor), marginBounds);
    }
}