简体   繁体   English

在WPF中的RTF文本框中突出显示关键字

[英]Highlighting keywords in a richtextbox in WPF

I'm making a program which needs to look through a paragraph of text and find how many times a certain keyword/keywords appear. 我正在制作一个程序,该程序需要浏览一段文本并查找某个关键字的出现次数。 It also has to highlight each of these key words in the text. 它还必须突出显示文本中的每个关键词。

I have managed to make he interface and it can now track how many times the word appears but I am really stuck for how to highlight where the keywords appear. 我设法使他成为界面,它现在可以跟踪单词出现的次数,但是我真的对如何突出显示关键字出现的位置感到困惑。 I will post my code below, any help is greatly appreciated on how to search for and highlight text inside a richtextbox. 我将在下面发布我的代码,对于如何在Richtextbox中搜索和突出显示文本的任何帮助,我们将不胜感激。 Since this is in WPF the obvious richtextbox.find() is not avaliable for use. 由于这是在WPF中,因此显然无法使用richtextbox.find()。

class TextAnalyser
{
    public int FindNumberOfOccurances(List<string> keywords, string email)
    {
        int occurances = 0;
        foreach (string keyword in keywords)
        {
            occurances += email.ToUpper().Split(new string[] { keyword.ToUpper() }, StringSplitOptions.None).Count() - 1; 
        }
        return occurances;
    }

    public void TurnTextRed(List<string> keywords, string email, RichTextBox TextBox)
    {
        foreach(string keyword in keywords)
        {
        }
    }

    public List<string> ConvertTextToList(string text)
    {
        char[] splitChars = {};
        string[] ArrayText = text.Split( splitChars, StringSplitOptions.RemoveEmptyEntries);
        return ArrayText.ToList<string>();
    }

    public string GetStringFromTextBox(RichTextBox TextBox)
    {
        var textRange = new TextRange(
            TextBox.Document.ContentStart,
            TextBox.Document.ContentEnd
        );
        return textRange.Text;
    }
}

And here is my Main Window 这是我的主窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AnalyseButton_Click(object sender, RoutedEventArgs e)
    {
        var textTool = new TextAnalyser();
        var keyWords = textTool.ConvertTextToList(textTool.GetStringFromTextBox(WordTextBox).Trim());
        var email = textTool.GetStringFromTextBox(EmailTextBox).Trim();
        int usesOfWord = textTool.FindNumberOfOccurances(keyWords, email);
        Occurances.Text = usesOfWord.ToString();
    }
}

Here is the method is used to get all of word in richtextbox's document. 这是用于获取richtextbox文档中所有单词的方法。

 public static IEnumerable<TextRange> GetAllWordRanges(FlowDocument document)
     {
         string pattern = @"[^\W\d](\w|[-']{1,2}(?=\w))*";
         TextPointer pointer = document.ContentStart;
         while (pointer != null)
         {
             if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
             {
                 string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
                 MatchCollection matches = Regex.Matches(textRun, pattern);
                 foreach (Match match in matches)
                 {
                     int startIndex = match.Index;
                     int length = match.Length;
                     TextPointer start = pointer.GetPositionAtOffset(startIndex);
                     TextPointer end = start.GetPositionAtOffset(length);
                     yield return new TextRange(start, end);
                 }
             }

             pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
         }
     }

You can change the pattern which is used to split words. 您可以更改用于拆分单词的模式。

At last, easy to highlight your words. 最后,轻松突出您的文字。

  IEnumerable<TextRange> wordRanges = GetAllWordRanges(RichTextBox.Document);
        foreach (TextRange wordRange in wordRanges)
        {
            if (wordRange.Text == "keyword")
            {
                wordRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            }
        }

