C# 控制台应用程序输出颜色字体[更正版]

首先感谢院子里的“yanxinchen”,之前的方法是通过c#调用系统api实现的,相比之下我的有点画蛇添足了,哈哈。

最佳解决方案的代码:

 

复制代码
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Hello, color text!");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Hello, color text!");
Console.ReadKey();
}
复制代码

效果如图:

 

=================================================================================

我之前的代码如下(已过时):

控制台应用程序字体本身不能输出带颜色的字体,所有就需要调用系统的api实现,下面是一个封装好的类,另赋调用的方法,使用起来很方便,效果如图:

封装调用系统api的类ConsoleColor.cs代码如下:

ConsoleColor.cs

在Main方法的调用代码如下:

 

复制代码
ConsoleColor TextChange = new ConsoleColor();
Console.WriteLine("Original Colors");
Console.WriteLine("Press Enter to Begin");
Console.ReadLine();
TextChange.TextColor((int)ConsoleColor.Foreground.Green +
(int)ConsoleColor.Foreground.Intensity);
Console.WriteLine("THIS TEXT IS GREEN");
Console.WriteLine("Press Enter to change colors again");
Console.ReadLine();
TextChange.TextColor((int)ConsoleColor.Foreground.Red +
(int)ConsoleColor.Foreground.Blue +
(int)ConsoleColor.Foreground.Intensity);
Console.WriteLine("NOW THE TEXT IS PURPLE");
Console.WriteLine("Press Enter to change colors again");
Console.ReadLine();
TextChange.TextColor((int)ConsoleColor.Foreground.Blue +
(int)ConsoleColor.Foreground.Intensity +
(int)ConsoleColor.Background.Green +
(int)ConsoleColor.Background.Intensity);
Console.WriteLine("NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN");
Console.WriteLine("Press Enter change everything back to normal");
Console.ReadLine();
TextChange.ResetColor();
Console.WriteLine("Back to Original Colors");
Console.WriteLine("Press Enter to Terminate");
Console.ReadLine();
复制代码

 

转载于:https://www.cnblogs.com/wuyuxin/p/7002065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值