引言
利用顏色來更好辨別輸出日志信息,好了其他的不多說了,直接奔入主圖吧。
?效果圖
實現代碼
/// <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 編輯過