Exemplo n.º 1
0
        private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule)
        {
            System.Windows.Controls.RichTextBox rtbx = this.TargetRichTextBox;

            TextRange tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd);

            tr.Text = logMessage + "\n";
            tr.ApplyPropertyValue(TextElement.ForegroundProperty,
                                  new SolidColorBrush(GetColorFromString(rule.FontColor, (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty)))
                                  );
            tr.ApplyPropertyValue(TextElement.BackgroundProperty,
                                  new SolidColorBrush(GetColorFromString(rule.BackgroundColor, (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty)))
                                  );
            tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style);
            tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight);

            //int startIndex = rtbx.Text.Length;
            //rtbx.SelectionStart = startIndex;
            //rtbx.SelectionBackColor = GetColorFromString(rule.BackgroundColor, rtbx.BackColor);
            //rtbx.SelectionColor = GetColorFromString(rule.FontColor, rtbx.ForeColor);
            //rtbx.SelectionFont = new Font(rtbx.SelectionFont, rtbx.SelectionFont.Style ^ rule.Style);
            //rtbx.AppendText(logMessage + "\n");
            //rtbx.SelectionLength = rtbx.Text.Length - rtbx.SelectionStart;

            //// find word to color
            //foreach (RichTextBoxWordColoringRule wordRule in this.WordColoringRules)
            //{
            //    MatchCollection mc = wordRule.CompiledRegex.Matches(rtbx.Text, startIndex);
            //    foreach (Match m in mc)
            //    {
            //        rtbx.SelectionStart = m.Index;
            //        rtbx.SelectionLength = m.Length;
            //        rtbx.SelectionBackColor = GetColorFromString(wordRule.BackgroundColor, rtbx.BackColor);
            //        rtbx.SelectionColor = GetColorFromString(wordRule.FontColor, rtbx.ForeColor);
            //        rtbx.SelectionFont = new Font(rtbx.SelectionFont, rtbx.SelectionFont.Style ^ wordRule.Style);
            //    }
            //}

            if (this.MaxLines > 0)
            {
                this.lineCount++;
                if (this.lineCount > this.MaxLines)
                {
                    tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd);
                    tr.Text.Remove(0, tr.Text.IndexOf('\n'));
                    this.lineCount--;
                }
            }

            if (this.AutoScroll)
            {
                //rtbx.Select(rtbx.TextLength, 0);
                rtbx.ScrollToEnd();
            }
        }
Exemplo n.º 2
0
        // -- END https://nlog.codeplex.com/workitem/6272

        private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule)
        {
            RichTextBox rtbx = TargetRichTextBox;

            var tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd)
            {
                Text = logMessage + "\r"
            };

            tr.ApplyPropertyValue(TextElement.ForegroundProperty,
                                  new SolidColorBrush(GetColorFromString(rule.FontColor,
                                                                         (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty)))
                                  );
            tr.ApplyPropertyValue(TextElement.BackgroundProperty,
                                  new SolidColorBrush(GetColorFromString(rule.BackgroundColor,
                                                                         (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty)))
                                  );
            tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style);
            tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight);

            if (MaxLines > 0)
            {
                _lineCount++;
                if (_lineCount > MaxLines)
                {
                    tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd);
                    tr.Text.Remove(0, tr.Text.IndexOf('\n'));
                    _lineCount--;
                }
            }

            if (AutoScroll)
            {
                rtbx.ScrollToEnd();
            }
        }
Exemplo n.º 3
0
        private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule)
        {
            System.Windows.Controls.RichTextBox rtbx = this.TargetRichTextBox;

            Paragraph paragraf = new Paragraph(new Run(logMessage));

            paragraf.Foreground = new SolidColorBrush(GetColorFromString(rule.FontColor, (SolidColorBrush)paragraf.Foreground));

            SolidColorBrush defaultBackgroundProperty = (SolidColorBrush)paragraf.Background;

            if (defaultBackgroundProperty == null)
            {
                defaultBackgroundProperty = Brushes.White;
            }
            paragraf.Background = new SolidColorBrush(GetColorFromString(rule.BackgroundColor, defaultBackgroundProperty));
            paragraf.FontStyle  = rule.Style;
            paragraf.FontWeight = rule.Weight;
            rtbx.Document.Blocks.Add(paragraf);

            //TextRange tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd);
            //tr.Text = logMessage + "\n";
            //tr.ApplyPropertyValue(TextElement.ForegroundProperty,
            //    new SolidColorBrush(GetColorFromString(rule.FontColor, (SolidColorBrush)tr.GetPropertyValue(TextElement.ForegroundProperty)))
            //);

            //SolidColorBrush defaultBackgroundProperty = (SolidColorBrush)tr.GetPropertyValue(TextElement.BackgroundProperty);
            //if (defaultBackgroundProperty == null)
            //    defaultBackgroundProperty = Brushes.White;
            //tr.ApplyPropertyValue(TextElement.BackgroundProperty,
            //    new SolidColorBrush(GetColorFromString(rule.BackgroundColor, defaultBackgroundProperty))
            //);
            //tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style);
            //tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight);

            //int startIndex = rtbx.Text.Length;
            //rtbx.SelectionStart = startIndex;
            //rtbx.SelectionBackColor = GetColorFromString(rule.BackgroundColor, rtbx.BackColor);
            //rtbx.SelectionColor = GetColorFromString(rule.FontColor, rtbx.ForeColor);
            //rtbx.SelectionFont = new Font(rtbx.SelectionFont, rtbx.SelectionFont.Style ^ rule.Style);
            //rtbx.AppendText(logMessage + "\n");
            //rtbx.SelectionLength = rtbx.Text.Length - rtbx.SelectionStart;

            //// find word to color
            //foreach (RichTextBoxWordColoringRule wordRule in this.WordColoringRules)
            //{
            //    MatchCollection mc = wordRule.CompiledRegex.Matches(rtbx.Text, startIndex);
            //    foreach (Match m in mc)
            //    {
            //        rtbx.SelectionStart = m.Index;
            //        rtbx.SelectionLength = m.Length;
            //        rtbx.SelectionBackColor = GetColorFromString(wordRule.BackgroundColor, rtbx.BackColor);
            //        rtbx.SelectionColor = GetColorFromString(wordRule.FontColor, rtbx.ForeColor);
            //        rtbx.SelectionFont = new Font(rtbx.SelectionFont, rtbx.SelectionFont.Style ^ wordRule.Style);
            //    }
            //}

            //if (this.MaxLines > 0)
            //{
            //    this.lineCount++;
            //    if (this.lineCount > this.MaxLines)
            //    {
            //        tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd);
            //        tr.Text.Remove(0, tr.Text.IndexOf('\n'));
            //        this.lineCount--;
            //    }
            //}

            if (this.AutoScroll)
            {
                //rtbx.Select(rtbx.TextLength, 0);
                rtbx.ScrollToEnd();
            }
        }