控制台文本内容多颜色打印
记录下,后面自己也可以用到,首先先写一个随机颜色的方法
/// <summary>
/// 获得随机颜色
/// </summary>
/// <returns></returns>
public Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
Thread.Sleep(RandomNum_First.Next(10));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
接着再写一个控件内容显示的颜色设置的方法
/// <summary>
/// richtext每行不同颜色
/// </summary>
/// <param name="rtBox">控件对象</param>
/// <param name="text">内容</param>
/// <param name="color">颜色</param>
/// <param name="addNewLine">是否换行</param>
private void AppendTextColorful(RichTextBox rtBox, string text, Color color, bool addNewLine)
{
if (addNewLine)
{
text += Environment.NewLine;
}
rtBox.SelectionStart = rtBox.TextLength;
rtBox.SelectionLength = 0;
rtBox.SelectionColor = color;
rtBox.AppendText(text);
rtBox.SelectionColor = rtBox.ForeColor;
}
然后就是方法的调用
AppendTextColorful(this.rhtxt_Run, allRunLogStr, GetRandomColor(), true);
我们来看看效果
顺便附上上图种美女的字符内容代码
/// <summary>
/// 显示美女
/// </summary>
/// <returns></returns>
private static string ShowBeatifulGirl()
{
string strTemp = "";
strTemp +=
" --------------------------------------------------------------- " + Environment.NewLine +
"| .::::. |" + Environment.NewLine +
"| .::::::::. 都什么时候了还在写程序 |" + Environment.NewLine +
"| ::::::::::: 抓紧泡妞去吧! |" + Environment.NewLine +
"| ..:::::::::::' |" + Environment.NewLine +
"| '::::::::::::' |" + Environment.NewLine +
"| .:::::::::: |" + Environment.NewLine +
"| '::::::::::::::.. |" + Environment.NewLine +
"| ..::::::::::::. |" + Environment.NewLine +
"| ``:::::::::::::::: |" + Environment.NewLine +
"| ::::``:::::::::' .:::. |" + Environment.NewLine +
"| ::::' ':::::' .::::::::. |" + Environment.NewLine +
"| .::::' :::: .:::::::'::::. |" + Environment.NewLine +
"| .:::' ::::: .:::::::::' ':::::. |" + Environment.NewLine +
"| .::' :::::.:::::::::' ':::::. |" + Environment.NewLine +
"| .::' ::::::::::::::' ``::::. |" + Environment.NewLine +
"| ...::: ::::::::::::' ``::. |" + Environment.NewLine +
"| ````':. ':::::::::' ::::.. |" + Environment.NewLine +
"| '.:::::' ':'````.. |" + Environment.NewLine +
" --------------------------------------------------------------- " + Environment.NewLine + Environment.NewLine;
return strTemp;
}