优雅的日志窗口在WinForms C#中

69

我正在寻找实现Windows窗体应用程序日志窗口的有效方法。在过去,我已经使用了TextBox和RichTextBox来实现多个日志窗口,但我仍然对其功能不完全满意。

该日志旨在为用户提供有关各种事件的最近历史记录,主要用于数据收集应用程序中,其中一个人可能会想知道特定事务是如何完成的。在这种情况下,日志无需永久存储或保存到文件中。

首先,提出一些拟议的要求:

  • 高效快速;如果快速连续写入数百行到日志中,它需要消耗最少的资源和时间。
  • 能够提供多达2000行左右的可变回滚。超过此长度是不必要的。
  • 优先考虑高亮显示和颜色。字体效果不是必需的。
  • 当滚动限制达到时自动修剪行。
  • 添加新数据时自动滚动。
  • 奖励但不是必需的:在手动交互期间暂停自动滚动,例如,如果用户正在浏览历史记录。

迄今为止我一直在使用以下代码来编写和修剪日志:

// rtbLog is a RichTextBox
// _MaxLines is an int
public void AppendLog(string s, Color c, bool bNewLine)
{
    if (rtbLog.InvokeRequired)
    {
        object[] args = { s, c, bNewLine };
        rtbLog.Invoke(new AppendLogDel(AppendLog), args);
        return;
    }
    try
    {
        rtbLog.SelectionColor = c;
        rtbLog.AppendText(s);
        if (bNewLine) rtbLog.AppendText(Environment.NewLine);
        TrimLog();
        rtbLog.SelectionStart = rtbLog.TextLength;
        rtbLog.ScrollToCaret();
        rtbLog.Update();
    }
    catch (Exception exc)
    {
        // exception handling
    }
}

private void TrimLog()
{
    try
    {
        // Extra lines as buffer to save time
        if (rtbLog.Lines.Length < _MaxLines + 10)
        {
            return;
        }
        else
        {
            string[] sTemp = rtxtLog.Lines;
            string[] sNew= new string[_MaxLines];
            int iLineOffset = sTemp.Length - _MaxLines;
            for (int n = 0; n < _MaxLines; n++)
            {
                sNew[n] = sTemp[iLineOffset];
                iLineOffset++;
            }
            rtbLog.Lines = sNew;
        }
    }
    catch (Exception exc)
    {
        // exception handling
    }
}

这种方法的问题在于每次调用TrimLog时,我都会失去颜色格式。对于普通的文本框,这个方法可以正常工作(当然需要做一些修改)。

寻找解决方案从未真正令人满意。有些人建议通过字符数而不是行数来修剪RichTextBox中的多余文本。我也看到过使用ListBox,但尝试过没有成功。


