1、RichTextBox封装类
public class ConsoleBox
{
public ConsoleBox(RichTextBox richTextBox)
{
this.rtb = richTextBox;
}
public void ReceiveDataPrintf(string str)
{
Run item = new Run("数据接收:" + str);
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(item);
paragraph.Foreground = Brushes.Black;
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
public void SendDataPrintf(string str)
{
Run item = new Run("数据发送" + str);
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(item);
paragraph.Foreground = Brushes.Black;
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
public void Error(string str)
{
Run item = new Run("错误=>" + str);
Paragraph paragraph = new Paragraph();
paragraph.Foreground = Brushes.Red;
paragraph.Inlines.Add(item);
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
public void Warning(string str)
{
Run item = new Run("警告");
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(item);
paragraph.Foreground = Brushes.Coral;
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
public void Right(string str)
{
Run item = new Run("正确");
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(item);
paragraph.Foreground = Brushes.Green;
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
public void General(string str)
{
Run item = new Run(str);
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(item);
paragraph.LineHeight = 1.0;
paragraph.Foreground = Brushes.SlateBlue;
this.rtb.Document.Blocks.Add(paragraph);
this.rtb.ScrollToEnd();
}
private RichTextBox rtb;
}
2、使用方法
private void printfErr(string str)
{
this.ConsoleBox.Dispatcher.Invoke(new Action(delegate ()
{
ConsoleBox console = new ConsoleBox(ConsoleBox);
console.Error(str);
}), null);
}