none
RichTextBox,键盘输入文字时,改变一些文字变色,要怎么弄? RRS feed

  • 问题

  • 想要制作一个关键字或关键字变色的功能,我的思路是替换FlowDocument里的关键字为关键字自身加上run标签来设置字体颜色,c#代码如下,

            private string a = "<FlowDocument PagePadding=\"5,0,5,0\" AllowDrop=\"True\" " +
                "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                "<Paragraph>${text}</Paragraph></FlowDocument>";
            string FontBegin = "<Run Foreground = \"blue\" >";
            string fontEnd = "</Run> ";
            public void setColorsFont(String text)
            {
                String[] replaceStr = { "update", "set" }; //关键字组
                string temp = text;
                foreach (var i in replaceStr)
                {
                    String r = FontBegin + i + fontEnd;
                    temp = Regex.Replace(temp, i, r, RegexOptions.IgnoreCase);
                }
                string wa = a.Replace("${text}", temp);
                System.IO.StringReader sr = new System.IO.StringReader(wa);
                System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
                textView.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);
            }
       RichTextBox textView = sender as RichTextBox;
                TextRange a = new TextRange(textView.Document.ContentStart, textView.Document.ContentEnd);
                _text = a.Text;
                setColorsFont(a.Text);

    KeyDown事件不行,TextChanged事件里调用setColorsFont方法运行时调用,会出现的异常信息,如下:

    托管调试助手 "FatalExecutionEngineError":“运行时遇到了错误。此错误的地址为 0x5e7f610f,在线程 0x1440 上。错误代码为 0x80131623。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。”

    如何在键盘输入时一边setColorsFont方法变色?还有copy到RichTextBox,也触发事件调用setColorsFont方法替换关键字颜色?

    除了这种方式的关键字颜色变化的思路外,还有其他的方式吗

    2019年8月22日 18:50

答案

  • Hi   便携式家园, 

    上面提供的一种思路,我觉得主要的是,你需要查找出你需要操作的字符TextRange,然后这这上面进行字体格式的调整。

    WPF:在TextBlock和RichTextBox中根据坐标位置设置TextRange属性


    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月26日 3:26
    版主

全部回复

  • Hi   便携式家园,

    >>想要制作一个关键字或关键字变色的功能,我的思路是替换FlowDocument里的关键字为关键字自身加上run标签来设置字体颜色

    你可以参考下面方法,查找关键字,然后更改颜色。

        
                IEnumerable<TextRange> wordRanges = GetAllWordRanges(rtb.Document, "class");
                foreach (TextRange wordRange in wordRanges)
                {
                    wordRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
                }
             
    
            public static IEnumerable<TextRange> GetAllWordRanges(FlowDocument document, string specialword)
            {
                int needreturn = 0;
                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);
                        for (int i = 0; i < matches.Count; i++)
                        {
                            Match match = matches[i];
                            int startIndex = match.Index;
                            int length = match.Length;
                            TextPointer start = pointer.GetPositionAtOffset(startIndex);
                            TextPointer end = start.GetPositionAtOffset(length);
                            TextRange current = new TextRange(start, end);
                            if(needreturn == 1)
                            {
                                needreturn = 0;
                                yield return new TextRange(start, end);
                            }
                               
                            if (current.Text == specialword)
                            {
                                needreturn = 1;
                            }
                        }
                    }
                    pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
                }
            }
    
    
    
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月23日 3:27
    版主
  • Hi   便携式家园,

    >>想要制作一个关键字或关键字变色的功能,我的思路是替换FlowDocument里的关键字为关键字自身加上run标签来设置字体颜色

    你可以参考下面方法,查找关键字,然后更改颜色。

        
                IEnumerable<TextRange> wordRanges = GetAllWordRanges(rtb.Document, "class");
                foreach (TextRange wordRange in wordRanges)
                {
                    wordRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
                }
             
    
            public static IEnumerable<TextRange> GetAllWordRanges(FlowDocument document, string specialword)
            {
                int needreturn = 0;
                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);
                        for (int i = 0; i < matches.Count; i++)
                        {
                            Match match = matches[i];
                            int startIndex = match.Index;
                            int length = match.Length;
                            TextPointer start = pointer.GetPositionAtOffset(startIndex);
                            TextPointer end = start.GetPositionAtOffset(length);
                            TextRange current = new TextRange(start, end);
                            if(needreturn == 1)
                            {
                                needreturn = 0;
                                yield return new TextRange(start, end);
                            }
                               
                            if (current.Text == specialword)
                            {
                                needreturn = 1;
                            }
                        }
                    }
                    pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
                }
            }
    
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    能根据正则表达式匹配的字符串所在的下标区间内的字符串进行变色么?或其他文字外观效果处理?
    2019年8月23日 8:34
  • Hi   便携式家园, 

    上面提供的一种思路,我觉得主要的是,你需要查找出你需要操作的字符TextRange,然后这这上面进行字体格式的调整。

    WPF:在TextBlock和RichTextBox中根据坐标位置设置TextRange属性


    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月26日 3:26
    版主