而且我也有RTF导致线程同步崩溃的经验,即使在我当时需要SyncLock防止时,它也无能为力。+1 - Brett Allen
嗨,JYelton。我知道我本可以发布一个问题,但如果您能分享您的日志窗口实现工作方式,就会非常有帮助,就像在这里建议的那样[https://dev59.com/8XI95IYBdhLWcg3wsQFm#2196198]。 - StackUseR
@AshishSrivastava 这是很多年前的事了。实际上,日志组件使用队列作为循环缓冲区来存储文本行(一个日志类对象)。定时器会定期修改 RichTextBox 的内容以显示队列中的所有行。日志类对象包含文本行,但也包括时间戳和颜色。我得搜索一下找到这段代码,但如果有时间的话我会尽力的。 - JYelton
@JYelton:非常感谢。我很感激您的建议并会尝试着去实现它。但如果我能使用代码参考,那将非常有帮助。 - StackUseR
@AshishSrivastava 我已经发布了一个回答,其中提供了代码并解释了我所使用的基本实现。 - JYelton
7个回答

34

我建议您不要使用控件作为日志记录,而应编写一个 集合 类来满足您需要的属性(不包括显示属性)。

然后编写少量的代码,将该集合转储到各种用户界面元素中。个人而言,我会在我的日志对象中添加 SendToEditControlSendToListBox 方法。我可能还会为这些方法添加过滤功能。

您可以根据需要更新 UI 日志,以获得最佳性能,更重要的是,在日志快速更改时可以减少 UI 开销。

重要的是不要将日志记录与特定的 UI 绑定在一起,这是一个错误。将来您可能想要无头运行。

从长远来看,一个好的日志记录 UI 可能是一个自定义控件。但在短期内,您只需要将日志记录与任何 特定的 UI 分离开即可。


我喜欢这个建议,因为我曾考虑过将日志事件写入自定义类,并定期更新UI控件,这样可以更好地处理一次写入大量事件的情况。+1 - JYelton
我假设“分离”部分是已知的。 - Neil N
1
@Neil:根据我的经验,大多数新手似乎将控件用作“数据存储”,直到后来才意识到这是一个错误,而此时撤销已经很困难了。 - John Knoeller
1
谢谢,约翰。我写了一个日志类,并且现在使用效果非常好。如果你或其他人有兴趣看到它,我很乐意分享(并欢迎任何批评和评论)。 - JYelton
1
如果新手能看到这个记录器类的实现示例代码,那将会非常有帮助。有人可以做这个荣誉吗? - dhiraj suvarna
@dhirajsuvarna 我已经发布了一个展示示例代码的答案。 - JYelton

31

我基于之前写过的一个更加复杂的日志记录器简化了一下这个东西。

它支持根据日志级别在列表框中显示不同颜色,支持使用Ctrl+V和右键单击进行RTF格式的复制,并能够处理来自其他线程的日志记录到列表框中。

你可以通过使用其中一个构造函数重载来覆盖列表框中保留的行数(默认为2000行)以及消息格式。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Text;

namespace StackOverflow
{
    public partial class Main : Form
    {
        public static ListBoxLog listBoxLog;
        public Main()
        {
            InitializeComponent();

            listBoxLog = new ListBoxLog(listBox1);

            Thread thread = new Thread(LogStuffThread);
            thread.IsBackground = true;
            thread.Start();
        }

        private void LogStuffThread()
        {
            int number = 0;
            while (true)
            {
                listBoxLog.Log(Level.Info, "A info level message from thread # {0,0000}", number++);
                Thread.Sleep(2000);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Debug, "A debug level message");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Verbose, "A verbose level message");
        }
        private void button3_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Info, "A info level message");
        }
        private void button4_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Warning, "A warning level message");
        }
        private void button5_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Error, "A error level message");
        }
        private void button6_Click(object sender, EventArgs e)
        {
            listBoxLog.Log(Level.Critical, "A critical level message");
        }
        private void button7_Click(object sender, EventArgs e)
        {
            listBoxLog.Paused = !listBoxLog.Paused;
        }
    }

    public enum Level : int
    {
        Critical = 0,
        Error = 1,
        Warning = 2,
        Info = 3,
        Verbose = 4,
        Debug = 5
    };
    public sealed class ListBoxLog : IDisposable
    {
        private const string DEFAULT_MESSAGE_FORMAT = "{0} [{5}] : {8}";
        private const int DEFAULT_MAX_LINES_IN_LISTBOX = 2000;

        private bool _disposed;
        private ListBox _listBox;
        private string _messageFormat;
        private int _maxEntriesInListBox;
        private bool _canAdd;
        private bool _paused;

        private void OnHandleCreated(object sender, EventArgs e)
        {
            _canAdd = true;
        }
        private void OnHandleDestroyed(object sender, EventArgs e)
        {
            _canAdd = false;
        }
        private void DrawItemHandler(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                e.DrawBackground();
                e.DrawFocusRectangle();

                LogEvent logEvent = ((ListBox)sender).Items[e.Index] as LogEvent;

                // SafeGuard against wrong configuration of list box
                if (logEvent == null)
                {
                    logEvent = new LogEvent(Level.Critical, ((ListBox)sender).Items[e.Index].ToString());
                }

                Color color;
                switch (logEvent.Level)
                {
                    case Level.Critical:
                        color = Color.White;
                        break;
                    case Level.Error:
                        color = Color.Red;
                        break;
                    case Level.Warning:
                        color = Color.Goldenrod;
                        break;
                    case Level.Info:
                        color = Color.Green;
                        break;
                    case Level.Verbose:
                        color = Color.Blue;
                        break;
                    default:
                        color = Color.Black;
                        break;
                }

                if (logEvent.Level == Level.Critical)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
                }
                e.Graphics.DrawString(FormatALogEventMessage(logEvent, _messageFormat), new Font("Lucida Console", 8.25f, FontStyle.Regular), new SolidBrush(color), e.Bounds);
            }
        }
        private void KeyDownHandler(object sender, KeyEventArgs e)
        {
            if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.C))
            {
                CopyToClipboard();
            }
        }
        private void CopyMenuOnClickHandler(object sender, EventArgs e)
        {
            CopyToClipboard();
        }
        private void CopyMenuPopupHandler(object sender, EventArgs e)
        {
            ContextMenu menu = sender as ContextMenu;
            if (menu != null)
            {
                menu.MenuItems[0].Enabled = (_listBox.SelectedItems.Count > 0);
            }
        }

        private class LogEvent
        {
            public LogEvent(Level level, string message)
            {
                EventTime = DateTime.Now;
                Level = level;
                Message = message;
            }

            public readonly DateTime EventTime;

            public readonly Level Level;
            public readonly string Message;
        }
        private void WriteEvent(LogEvent logEvent)
        {
            if ((logEvent != null) && (_canAdd))
            {
                _listBox.BeginInvoke(new AddALogEntryDelegate(AddALogEntry), logEvent);
            }
        }
        private delegate void AddALogEntryDelegate(object item);
        private void AddALogEntry(object item)
        {
            _listBox.Items.Add(item);

            if (_listBox.Items.Count > _maxEntriesInListBox)
            {
                _listBox.Items.RemoveAt(0);
            }

            if (!_paused) _listBox.TopIndex = _listBox.Items.Count - 1;
        }
        private string LevelName(Level level)
        {
            switch (level)
            {
                case Level.Critical: return "Critical";
                case Level.Error: return "Error";
                case Level.Warning: return "Warning";
                case Level.Info: return "Info";
                case Level.Verbose: return "Verbose";
                case Level.Debug: return "Debug";
                default: return string.Format("<value={0}>", (int)level);
            }
        }
        private string FormatALogEventMessage(LogEvent logEvent, string messageFormat)
        {
            string message = logEvent.Message;
            if (message == null) { message = "<NULL>"; }
            return string.Format(messageFormat,
                /* {0} */ logEvent.EventTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                /* {1} */ logEvent.EventTime.ToString("yyyy-MM-dd HH:mm:ss"),
                /* {2} */ logEvent.EventTime.ToString("yyyy-MM-dd"),
                /* {3} */ logEvent.EventTime.ToString("HH:mm:ss.fff"),
                /* {4} */ logEvent.EventTime.ToString("HH:mm:ss"),

                /* {5} */ LevelName(logEvent.Level)[0],
                /* {6} */ LevelName(logEvent.Level),
                /* {7} */ (int)logEvent.Level,

                /* {8} */ message);
        }
        private void CopyToClipboard()
        {
            if (_listBox.SelectedItems.Count > 0)
            {
                StringBuilder selectedItemsAsRTFText = new StringBuilder();
                selectedItemsAsRTFText.AppendLine(@"{\rtf1\ansi\deff0{\fonttbl{\f0\fcharset0 Courier;}}");
                selectedItemsAsRTFText.AppendLine(@"{\colortbl;\red255\green255\blue255;\red255\green0\blue0;\red218\green165\blue32;\red0\green128\blue0;\red0\green0\blue255;\red0\green0\blue0}");
                foreach (LogEvent logEvent in _listBox.SelectedItems)
                {
                    selectedItemsAsRTFText.AppendFormat(@"{{\f0\fs16\chshdng0\chcbpat{0}\cb{0}\cf{1} ", (logEvent.Level == Level.Critical) ? 2 : 1, (logEvent.Level == Level.Critical) ? 1 : ((int)logEvent.Level > 5) ? 6 : ((int)logEvent.Level) + 1);
                    selectedItemsAsRTFText.Append(FormatALogEventMessage(logEvent, _messageFormat));
                    selectedItemsAsRTFText.AppendLine(@"\par}");
                }
                selectedItemsAsRTFText.AppendLine(@"}");
                System.Diagnostics.Debug.WriteLine(selectedItemsAsRTFText.ToString());
                Clipboard.SetData(DataFormats.Rtf, selectedItemsAsRTFText.ToString());
            }

        }

        public ListBoxLog(ListBox listBox) : this(listBox, DEFAULT_MESSAGE_FORMAT, DEFAULT_MAX_LINES_IN_LISTBOX) { }
        public ListBoxLog(ListBox listBox, string messageFormat) : this(listBox, messageFormat, DEFAULT_MAX_LINES_IN_LISTBOX) { }
        public ListBoxLog(ListBox listBox, string messageFormat, int maxLinesInListbox)
        {
            _disposed = false;

            _listBox = listBox;
            _messageFormat = messageFormat;
            _maxEntriesInListBox = maxLinesInListbox;

            _paused = false;

            _canAdd = listBox.IsHandleCreated;

            _listBox.SelectionMode = SelectionMode.MultiExtended;

            _listBox.HandleCreated += OnHandleCreated;
            _listBox.HandleDestroyed += OnHandleDestroyed;
            _listBox.DrawItem += DrawItemHandler;
            _listBox.KeyDown += KeyDownHandler;

            MenuItem[] menuItems = new MenuItem[] { new MenuItem("Copy", new EventHandler(CopyMenuOnClickHandler)) };
            _listBox.ContextMenu = new ContextMenu(menuItems);
            _listBox.ContextMenu.Popup += new EventHandler(CopyMenuPopupHandler);

            _listBox.DrawMode = DrawMode.OwnerDrawFixed;
        }

        public void Log(string message) { Log(Level.Debug, message); }
        public void Log(string format, params object[] args) { Log(Level.Debug, (format == null) ? null : string.Format(format, args)); }
        public void Log(Level level, string format, params object[] args) { Log(level, (format == null) ? null : string.Format(format, args)); }
        public void Log(Level level, string message)
        {
            WriteEvent(new LogEvent(level, message));
        }

        public bool Paused
        {
            get { return _paused; }
            set { _paused = value; }
        }

        ~ListBoxLog()
        {
            if (!_disposed)
            {
                Dispose(false);
                _disposed = true;
            }
        }
        public void Dispose()
        {
            if (!_disposed)
            {
                Dispose(true);
                GC.SuppressFinalize(this);
                _disposed = true;
            }
        }
        private void Dispose(bool disposing)
        {
            if (_listBox != null)
            {
                _canAdd = false;

                _listBox.HandleCreated -= OnHandleCreated;
                _listBox.HandleCreated -= OnHandleDestroyed;
                _listBox.DrawItem -= DrawItemHandler;
                _listBox.KeyDown -= KeyDownHandler;

                _listBox.ContextMenu.MenuItems.Clear();
                _listBox.ContextMenu.Popup -= CopyMenuPopupHandler;
                _listBox.ContextMenu = null;

                _listBox.Items.Clear();
                _listBox.DrawMode = DrawMode.Normal;
                _listBox = null;
            }
        }
    }
}