Ran across a need for this and couldn't find any suitable solutions. 急需解决此问题,找不到任何合适的解决方案。 (Using a TextBox for binding, highlighting on the fly, multiple hits and colors, etc.) This can obviously be extended to suit your needs. (使用TextBox进行绑定,即时突出显示,多个匹配和颜色等。)显然,可以扩展此扩展以满足您的需求。 This references a couple of extension methods that add/remove adorners of the specified Type T from the UIElement's adorner layer. 这引用了一些扩展方法,这些方法从UIElement的装饰层添加/删除指定Type T的装饰。

 public class HighlightRule
{
    public SolidColorBrush Brush { get; set; }
    public string MatchText { get; set; }
    public HighlightRule(SolidColorBrush solidColorBrush, string matchText)
    {
        Brush = solidColorBrush;
        MatchText = matchText;
    }
    public HighlightRule(Color color, string matchText)
    {
        Brush = new SolidColorBrush(color);
        MatchText = matchText;
    }
    public HighlightRule()
    {
        MatchText = null;
        Brush = Brushes.Black;
    }
}
public class HighlightTextBox : TextBox
{
    public List<HighlightRule> HighlightRules
    {
        get { return ( List<HighlightRule>)GetValue(HighlightRulesProperty); }
        set { SetValue(HighlightRulesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for HighlightRules.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HighlightRulesProperty =
        DependencyProperty.Register("HighlightRules", typeof(List<HighlightRule>), typeof(HighlightTextBox), new FrameworkPropertyMetadata(new List<HighlightRule>(), new PropertyChangedCallback(HighlightRulesChanged)));

    private static void HighlightRulesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        HighlightTextBox tb = (HighlightTextBox)sender;
        tb.ApplyHighlights();
    }

    public HighlightTextBox() : base()
    {
        this.Loaded += HighlightTextBox_Loaded;
    }
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e);
        ApplyHighlights();
    }
    private void HighlightTextBox_Loaded(object sender, RoutedEventArgs e)
    {
        ApplyHighlights();
    }

    static HighlightTextBox()
    {
    }
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    }

    public void ApplyHighlights()
    {

        this.TryRemoveAdorner<GenericAdorner>();
        foreach(HighlightRule rule in HighlightRules)
        {
            if (!string.IsNullOrEmpty(rule.MatchText) && !string.IsNullOrEmpty(Text) &&
                Text.ToLower().Contains(rule.MatchText.ToLower()))
            {
                if (base.ActualHeight != 0 && base.ActualWidth != 0)
                {
                    int indexOf = 0;
                    do
                    {
                        indexOf = Text.IndexOf(rule.MatchText, indexOf);
                        if (indexOf == -1) break;
                        Rect rect = base.GetRectFromCharacterIndex(indexOf);
                        Rect backRect = base.GetRectFromCharacterIndex(indexOf + rule.MatchText.Length - 1, true);
                        this.TryAddAdorner<GenericAdorner>(new GenericAdorner(this, new Rectangle()
                        { Height = rect.Height, Width = backRect.X - rect.X, Fill = rule.Brush, Opacity = 0.5 }));
                        indexOf++;
                    } while (true);

                }
            }
        }

    }

}

GenericAdorner / Helper Methods GenericAdorner /辅助方法

 public class GenericAdorner : Adorner
{
    private readonly UIElement adorner;
    private readonly Point point;
    public GenericAdorner(UIElement targetElement, UIElement adorner, Point point) : base(targetElement)
    {
        this.adorner = adorner;
        if (adorner != null)
        {
            AddVisualChild(adorner);
        }
        this.point = point;
    }
    protected override int VisualChildrenCount
    {
        get { return adorner == null ? 0 : 1; }
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        if (adorner != null)
        {
            adorner.Arrange(new Rect(point, adorner.DesiredSize));
        }
        return finalSize;
    }
    protected override Visual GetVisualChild(int index)
    {
        if (index == 0 && adorner != null)
        {
            return adorner;
        }
        return base.GetVisualChild(index);
    }
}
 public static void TryRemoveAdorner<T>(this UIElement element)
        where T:Adorner
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
        if (layer != null)
            layer.RemoveAdorners<T>(element);
    }
    public static void RemoveAdorners<T>(this AdornerLayer layer, UIElement element)
        where T : Adorner
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return;
        for (int i = adorners.Length -1; i >= 0; i--)
        {
            if (adorners[i] is T)
                layer.Remove(adorners[i]);
        }
    }
    public static void TryAddAdorner<T>(this UIElement element, Adorner adorner)
        where T: Adorner
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);

        if (layer != null)
            try
            {
                layer.Add(adorner);
            }
            catch (Exception) { }
    }
    public static bool HasAdorner<T>(this AdornerLayer layer, UIElement element) where T : Adorner
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return false;
        for (int i = adorners.Length - 1; i >= 0; i--)
        {
            if (adorners[i] is T)
                return true;
        }
        return false;
    }
    public static void RemoveAdorners(this AdornerLayer layer, UIElement element)
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return;
        foreach (Adorner remove in adorners)
            layer.Remove(remove);
    }

The XAML XAML

<local:HighlightTextBox FontFamily="Calibri" Foreground="Blue" FontSize="12" Text="Hello you!! And also hello to you!" TextWrapping="Wrap" Margin="5,3,0,0">
  <local:HighlightTextBox.HighlightRules>
         <local:HighlightRule Brush="Red" MatchText="you"/>
         <local:HighlightRule Brush="Blue" MatchText="also"/>
         </local:HighlightTextBox.HighlightRules>
</local:HighlightTextBox>

Appearance 出现 突出示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM