Back to Devexpress

PagePainter Class

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-layout-65a71218.md

latest12.0 KB
Original Source

PagePainter Class

Base class to implement custom drawing for layout elements.

Namespace : DevExpress.XtraRichEdit.API.Layout

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public class PagePainter
vb
Public Class PagePainter

The following members return PagePainter objects:

Remarks

To implement custom drawing, subclass the PagePainter class, override the required methods and handle the BeforePagePaint event of the Rich Text Editor to specify a custom painter using the BeforePagePaintEventArgs.Painter property of event arguments.

Example

This code snippet illustrates the implementation of a PagePainter descendant. It draws color rectangles in place of the words and labels a floating picture with a text string drawn over it.

View Example

csharp
public class MyLayoutPainter : PagePainter
{
    public override void DrawPlainTextBox(PlainTextBox plainTextBox)
    {
        if (Form1.customDrawText == true)
        {
            Canvas.DrawRectangle(new RichEditPen(Color.FromArgb(141, 179, 226)), plainTextBox.Bounds);
        }
        else base.DrawPlainTextBox(plainTextBox);
    }
    public override void DrawFloatingPicture(LayoutFloatingPicture floatingPicture)
    {
        if (Form1.customDrawImage == true)
        {
            Rectangle bounds = floatingPicture.Bounds;
            Point startPoint = new Point(bounds.X + 10, bounds.Y + bounds.Height - 40);
            RichEditPen pEn = new RichEditPen(Color.Coral);
            pEn.Thickness = 3;
            pEn.DashStyle = RichEditDashStyle.Dash;
            Canvas.DrawEllipse(pEn, bounds);
            Canvas.DrawString("Approved", new Font("Courier New", 26), new RichEditBrush(Color.DarkMagenta), startPoint);
        }
        else
            base.DrawFloatingPicture(floatingPicture);
    }
    public override void DrawTableCell(LayoutTableCell tableCell)
    {
        if (Form1.customDrawTable == true)
        {
            Rectangle Tbounds = tableCell.Bounds;
            Rectangle tableRectangle = new Rectangle(new Point(Tbounds.X + Tbounds.Width / 2 - 10, Tbounds.Y + Tbounds.Height / 2 - 10), new Size(20, 20));
            Canvas.FillEllipse(new RichEditBrush(Color.MediumAquamarine), Canvas.ConvertToDrawingLayoutUnits(tableRectangle, DocumentLayoutUnit.Pixel));
            Canvas.DrawImage(OfficeImage.CreateImage(DevExpress.Images.ImageResourceCache.Default.GetImage("devav/people/employeeaward_16x16.png")), tableCell.Bounds, ImageSizeMode.Squeeze);
            base.DrawTableCell(tableCell);
        }
        else
            base.DrawTableCell(tableCell);
    }
    public override void DrawInlinePictureBox(InlinePictureBox inlinePictureBox)
    {
        if (Form1.customDrawPicture == true)
        {
            Rectangle Ebounds = inlinePictureBox.Bounds;
            RichEditPen pen = new RichEditPen(Color.Maroon, 2);
            pen.DashStyle = RichEditDashStyle.Dot;
            Canvas.DrawLine(pen, new Point(Ebounds.X, Ebounds.Y + Ebounds.Height), new Point(Ebounds.X + Ebounds.Width, Ebounds.Y));
            Canvas.DrawLine(pen, new Point(Ebounds.X, Ebounds.Y), new Point(Ebounds.X + Ebounds.Width, Ebounds.Y + Ebounds.Height));
            Rectangle inlineRect = new Rectangle(Ebounds.X, Ebounds.Y, Ebounds.Width, Ebounds.Height);
            Canvas.DrawRectangle(new RichEditPen(Color.Aquamarine, 4), inlineRect);
        }
        else
            base.DrawInlinePictureBox(inlinePictureBox);
    }
    public override void DrawTextBox(LayoutTextBox textBox)
    {
        if (Form1.customDrawTextBox == true)
        {
            Point[] StarPoints =
                {
            new Point(textBox.Bounds.X+textBox.Bounds.Width/2,textBox.Bounds.Y),
            new Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height),
            new Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height/2),
            new Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height/2),
            new Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height),
            new Point(textBox.Bounds.X+textBox.Bounds.Width/2,textBox.Bounds.Y)
                };
            Canvas.DrawLines(new RichEditPen(Color.HotPink, 3), StarPoints);
        }
        else
            base.DrawTextBox(textBox);
    }
    public override void DrawNumberingListWithSeparatorBox(NumberingListWithSeparatorBox numberingListWithSeparatorBox)
    {
        if (Form1.customDrawSeparator == true) Canvas.FillRectangle(new RichEditBrush(Color.DarkRed), numberingListWithSeparatorBox.Bounds);
        else base.DrawNumberingListWithSeparatorBox(numberingListWithSeparatorBox);
    }
    public override void DrawHeader(LayoutHeader header)
    {
        if (Form1.customDrawHeader == true)
        {

            Point p1 = new Point(0, 0);
            Point p2 = new Point(100, 100);
            Canvas.DrawLine(new RichEditPen(Color.Red, 5), p1, p2);
            Canvas.DrawString("Default Layout Unit", new Font("Comic Sans", 12), new RichEditBrush(Color.Red), new Point(0, 0));
            Point p3 = new Point(0, 100);
            Point p4 = new Point(100, 200);
            Canvas.DrawLine(new RichEditPen(Color.Blue, 5), p3, p4, DocumentLayoutUnit.Pixel);
            Canvas.DrawString("Layout Unit Specified", new Font("Comic Sans", 12), new RichEditBrush(Color.Blue), new Point(0, 100), DocumentLayoutUnit.Pixel);
        }
        else
            base.DrawHeader(header);
    }
    public override void DrawPage(LayoutPage page)
    {
        if (Form1.customDrawPage == true)
        {
            Rectangle inlineRect = new Rectangle(100, 100, 150, 200);
            Canvas.DrawRectangle(new RichEditPen(Color.Aquamarine, Canvas.ConvertToDrawingLayoutUnits(4, DocumentLayoutUnit.Pixel)), Canvas.ConvertToDrawingLayoutUnits(inlineRect, DocumentLayoutUnit.Pixel));

        }
        base.DrawPage(page);
    }