3
你会如何修改这个程序,使得长消息可以自动换行而不是被截断? - Chris

15
我会将这个代码段保存在这里,以便于我在未来想要使用 RichTextBox 来记录彩色行时可以查看。以下代码可以删除 RichTextBox 中的第一行:
if ( logTextBox.Lines.Length > MAX_LINES )
{
  logTextBox.Select(0, logTextBox.Text.IndexOf('\n')+1);
  logTextBox.SelectedRtf = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1053\\uc1 }";
}

我花了太长时间才弄清楚,将SelectedRtf属性设置为""是行不通的,但是将其设置为没有文本内容的“正确”的RTF格式是可以的。


3
我喜欢这个答案,因为它确实回答了原问题,对于想知道如何在与记录无关的 Rich Text Box 中实现此操作的人而言,这非常重要。 - djsumdog

15

我创建基本日志窗口的解决方案与John Knoeller在他的答案中建议的一样。避免直接将日志信息存储在TextBox或RichTextBox控件中,而是创建一个日志类,可以用于填充控件或写入文件等。

这个示例解决方案有几个部分:

  1. 日志类本身,Logger
  2. 修改RichTextBox控件以在更新后添加滚动到底部功能;ScrollingRichTextBox
  3. 主窗体以演示其使用,LoggerExample

