windowsforms-118971-controls-and-libraries-rich-text-editor-examples-layout-how-to-set-background-color-for-the-line-number-column.md
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.
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);
}
}
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