vb
Public Class MyLayoutPainter
    Inherits PagePainter
    Public Overrides Sub DrawPlainTextBox(ByVal plainTextBox As PlainTextBox)
        If Form1.customDrawText = True Then
            Canvas.DrawRectangle(New RichEditPen(Color.FromArgb(141, 179, 226)), plainTextBox.Bounds)
        Else
            MyBase.DrawPlainTextBox(plainTextBox)
        End If
    End Sub
    Public Overrides Sub DrawFloatingPicture(ByVal floatingPicture As LayoutFloatingPicture)
        If Form1.customDrawImage = True Then
            Dim bounds As Rectangle = floatingPicture.Bounds
            Dim startPoint As New Point(bounds.X + 10, bounds.Y + bounds.Height - 40)
            Dim pEn As New RichEditPen(Color.Coral)
            pEn.Thickness = 3
            pEn.DashStyle = RichEditDashStyle.Dash
            Canvas.DrawEllipse(pEn, bounds)
            Canvas.DrawString("Approved", New Font("Courier New", 26), New RichEditBrush(Color.DarkMagenta), startPoint)
        Else
            MyBase.DrawFloatingPicture(floatingPicture)
        End If
    End Sub
    Public Overrides Sub DrawTableCell(ByVal tableCell As LayoutTableCell)
        If Form1.customDrawTable = True Then
            Dim Tbounds As Rectangle = tableCell.Bounds
            Dim tableRectangle As New Rectangle(New Point(Tbounds.X + Tbounds.Width \ 2 - 10, Tbounds.Y + Tbounds.Height \ 2 - 10), New Size(20, 20))
            Canvas.FillEllipse(New RichEditBrush(Color.MediumAquamarine), Canvas.ConvertToDrawingLayoutUnits(tableRectangle, DocumentLayoutUnit.Pixel))
            Canvas.DrawImage(OfficeImage.CreateImage(DevExpress.Images.ImageResourceCache.Default.GetImage("devav/people/employeeaward_16x16.png")), tableCell.Bounds, ImageSizeMode.Squeeze)
            MyBase.DrawTableCell(tableCell)
        Else
            MyBase.DrawTableCell(tableCell)
        End If
    End Sub
    Public Overrides Sub DrawInlinePictureBox(ByVal inlinePictureBox As InlinePictureBox)
        If Form1.customDrawPicture = True Then
            Dim Ebounds As Rectangle = inlinePictureBox.Bounds
            Dim pen As New RichEditPen(Color.Maroon, 2)
            pen.DashStyle = RichEditDashStyle.Dot
            Canvas.DrawLine(pen, New Point(Ebounds.X, Ebounds.Y + Ebounds.Height), New Point(Ebounds.X + Ebounds.Width, Ebounds.Y))
            Canvas.DrawLine(pen, New Point(Ebounds.X, Ebounds.Y), New Point(Ebounds.X + Ebounds.Width, Ebounds.Y + Ebounds.Height))
            Dim inlineRect As New Rectangle(Ebounds.X, Ebounds.Y, Ebounds.Width, Ebounds.Height)
            Canvas.DrawRectangle(New RichEditPen(Color.Aquamarine, 4), inlineRect)
        Else
            MyBase.DrawInlinePictureBox(inlinePictureBox)
        End If
    End Sub
    Public Overrides Sub DrawTextBox(ByVal textBox As LayoutTextBox)
        If Form1.customDrawTextBox = True Then
            Dim StarPoints() As Point = { _
                New Point(textBox.Bounds.X+textBox.Bounds.Width\2,textBox.Bounds.Y), _
                New Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height), _
                New Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height\2), _
                New Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height\2), _
                New Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height), _
                New Point(textBox.Bounds.X+textBox.Bounds.Width\2,textBox.Bounds.Y) _
            }
            Canvas.DrawLines(New RichEditPen(Color.HotPink, 3), StarPoints)
        Else
            MyBase.DrawTextBox(textBox)
        End If
    End Sub
    Public Overrides Sub DrawNumberingListWithSeparatorBox(ByVal numberingListWithSeparatorBox As NumberingListWithSeparatorBox)
        If Form1.customDrawSeparator = True Then
            Canvas.FillRectangle(New RichEditBrush(Color.DarkRed), numberingListWithSeparatorBox.Bounds)
        Else
            MyBase.DrawNumberingListWithSeparatorBox(numberingListWithSeparatorBox)
        End If
    End Sub
    Public Overrides Sub DrawHeader(ByVal header As LayoutHeader)
        If Form1.customDrawHeader = True Then

            Dim p1 As New Point(0, 0)
            Dim p2 As New Point(100, 100)
            Canvas.DrawLine(New RichEditPen(Color.Red, 5), p1, p2)
            Canvas.DrawString("Default Layout Unit", New Font("Comic Sans", 12), New RichEditBrush(Color.Red), New Point(0, 0))
            Dim p3 As New Point(0, 100)
            Dim p4 As New Point(100, 200)
            Canvas.DrawLine(New RichEditPen(Color.Blue, 5), p3, p4, DocumentLayoutUnit.Pixel)
            Canvas.DrawString("Layout Unit Specified", New Font("Comic Sans", 12), New RichEditBrush(Color.Blue), New Point(0, 100), DocumentLayoutUnit.Pixel)
        Else
            MyBase.DrawHeader(header)
        End If
    End Sub
    Public Overrides Sub DrawPage(ByVal page As LayoutPage)
        If Form1.customDrawPage = True Then
            Dim inlineRect As New Rectangle(100, 100, 150, 200)
            Canvas.DrawRectangle(New RichEditPen(Color.Aquamarine, Canvas.ConvertToDrawingLayoutUnits(4, DocumentLayoutUnit.Pixel)), Canvas.ConvertToDrawingLayoutUnits(inlineRect, DocumentLayoutUnit.Pixel))

        End If
        MyBase.DrawPage(page)
    End Sub

Inheritance

Object PagePainter

See Also

PagePainter Members

DevExpress.XtraRichEdit.API.Layout Namespace