Back to Devexpress

RichEditControl.BeforePagePaint Event

windowsforms-devexpress-dot-xtrarichedit-dot-richeditcontrol-5a1f3e7f.md

latest11.9 KB
Original Source

RichEditControl.BeforePagePaint Event

Enables you to specify a custom PagePainter descendant to alter the way the layout elements are drawn.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.XtraRichEdit.v25.2.dll

NuGet Package : DevExpress.Win.RichEdit

Declaration

csharp
public event BeforePagePaintEventHandler BeforePagePaint
vb
Public Event BeforePagePaint As BeforePagePaintEventHandler

Event Data

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

PropertyDescription
CanvasProvides access to layout drawing surface.
CanvasOwnerTypeDetermines the type of device to which the document layout is rendered.
PageProvides access to the current page being rendered.
PainterGets or sets the painter object which implements methods for drawing layout elements.

Example

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

csharp
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);
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Layout
Imports DevExpress.XtraRichEdit.API.Native
'...

Private Sub RichEditControl1_BeforePagePaint(ByVal sender As Object, ByVal e As DevExpress.XtraRichEdit.BeforePagePaintEventArgs)
    If e.CanvasOwnerType = DevExpress.XtraRichEdit.API.Layout.CanvasOwnerType.Printer Then
        Return
    End If
    Dim style As DevExpress.XtraRichEdit.API.Native.CharacterStyle = richEditControl1.Document.CharacterStyles("Line Number")
    Dim customPagePainter As New MyPagePainter(richEditControl1, SystemColors.Info, style)
    customPagePainter.LineNumberPadding = 60
    e.Painter = customPagePainter
End Sub

Public Class MyPagePainter
    Inherits PagePainter

    Private richEditControl As RichEditControl

    Private previousColumnIndex As Integer = -1

    Private lineNumberFont As Font

    Public Sub New(ByVal richEdit As RichEditControl)
        MyBase.New()
        richEditControl = richEdit
    End Sub

    Public Sub New(ByVal richEdit As RichEditControl, ByVal backColor As Color, ByVal style As CharacterStyle)
        MyBase.New()
        richEditControl = richEdit
        NumberingHighlightColor = backColor
        NumberingFontName = style.FontName
        NumberingFontSize = If(style.FontSize, 10F)
        NumberingFontColor = If(style.ForeColor, Color.Black)
    End Sub

    Public Property NumberingFontName As String

    Public Property NumberingFontSize As Single

    Public Property NumberingFontColor As Color

    Public Property NumberingHighlightColor As Color

    Public Property LineNumberPadding As Integer

    Public Overrides Sub DrawPage(ByVal page As LayoutPage)
        lineNumberFont = New Font(NumberingFontName, NumberingFontSize, FontStyle.Regular)
        MyBase.DrawPage(page)
        lineNumberFont.Dispose()
    End Sub

    Public Overrides Sub DrawPageArea(ByVal pageArea As LayoutPageArea)
        Dim lineNumberBounds As Rectangle = New Rectangle(New Point(-LineNumberPadding, 0), New Size(LineNumberPadding, pageArea.Bounds.Height))
        Canvas.FillRectangle(New RichEditBrush(NumberingHighlightColor), lineNumberBounds)
        MyBase.DrawPageArea(pageArea)
        previousColumnIndex = -1
    End Sub

    Public Overrides Sub DrawColumn(ByVal column As LayoutColumn)
        Dim pageArea As LayoutPageArea = column.GetParentByType(Of LayoutPageArea)()
        If pageArea IsNot Nothing Then
            Dim leftBoundary As Integer = 0
            If previousColumnIndex >= 0 Then
                leftBoundary = pageArea.Columns(previousColumnIndex).Bounds.Right
            End If

            If column.LineNumbers.Count > 0 Then
                HighlightLineNumberingArea(column, leftBoundary)
            End If

            previousColumnIndex += 1
        End If

        MyBase.DrawColumn(column)
    End Sub

    Public Overrides Sub DrawLineNumberBox(ByVal lineNumberBox As LineNumberBox)
        Canvas.DrawString(lineNumberBox.Text, lineNumberFont, New RichEditBrush(NumberingFontColor), lineNumberBox.Bounds, richEditControl.LayoutUnit)
    End Sub

    Private Sub HighlightLineNumberingArea(ByVal column As LayoutColumn, ByVal leftBoundary As Integer)
        Dim page As LayoutPage = column.GetParentByType(Of LayoutPage)()
        Dim marginBounds As Rectangle = New Rectangle(New Point(leftBoundary, 0), New Size(column.Bounds.X - leftBoundary, page.Bounds.Height))
        Canvas.FillRectangle(New RichEditBrush(NumberingHighlightColor), marginBounds)
    End Sub
End Class

The following code snippets (auto-collected from DevExpress Examples) contain references to the BeforePagePaint event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-layout-api/CS/Form1.cs#L113

csharp
{
    richEditControl1.BeforePagePaint += richEditControl1_BeforePagePaint;
    layoutControlGroup2.Enabled = true;

winforms-richedit-enable-line-numbering-and-count-document-rows/CS/LineNumberingExample/Form1.cs#L64

csharp
if (barCheckLineNumberBackColoring.Checked)
    richEditControl1.BeforePagePaint += RichEditControl1_BeforePagePaint;
else

winforms-richedit-layout-api/VB/Form1.vb#L93

vb
If checkbtnCustomDraw.Checked Then
    AddHandler richEditControl1.BeforePagePaint, AddressOf richEditControl1_BeforePagePaint
    LayoutControlGroup2.Enabled = True

winforms-richedit-enable-line-numbering-and-count-document-rows/VB/LineNumberingExample/Form1.vb#L59

vb
If barCheckLineNumberBackColoring.Checked Then
    AddHandler richEditControl1.BeforePagePaint, AddressOf RichEditControl1_BeforePagePaint
Else

See Also

RichEditControl Class

RichEditControl Members

DevExpress.XtraRichEdit Namespace