Back to Devexpress

RichEditControl.CustomMarkDraw Event

wpf-devexpress-dot-xpf-dot-richedit-dot-richeditcontrol-691bcab4.md

latest6.9 KB
Original Source

RichEditControl.CustomMarkDraw Event

Fires before a custom mark is painted, and enables you to visualize the custom mark as required.

Namespace : DevExpress.Xpf.RichEdit

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

NuGet Package : DevExpress.Wpf.RichEdit

Declaration

csharp
public event CustomMarkDrawEventHandler CustomMarkDraw
vb
Public Event CustomMarkDraw As CustomMarkDrawEventHandler

Event Data

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

PropertyDescription
VisualInfoCollectionProvides access to information required to visualize custom marks.

Remarks

Use the CustomMarkCollection.Create method to create a mark, add it to the SubDocument.CustomMarks collection and visualize it by handling the CustomMarkDraw event.

Example

vb
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit.Layout.Export
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes

Namespace CustomMarkDraw
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
            richEditControl1.LoadDocument("Test.docx")
        End Sub

        Private Sub barButtonItem1_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
            Dim doc As Document = richEditControl1.Document
            ' Create a custom mark at the caret position and attach arbitrary data to the mark.
            ' In this example the data specifies the color that will be used to draw the mark.
            Dim m As CustomMark = doc.CustomMarks.Create(doc.Selection.Start, Brushes.Orange)
        End Sub

        Private Sub richEditControl1_CustomMarkDraw(ByVal sender As Object, ByVal e As DevExpress.Xpf.RichEdit.CustomMarkDrawEventArgs)
            If Not richEditControl1.IsLoaded Then
                Return
            End If
            Dim surface As Canvas = TryCast(richEditControl1.Template.FindName("Surface", richEditControl1), Canvas)
            If Not surface.IsLoaded Then
                Return
            End If
            Dim transform As GeneralTransform = surface.TransformToVisual(richEditControl1)

            Dim clip_Renamed As New RectangleGeometry(New Rect(transform.Transform(New Point(0, 0)), surface.RenderSize))
            richEditCanvas.Children.Clear()
            richEditCanvas.Clip = clip_Renamed
            For Each info As CustomMarkVisualInfo In e.VisualInfoCollection
                Dim doc As Document = richEditControl1.Document
                Dim mark As CustomMark = doc.CustomMarks.GetByVisualInfo(info)
                ' Get a brush associated with the mark.
                Dim curBrush As Brush = TryCast(info.UserData, Brush)
                ' Use a different brush to draw custom marks located above the caret.
                If mark.Position < doc.Selection.Start Then
                    curBrush = Brushes.Green
                End If
                Dim rect As New Rectangle()
                rect.Width = 2
                rect.Height = info.Bounds.Height
                rect.Fill = curBrush
                Canvas.SetLeft(rect, info.Bounds.X)
                Canvas.SetTop(rect, info.Bounds.Y)
                richEditCanvas.Children.Add(rect)
            Next info
        End Sub
    End Class
End Namespace
csharp
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Layout.Export;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace CustomMarkDraw {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            richEditControl1.LoadDocument("Test.docx");           
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Document doc = richEditControl1.Document;
            // Create a custom mark at the caret position and attach arbitrary data to the mark.
            // In this example the data specifies the color that will be used to draw the mark.
            CustomMark m = doc.CustomMarks.Create(doc.Selection.Start, Brushes.Orange);
        }

        private void richEditControl1_CustomMarkDraw(object sender, DevExpress.Xpf.RichEdit.CustomMarkDrawEventArgs e) {
            if (!richEditControl1.IsLoaded)
                return;
            Canvas surface = richEditControl1.Template.FindName("Surface", richEditControl1) as Canvas;
            if (!surface.IsLoaded)
                return;
            GeneralTransform transform = surface.TransformToVisual(richEditControl1);
            RectangleGeometry clip = new RectangleGeometry(new Rect(transform.Transform(new Point(0, 0)), surface.RenderSize));
            richEditCanvas.Children.Clear();
            richEditCanvas.Clip = clip;
            foreach (CustomMarkVisualInfo info in e.VisualInfoCollection)
            {
                Document doc = richEditControl1.Document;
                CustomMark mark = doc.CustomMarks.GetByVisualInfo(info);
                // Get a brush associated with the mark.
                Brush curBrush = info.UserData as Brush;
                // Use a different brush to draw custom marks located above the caret.
                if (mark.Position < doc.Selection.Start) curBrush = Brushes.Green;
                Rectangle rect = new Rectangle();
                rect.Width = 2;
                rect.Height = info.Bounds.Height;
                rect.Fill = curBrush;
                Canvas.SetLeft(rect, info.Bounds.X);
                Canvas.SetTop(rect, info.Bounds.Y);
                richEditCanvas.Children.Add(rect);
            }
        }
    }
}

See Also

CustomMarks

RichEditControl Class

RichEditControl Members

DevExpress.Xpf.RichEdit Namespace