首先,是日志类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Logger
{
    /// <summary>
    /// A circular buffer style logging class which stores N items for display in a Rich Text Box.
    /// </summary>
    public class Logger
    {
        private readonly Queue<LogEntry> _log;
        private uint _entryNumber;
        private readonly uint _maxEntries;
        private readonly object _logLock = new object();
        private readonly Color _defaultColor = Color.White;

        private class LogEntry
        {
            public uint EntryId;
            public DateTime EntryTimeStamp;
            public string EntryText;
            public Color EntryColor;
        }

        private struct ColorTableItem
        {
            public uint Index;
            public string RichColor;
        }

        /// <summary>
        /// Create an instance of the Logger class which stores <paramref name="maximumEntries"/> log entries.
        /// </summary>
        public Logger(uint maximumEntries)
        {
            _log = new Queue<LogEntry>();
            _maxEntries = maximumEntries;
        }

        /// <summary>
        /// Retrieve the contents of the log as rich text, suitable for populating a <see cref="System.Windows.Forms.RichTextBox.Rtf"/> property.
        /// </summary>
        /// <param name="includeEntryNumbers">Option to prepend line numbers to each entry.</param>
        public string GetLogAsRichText(bool includeEntryNumbers)
        {
            lock (_logLock)
            {
                var sb = new StringBuilder();

                var uniqueColors = BuildRichTextColorTable();
                sb.AppendLine($@"{{\rtf1{{\colortbl;{ string.Join("", uniqueColors.Select(d => d.Value.RichColor)) }}}");

                foreach (var entry in _log)
                {
                    if (includeEntryNumbers)
                        sb.Append($"\\cf1 { entry.EntryId }. ");

                    sb.Append($"\\cf1 { entry.EntryTimeStamp.ToShortDateString() } { entry.EntryTimeStamp.ToShortTimeString() }: ");

                    var richColor = $"\\cf{ uniqueColors[entry.EntryColor].Index + 1 }";
                    sb.Append($"{ richColor } { entry.EntryText }\\par").AppendLine();
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// Adds <paramref name="text"/> as a log entry.
        /// </summary>
        public void AddToLog(string text)
        {
            AddToLog(text, _defaultColor);
        }

        /// <summary>
        /// Adds <paramref name="text"/> as a log entry, and specifies a color to display it in.
        /// </summary>
        public void AddToLog(string text, Color entryColor)
        {
            lock (_logLock)
            {
                if (_entryNumber >= uint.MaxValue)
                    _entryNumber = 0;
                _entryNumber++;
                var logEntry = new LogEntry { EntryId = _entryNumber, EntryTimeStamp = DateTime.Now, EntryText = text, EntryColor = entryColor };
                _log.Enqueue(logEntry);

                while (_log.Count > _maxEntries)
                    _log.Dequeue();
            }
        }

        /// <summary>
        /// Clears the entire log.
        /// </summary>
        public void Clear()
        {
            lock (_logLock)
            {
                _log.Clear();
            }
        }

        private Dictionary<Color, ColorTableItem> BuildRichTextColorTable()
        {
            var uniqueColors = new Dictionary<Color, ColorTableItem>();
            var index = 0u;

            uniqueColors.Add(_defaultColor, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(_defaultColor) });

            foreach (var c in _log.Select(l => l.EntryColor).Distinct().Where(c => c != _defaultColor))
                uniqueColors.Add(c, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(c) });

            return uniqueColors;
        }

        private string ColorToRichColorString(Color c)
        {
            return $"\\red{c.R}\\green{c.G}\\blue{c.B};";
        }
    }
}

Logger类集成了另一个类LogEntry,它跟踪行号、时间戳和所需颜色。使用结构体来构建富文本颜色表。

接下来是修改后的RichTextBox:

using System;
using System.Runtime.InteropServices;

namespace Logger
{
    public class ScrollingRichTextBox : System.Windows.Forms.RichTextBox
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(
            IntPtr hWnd,
            uint Msg,
            IntPtr wParam,
            IntPtr LParam);

        private const int _WM_VSCROLL = 277;
        private const int _SB_BOTTOM = 7;

        /// <summary>
        /// Scrolls to the bottom of the RichTextBox.
        /// </summary>
        public void ScrollToBottom()
        {
            SendMessage(Handle, _WM_VSCROLL, new IntPtr(_SB_BOTTOM), new IntPtr(0));
        }
    }
}

