当前位置: 首页 > ai >正文

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

引言

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

效果图

实现代码
/// <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
{public partial class Form1 : 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 = new char[length];for (int i = 0; i < length; i++){chars[i] = chineseChars[rand.Next(chineseChars.Length)];}return new string(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
{partial class Form1{/// <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);}#endregionprivate Panel panel1;private Button button1;private Panel panel2;private RichTextBox txtBoxResultInfo;}
}

界面

http://www.xdnf.cn/news/17218.html

相关文章:

  • React 原生部落的生存现状:观察“Hooks 猎人“如何用useEffect设陷阱反被依赖项追杀
  • HarmonyOS 设备自动发现与连接全攻略:从原理到可运行 Demo
  • FreeRTOS入门知识(初识RTOS)(二)
  • Latex中公式部分输入正体的字母\mathrm{c}
  • A100用transformers推理gpt-oss
  • Dijkstra?spfa?SPstra?
  • 【Rust】多级目录模块化集成测试——以Cucumber为例
  • 深入探索 PDF 数据提取:PyMuPDF 与 pdfplumber 的对比与实战
  • PCB焊盘脱落的补救办法与猎板制造优势解析
  • 五种IO模型 阻塞IO 多路转接之select 多路转接之poll
  • AI学习笔记三十五:实时传输视频
  • python应用GRPC || consul 服务注册发现
  • GraphRAG 入门教程:从原理到实战
  • 碰一碰NFC开发写好评php语言源码
  • day21|学习前端vue3框架和ts语言
  • 什么是SpringBoot
  • Spring事务失效场景?
  • TCP粘包问题详解与解决方案
  • 使用SETNX实现分布式锁
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘spacy’问题
  • 【C#补全计划:类和对象(九)】接口
  • 嵌入式开发硬件——单片机
  • QtC++ 中使用 qtwebsocket 开源库实现基于websocket的本地服务开发详解
  • Java中接口与抽象类
  • 【MATLAB】(十)符号运算
  • idea开发工具中git如何忽略编译文件build、gradle的文件?
  • 为什么 `source ~/.bashrc` 在 systemd 或 crontab 中不生效
  • 安卓开发:网络状态监听封装的奥秘
  • vLLM:彻底改变大型语言模型推理延迟和吞吐量
  • 【Apache Olingo】全面深入分析报告-OData