zoukankan      html  css  js  c++  java
  • 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();
    复制代码
  • 相关阅读:
    052-159
    ConfigurationManager.AppSettings["SiteUrl"] DataIDField="Id"
    for循环提交Grid中的数据,防止乱码的问题
    d_YWZC_new_Main中如何发送邮件
    EditGrid中的用法
    extjs中的suspendLayouts、suspendEvents、resumeEvents、resumeLayouts;
    如何保存复选框中的值,以及如何显示复选框中的值
    提交时,若Grid列表框中没有数据,如何利用js提示
    上传文件的时候,如何只获取选中的文件名
    复选框的全选和取消全选功能(来自华三,使用js代码实现)
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7002065.html
Copyright © 2011-2022 走看看