double offsetDebug = 0;
int maxLines = 300;
void ShowLog(string msg, Brush color)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart)delegate ()
{
string s = string.Format("{0}:[{1}]", DateTime.Now, msg);
Paragraph p = new Paragraph(new Run(s));
p.FontSize = 14;
p.LineHeight = 3;
p.Foreground = color;
rtb_Msg.Document.Blocks.Add(p);
needAutoScroll(rtb_Msg, ref offsetDebug);
if (rtb_Msg.Document.Blocks.Count > maxLines)
for (int i = maxLines; i < rtb_Msg.Document.Blocks.Count; i++)
{
rtb_Msg.Document.Blocks.Remove(rtb_Msg.Document.Blocks.FirstBlock);
}
});
}
bool needAutoScroll(RichTextBox rich, ref double offset)
{
if (rich.VerticalOffset >= offset)
{
rtb_Msg.ScrollToEnd();
offset = rich.VerticalOffset;
return true;
}
else
return false;
}
ShowLog("Test..", Brushes.Green);