Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我们可以在WPF的RichTextBox中以编程方式搜索突出显示的文本吗_C#_Wpf_Richtextbox_Rtf - Fatal编程技术网

C# 我们可以在WPF的RichTextBox中以编程方式搜索突出显示的文本吗

C# 我们可以在WPF的RichTextBox中以编程方式搜索突出显示的文本吗,c#,wpf,richtextbox,rtf,C#,Wpf,Richtextbox,Rtf,如下图所示,使用Microsoft WORD VBA,我们可以搜索MS WORD文档中所有突出显示的文本。在遗留系统中使用C#也可以实现同样的效果问题:我们如何以编程方式获取WPF RichTextBox中所有突出显示的文本的文本范围 WPF RichTextBox显示: string sRTF = ""; TextRange tr = new TextRange(rtbTest.Document.ContentStart, rtbTest.Document.ContentE

如下图所示,使用Microsoft WORD VBA,我们可以搜索
MS WORD
文档中所有突出显示的文本。在遗留系统中使用C#也可以实现同样的效果问题:我们如何以编程方式获取
WPF RichTextBox
中所有突出显示的文本的文本范围

WPF RichTextBox显示

string sRTF = "";
TextRange tr = new TextRange(rtbTest.Document.ContentStart, rtbTest.Document.ContentEnd);

using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    sRTF = ASCIIEncoding.Default.GetString(ms.ToArray());
}

Debug.Write(sRTF);

获取rtf的代码:

string sRTF = "";
TextRange tr = new TextRange(rtbTest.Document.ContentStart, rtbTest.Document.ContentEnd);

using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    sRTF = ASCIIEncoding.Default.GetString(ms.ToArray());
}

Debug.Write(sRTF);
RTF输出

string sRTF = "";
TextRange tr = new TextRange(rtbTest.Document.ContentStart, rtbTest.Document.ContentEnd);

using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    sRTF = ASCIIEncoding.Default.GetString(ms.ToArray());
}

Debug.Write(sRTF);
在下面的输出中,我们可以看到突出显示的文本
test
的rtf是
{\lang9\highlight2\ltrch test}
。我们如何以编程方式在此处获取突出显示的文本(即
test
)。这只是一个例子,我的意思是以编程方式获取所有突出显示的文本

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Calibri;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red255\green255\blue0;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\fs22\f2 {\lang9\ltrch This is a }{\lang9\highlight2\ltrch test}{\lang9\ltrch  for a WPF RichTextBox}\li0\ri0\sa200\sb0\fi0\ql\par}
{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
我们是否可以在WPF RichTextBox中以编程方式实现相同的功能,就像(例如)通过下面所示的VBA宏或通过传统VSTO加载项中的c#在WORD文档中那样:

Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute

为了理解什么是算法可以用来分析
流程文档的内容
阅读文章的最佳方式,或者,例如,阅读C#中MacDonald M.-Pro WPF 4.5一书中的“文档”一章。.NET 4.5中的Windows演示基础(专家声音在.NET)中,2012 < <强>

因此,
FlowDocument
包含
BlockCollection块
属性。这是整个
FlowDocument
内容的顶级块

下面的代码使用此属性递归地解析和分析FlowDocument中的所有元素,包括搜索具有指定背景颜色的文本片段

出于测试目的,应用程序窗口包含颜色
组合框
,该组合框允许选择某种颜色并使用“设置颜色”按钮绘制选定的文本片段。“搜索颜色文本”按钮开始扫描文档,查找指定颜色的文本,并使用创建的
TextRange
列表将文本重新绘制为粉红色

MainWindow.xaml


? 
MainWindow.xaml.cs

使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Media;
名称空间WpfApp17
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
ComboColor.ItemsSource=typeof(Colors).GetProperties();
ComboColor.SelectedValue=“棕色”;
}
私有无效搜索\u单击(对象发送者,路由目标)
{
解析(rtb);
}
公共无效解析(RichTextBox rtb)
{
//获取所选颜色
var c=System.Drawing.Color.FromName(ComboColor.SelectedValue.ToString());
var parser=new RtfDocumentParser();
//使用选定颜色初始化
Init(Color.FromArgb(c.A,c.R,c.G,c.B));
//加工
解析(rtb);
//找到的颜色范围为粉红色
foreach(parser.TextRanges中的var tr)
{
tr.ApplyPropertyValue(TextElement.BackgroundProperty,新的SolidColorBrush(Colors.Pink));
}
}
私有void SetColor\u单击(对象发送者,路由目标e)
{
var选择=rtb选择;
如果(!selection.IsEmpty)
{
var c=System.Drawing.Color.FromName(ComboColor.SelectedValue.ToString());
selection.ApplyPropertyValue(TextElement.BackgroundProperty,新的SolidColorBrush(Color.FromArgb(c.A,c.R,c.G,c.B));
}
}
}
}
RtfDocumentParser.cs

使用系统;
使用System.Collections.Generic;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Media;
名称空间WpfApp17
{
公共类RtfDocumentParser
{
#区域公共财产
公共颜色搜索颜色{get;private set;}
公共IList TextRanges{get;private set;}
#端区
#区域私有财产
私有文本指针开始{get;set;}
私有文本指针结束{get;set;}
#端区
#区域导体
公共RtfDocumentParser()
{
Init(颜色。黄色);
}
#端区
#区域公共方法
公共void Init(颜色)
{
搜索颜色=颜色;
TextRanges=新列表();
}
公共无效分析(RichTextBox rtb)
{
ParseBlockCollection(rtb.Document.Blocks);
近距离();
}
#端区
#区域私有方法
私有无效ParseBlockCollection(BlockCollection块)
{
foreach(块中的var块)
{
近距离();
如果(块是段落段落){ParseInlineCollection(段落Inlines);}
else if(块是列表)
{
foreach(list.ListItems中的var litem)
{
近距离();
TextRange范围=新的TextRange(litem.ElementStart,litem.ElementEnd);
P