LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

WinForm利用 RichTextBox组件实现输出各种颜色字体日志信息

admin
2025年8月14日 19:43 本文热度 8

引言

   利用颜色来更好辨别输出日志信息,好了其他的不多说了,直接奔入主图吧。

效果图

实现代码

/// <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(100256),
                    rand.Next(100256),
                    rand.Next(100256)
                );
                // 生成随机文字
                string text = GenerateRandomString();
                WiterLog($"{i}{text}", randomColor);
            }
        }

        private string GenerateRandomString()
        {
            Random rand = new Random();
            int length = rand.Next(2050);

            // 常见汉字字符集
            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(1212);
            panel1.Name = "panel1";
            panel1.Size = new Size(105091);
            panel1.TabIndex = 0;
            // 
            // button1
            // 
            button1.Location = new Point(3755);
            button1.Name = "button1";
            button1.Size = new Size(24359);
            button1.TabIndex = 0;
            button1.Text = "开始生成随机字体颜色";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // panel2
            // 
            panel2.Controls.Add(txtBoxResultInfo);
            panel2.Location = new Point(19125);
            panel2.Name = "panel2";
            panel2.Size = new Size(1043531);
            panel2.TabIndex = 1;
            // 
            // txtBoxResultInfo
            // 
            txtBoxResultInfo.BackColor = SystemColors.InfoText;
            txtBoxResultInfo.Location = new Point(30);
            txtBoxResultInfo.Name = "txtBoxResultInfo";
            txtBoxResultInfo.Size = new Size(1040528);
            txtBoxResultInfo.TabIndex = 0;
            txtBoxResultInfo.Text = "";
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(11F24F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(1074668);
            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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved