将日志信息(提示、警告、错误、历史报警)显示到RichTextBox组件上

将日志信息(提示、警告、错误、历史报警)显示到RichTextBox组件上、点击某个事件按钮,会显示相对应的日志信息。效果图如下:

提示信息代码如下:

        /// <summary>
        /// 提示事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_tip_Click(object sender, EventArgs e)
        {
            if (tsb_tip.Checked)
            {
                tsb_warn.Checked = false;
                tsb_error.Checked = false;
                tsb_historyAlarm.Checked = false;

                tbx_msg.Clear();
                tbx_msg.ForeColor = Color.FromArgb(48, 48, 48);
                //使用for循环遍历Configuration.msgItems集合中的每个元素
                for (int i = 0; i< Configuration.msgItems.Count; i++)
                {
                    //对于每个元素,检查其infoType属性是否等于InfoType.Tip
                    if (Configuration.msgItems[i].infoType == InfoType.Tip)
                    {
                        //如果文本框tbx_msg是空的(即tbx_msg.Text == string.Empty)
                        if (tbx_msg.Text == string.Empty)
                        {
                            //直接将时间、空格和消息内容追加到文本框中
                            tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                        }
                        else
                        {
                            //如果文本框不为空,则在追加新消息前,先追加一个换行符(Environment.NewLine),然后追加时间、空格和消息内容
                            tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                        }
                    }
                }
            }
            else
            {
                tbx_msg.Clear();
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    tbx_msg.SelectionStart = tbx_msg.Text.Length;
                    tbx_msg.SelectionLength = 0;

                    switch (Configuration.msgItems[i].infoType)
                    {
                        case InfoType.Tip:
                            tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        
                        case InfoType.Warn:
                           tbx_msg.SelectionColor = Color.Orange;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        
                        case InfoType.Error:
                           tbx_msg.SelectionColor = Color.Red;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                    }
                }
            }
            tbx_msg.ScrollToCaret();
        }

警告事件代码如下:

        /// <summary>
        /// 警告事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_warn_Click(object sender, EventArgs e)
        {
            if (tsb_warn.Checked)
            {
                tsb_tip.Checked = false;
                tsb_error.Checked = false;
                tsb_historyAlarm.Checked = false;

                tbx_msg.Clear();
                tbx_msg.ForeColor = Color.Orange;
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    if (Configuration.msgItems[i].infoType == InfoType.Warn)
                    {
                        if (tbx_msg.Text == string.Empty)
                            tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                        else
                            tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                    }
                }
            }
            else
            {
                tbx_msg.Clear();
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    tbx_msg.SelectionStart = tbx_msg.Text.Length;
                    tbx_msg.SelectionLength = 0;

                    switch (Configuration.msgItems[i].infoType)
                    {
                        case InfoType.Tip:
                           tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Warn:
                            tbx_msg.SelectionColor = Color.Orange;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Error:
                            tbx_msg.SelectionColor = Color.Red;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                    }
                }
            }
            tbx_msg.ScrollToCaret();
        }

错误事件代码如下:

        /// <summary>
        /// 错误事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_error_Click(object sender, EventArgs e)
        {
            if (tsb_error.Checked)
            {
                tsb_tip.Checked = false;
                tsb_warn.Checked = false;
                tsb_historyAlarm.Checked = false;

                tbx_msg.Clear();
                tbx_msg.ForeColor = Color.Red;
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    if (Configuration.msgItems[i].infoType == InfoType.Error)
                    {
                        if (tbx_msg.Text == string.Empty)
                            tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                        else
                            tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                    }
                }
            }
            else
            {
                tbx_msg.Clear();
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    tbx_msg.SelectionStart = tbx_msg.Text.Length;
                    tbx_msg.SelectionLength = 0;

                    switch (Configuration.msgItems[i].infoType)
                    {
                        case InfoType.Tip:
                            tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Warn:
                            tbx_msg.SelectionColor = Color.Orange;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Error:
                            tbx_msg.SelectionColor = Color.Red;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                    }
                }
            }
            tbx_msg.ScrollToCaret();
        }

历史报警事件代码如下:

        /// <summary>
        /// 历史报警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
private void tsb_historyAlarm_Click(object sender, EventArgs e)
        {
            if (tsb_historyAlarm.Checked)
            {
                tsb_tip.Checked = false;
                tsb_warn.Checked = false;
                tsb_error.Checked = false;

                tbx_msg.Clear();
                tbx_msg.ForeColor = Color.Red;
                foreach (KeyValuePair<DateTime, string> item in Configuration.Instance.historyAlarm)
                {
                    if (tbx_msg.Text == string.Empty)
                        tbx_msg.AppendText(item.Key.ToString("yyyy_MM_dd HH:mm:ss") + "   " + item.Value);
                    else
                        tbx_msg.AppendText(Environment.NewLine + item.Key.ToString("yyyy_MM_dd HH:mm:ss") + "   " + item.Value);
                }
            }
            else
            {
                tbx_msg.Clear();
                for (int i = 0; i < Configuration.msgItems.Count; i++)
                {
                    tbx_msg.SelectionStart = tbx_msg.Text.Length;
                    tbx_msg.SelectionLength = 0;

                    switch (Configuration.msgItems[i].infoType)
                    {
                        case InfoType.Tip:
                            tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Warn:
                            tbx_msg.SelectionColor = Color.Orange;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                        case InfoType.Error:
                            tbx_msg.SelectionColor = Color.Red;

                            if (tbx_msg.Text == string.Empty)
                                tbx_msg.AppendText(Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            else
                                tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + "   " + Configuration.msgItems[i].msg);
                            break;
                    }
                }
            }
            tbx_msg.ScrollToCaret();
        }

可根据自己需求自行修改补充代码。

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问。关于您的问题,可以通过以下代码实现: ```c# public partial class Form1 : Form { [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll")] static extern IntPtr LoadImage(IntPtr hInst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad); private const uint WM_SETREDRAW = 0x000B; private const uint OIC_DOCUMENT = 0x00000003; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件对话框 OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "NC files (*.nc)|*.nc"; openFileDialog1.Title = "Select a NC File"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); IntPtr hIcon = LoadImage(IntPtr.Zero, "shell32.dll", OIC_DOCUMENT, 16, 16, 0x00000000); richTextBox1.InsertImage(hIcon, new FileInfo(openFileDialog1.FileName).Name + "\n"); StreamReader sr = new StreamReader(openFileDialog1.FileName); richTextBox1.AppendText(sr.ReadToEnd()); sr.Close(); } } private void richTextBox1_VScroll(object sender, EventArgs e) { SendMessage(richTextBox1.Handle, WM_SETREDRAW, 0, 0); richTextBox1.Invalidate(); SendMessage(richTextBox1.Handle, WM_SETREDRAW, 1, 0); } } public static class RichTextBoxExtensions { public static void InsertImage(this RichTextBox box, Image image, string caption) { Clipboard.SetImage(image); if (box.CanPaste(DataFormats.GetFormat(DataFormats.Bitmap))) { box.SelectionStart = 0; box.Paste(); box.AppendText(caption + "\n"); } } } ``` 在这个例子中,利用SendMessage函数和WM_SETREDRAW消息实现了快速滚动和加速地绘制内容。同时,用InsertImage函数将NC文件的图标插入到RichTextBox中。 希望能够帮到您,感谢您的提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值