我在这里所做的只是继承一个 RichTextBox 并添加一个“滚动到底部”的方法。关于如何实现这一点的其他问题有很多,我从 StackOverflow 上汲取了这种方法。

最后,以下是一个使用这个类的示例表单:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Logger
{
    public partial class LoggerExample : Form
    {
        private Logger _log = new Logger(100u);
        private List<Color> _randomColors = new List<Color> { Color.Red, Color.SkyBlue, Color.Green };
        private Random _r = new Random((int)DateTime.Now.Ticks);

        public LoggerExample()
        {
            InitializeComponent();
        }

        private void timerGenerateText_Tick(object sender, EventArgs e)
        {
            if (_r.Next(10) > 5)
                _log.AddToLog("Some event to log.", _randomColors[_r.Next(3)]);
        }

        private void timeUpdateLogWindow_Tick(object sender, EventArgs e)
        {
            richTextBox1.Rtf = _log.GetLogAsRichText(true);
            richTextBox1.ScrollToBottom();
        }
    }
}

这个表单使用了两个计时器,一个用于伪随机生成日志条目,另一个用于填充RichTextBox本身。在本例中,日志类实例化了100行的回滚。RichTextBox控件的颜色设置为黑色背景,带有白色和各种颜色的前景。生成文本的计时器间隔为100ms,更新日志窗口的计时器间隔为1000ms。

