WinForm利用 RichTextBox组件实现输出各种颜色字体日志信息
				
									
					
					
						|  | 
							admin 2025年8月14日 19:43
								本文热度 768 | 
					
				 
				引言
   利用颜色来更好辨别输出日志信息,好了其他的不多说了,直接奔入主图吧。
 效果图
效果图
实现代码
/// <summary>
/// 输出并设置字体颜色
/// </summary>
/// <param name="msg"></param>
/// <param name="color"></param>
private void WiterLog(string msg, Color color)
{
    this.Invoke(new Action(() =>
    {
        txtBoxResultInfo.SuspendLayout();
        txtBoxResultInfo.SelectionStart = txtBoxResultInfo.TextLength;
        txtBoxResultInfo.SelectionLength = 0;
        txtBoxResultInfo.SelectionColor = color;
        txtBoxResultInfo.AppendText(msg + Environment.NewLine);
        txtBoxResultInfo.SelectionColor = txtBoxResultInfo.ForeColor;
        // 滚动到TextBox的最后一行
        txtBoxResultInfo.ScrollToCaret();
        txtBoxResultInfo.ResumeLayout();
    }));
}
完整的示例代码
namespace WinFormsApp1
{
    publicpartialclassForm1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
           
            Random rand = new Random();
          
            for (int i = 1; i <= 50; i++)
            {
                // 生成随机颜色
                Color randomColor = Color.FromArgb(
                    rand.Next(100, 256),
                    rand.Next(100, 256),
                    rand.Next(100, 256)
                );
                // 生成随机文字
                string text = GenerateRandomString();
                WiterLog($"{i}、{text}", randomColor);
            }
        }
        private string GenerateRandomString()
        {
            Random rand = new Random();
            int length = rand.Next(20, 50);
            // 常见汉字字符集
            string chineseChars = "的一是了我不人在有他这为之大来以个中上们到说国和地也子时道出而于就得里后自之者发经行家方如事成";
            // 生成随机中文字符
            char[] chars = newchar[length];
            for (int i = 0; i < length; i++)
            {
                chars[i] = chineseChars[rand.Next(chineseChars.Length)];
            }
            returnnewstring(chars);
        }
        /// <summary>
        /// 输出并设置字体颜色
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="color"></param>
        private void WiterLog(string msg, Color color)
        {
            this.Invoke(new Action(() =>
            {
                txtBoxResultInfo.SuspendLayout();
                txtBoxResultInfo.SelectionStart = txtBoxResultInfo.TextLength;
                txtBoxResultInfo.SelectionLength = 0;
                txtBoxResultInfo.SelectionColor = color;
                txtBoxResultInfo.AppendText(msg + Environment.NewLine);
                txtBoxResultInfo.SelectionColor = txtBoxResultInfo.ForeColor;
                // 滚动到TextBox的最后一行
                txtBoxResultInfo.ScrollToCaret();
                txtBoxResultInfo.ResumeLayout();
            }));
        }
    }
}
设计器代码
namespace WinFormsApp1
{
    partialclassForm1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            panel1 = new Panel();
            button1 = new Button();
            panel2 = new Panel();
            txtBoxResultInfo = new RichTextBox();
            panel1.SuspendLayout();
            panel2.SuspendLayout();
            SuspendLayout();
            // 
            // panel1
            // 
            panel1.Controls.Add(button1);
            panel1.Location = new Point(12, 12);
            panel1.Name = "panel1";
            panel1.Size = new Size(1050, 91);
            panel1.TabIndex = 0;
            // 
            // button1
            // 
            button1.Location = new Point(375, 5);
            button1.Name = "button1";
            button1.Size = new Size(243, 59);
            button1.TabIndex = 0;
            button1.Text = "开始生成随机字体颜色";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // panel2
            // 
            panel2.Controls.Add(txtBoxResultInfo);
            panel2.Location = new Point(19, 125);
            panel2.Name = "panel2";
            panel2.Size = new Size(1043, 531);
            panel2.TabIndex = 1;
            // 
            // txtBoxResultInfo
            // 
            txtBoxResultInfo.BackColor = SystemColors.InfoText;
            txtBoxResultInfo.Location = new Point(3, 0);
            txtBoxResultInfo.Name = "txtBoxResultInfo";
            txtBoxResultInfo.Size = new Size(1040, 528);
            txtBoxResultInfo.TabIndex = 0;
            txtBoxResultInfo.Text = "";
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(11F, 24F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(1074, 668);
            Controls.Add(panel2);
            Controls.Add(panel1);
            Name = "Form1";
            Text = "Form1";
            panel1.ResumeLayout(false);
            panel2.ResumeLayout(false);
            ResumeLayout(false);
        }
        #endregion
        private Panel panel1;
        private Button button1;
        private Panel panel2;
        private RichTextBox txtBoxResultInfo;
    }
}
 界面
界面
阅读原文:原文链接
该文章在 2025/8/15 12:37:33 编辑过