C# 输出带颜色文字,用于实时日志输出

 1      private void button1_Click(object sender, EventArgs e)  
 2         {  
 3           LogMessage("绿色");  
 4        LogError("红色");  
 5        LogWarning("粉色");  
 6         }  
 9 #region 日志记录、支持其他线程访问  
10   
11         public delegate void LogAppendDelegate(Color color, string text);  
12   
13         public void LogAppendMethod(Color color, string text)  
14         {  
15             if (!richTextBox1.ReadOnly)  
16                 richTextBox1.ReadOnly = true;  
17   
18             richTextBox1.AppendText("\n");  
19             richTextBox1.SelectionColor = color;  
20             richTextBox1.AppendText(text);  
21         }  
22   
23         public void LogError(string text)  
24         {  
25             LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);  
26             richTextBox1.Invoke(la, Color.Red, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);  
27         }  
28         public void LogWarning(string text)  
29         {  
30             LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);  
31             richTextBox1.Invoke(la, Color.Violet, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);  
32         }  
33         public void LogMessage(string text)  
34         {  
35             LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);  
36             richTextBox1.Invoke(la, Color.Green, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);  
37         }  
38          
39 #endregion  

 如图显示:

posted @ 2017-03-14 15:40  法号阿兴  阅读(4615)  评论(0编辑  收藏  举报