示例输出:

Logger Example Output

它远非完美或完成,但这里有一些警告和可添加或改进的事项(其中一些我在以后的项目中已经完成):

  1. 对于 maximumEntries 的大值,性能较差。这个日志记录类只设计了几百行回滚。
  2. 替换RichTextBox的文本可能会导致闪烁。我总是将刷新计时器保持在相对较慢的间隔(例如此示例中的一秒钟)。
  3. 添加到第2点,我的一些项目会在重新绘制RichTextBox内容之前检查日志是否有任何新条目,以避免不必要地刷新它。
  4. 每个日志条目上的时间戳可以选择是否显示,还可允许不同的格式。
  5. 在此示例中,没有办法暂停日志记录,但我的许多项目确实提供了一种暂停滚动行为的机制,以允许用户手动从日志窗口中滚动、选择和复制文本。

欢迎修改和改进此示例。欢迎反馈。


非常感谢JYelton。这确实帮了很大的忙。真的很抱歉给您添麻烦了。 - StackUseR
@JYelton 我认为在方法 AddToLog(string text, Color entryColor) 中的锁应该是 lock(_logLock),而不是 lock(_log) - mertcanb
非常感谢您提供的解决方案。您有处理手动滚动/选择的示例吗? - mertcanb
@mertcanb 我没有任何容易获取的。我已经转行,从软件转向硬件;抱歉! - JYelton
@JYelton 没问题。搬家恭喜! - mertcanb

6
我认为ListView(在详细查看模式下)非常适合这个需求,我在一些内部应用程序中正是使用它来实现此功能。
提示:如果您知道将一次性添加/删除大量项,请使用BeginUpdate()和EndUpdate()。

2
这也是我使用的方法。我以相反的顺序编写日志事件,从顶部插入并从底部拉出。还要捕获任何鼠标按下或滚动事件,以便在更新到来时不会与用户发生冲突。如果您的日志消息比窗口更长,则可以使用工具提示来显示额外的内容(https://dev59.com/1XVC5IYBdhLWcg3ww0Dr),或者在列表框旁边包含一个单独的文本框来保存详细信息。 - Ed Power
一个问题可能是BeginUpdate()/EndUpdate()的使用,因为发送日志事件的程序部分不一定“知道”是否会有大量事件。这并不意味着它们不能被实现,比如在包含所有生成日志事件的for循环周围。 - JYelton
我以前使用 ListBox,但刚尝试了一下 ListView。它似乎运行更快,而且不那么古怪,所以我可能从现在开始使用它,尽管我喜欢 ListBox 可以具有可变高度或所有者绘制项目的能力。ListView 能做这样的事情吗? - supercat

5

我最近也实现了类似的功能。我们的做法是保持一个滚动记录的环形缓冲区,然后手动绘制日志文本(使用Graphics.DrawString)。如果用户想要向后滚动、复制文本等操作,我们有一个"暂停"按钮,可以切换回普通的TextBox控件。


另一个好建议,我想知道创建自定义或继承的UI控件是否比尝试使用现有控件更容易。我唯一看到的问题是,在“暂停”期间,当自动滚动被暂停时,颜色格式将会丢失。 - JYelton

2
如果您想要高亮和颜色格式,我建议使用RichTextBox。
如果您想要自动滚动,则使用ListBox。
在任何情况下,将其绑定到行的循环缓冲区。

RTF存在他所述的问题,而ListBox不支持颜色编码。此外,我不喜欢不能像在RTF或文本框中那样突出显示和复制多行。 - Brett Allen
Aequitarum提出的一个好点是能够从日志中复制。看起来各种UI控件都有其优缺点。RTF是当前的实现,运行良好,但对其进行修剪并不直观。 - JYelton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接