C# 控制台应用程序中输出彩色字体

简介: using System;class Example{ public static void Main() { // Get a string array with the names of ConsoleColor enumeration members.
using System;

class Example
{
   public static void Main() 
   {
      // Get a string array with the names of ConsoleColor enumeration members.
      String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

      // Display each foreground color except black on a constant black background.
      Console.WriteLine("All the foreground colors (except Black) on a constant black background:");

      foreach (string colorName in colorNames)
      {
         // Convert the string representing the enum name to the enum value.
         ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);

         if (color == ConsoleColor.Black) continue;

         Console.Write("{0,11}: ", colorName);
         Console.BackgroundColor = ConsoleColor.Black;
         Console.ForegroundColor = color;
         Console.WriteLine("This is foreground color {0}.", colorName);
         // Restore the original foreground and background colors.
         Console.ResetColor();
      }
      Console.WriteLine();

      // Display each background color except white with a constant white foreground.
      Console.WriteLine("All the background colors (except White) with a constant white foreground:");

      foreach (string colorName in colorNames)
      {
         // Convert the string representing the enum name to the enum value.
         ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);

         if (color == ConsoleColor.White) continue;

         Console.Write("{0,11}: ", colorName);
         Console.ForegroundColor = ConsoleColor.White;
         Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
         Console.WriteLine("This is background color {0}.", colorName);
         Console.ResetColor();
      }
   }
}
效果如下:

目录
相关文章
|
17天前
|
C#
C# DEV 关于设置gridview 指定单元格字体为红色
C# DEV 关于设置gridview 指定单元格字体为红色
|
3月前
|
C# 开发者
C# 9.0中的模块初始化器:程序启动的新控制点
【1月更文挑战第14天】本文介绍了C# 9.0中引入的新特性——模块初始化器(Module initializers)。模块初始化器允许开发者在程序集加载时执行特定代码,为类型初始化提供了更细粒度的控制。文章详细阐述了模块初始化器的语法、用途以及与传统类型初始化器的区别,并通过示例代码展示了如何在实际项目中应用这一新特性。
|
3月前
|
编译器 C# 开发者
C# 9.0中的顶级语句:简化程序入口的新特性
【1月更文挑战第13天】本文介绍了C# 9.0中引入的顶级语句(Top-level statements)特性,该特性允许开发者在不使用传统的类和方法结构的情况下编写简洁的程序入口代码。文章详细阐述了顶级语句的语法、使用场景以及与传统程序结构的区别,并通过示例代码展示了其在实际应用中的便捷性。
|
6月前
|
C#
CAD2015 C#二次开发 字体变形
CAD2015 C#二次开发 字体变形
|
6月前
|
开发框架 .NET C#
利用WinDbg分析C#程序产生的转储文件
利用WinDbg分析C#程序产生的转储文件
|
6月前
|
C# C++
VS调试C#程序产生的dump
VS调试C#程序产生的dump
|
6月前
|
C#
C#程序Debug文件夹可以运行,无法调试
C#程序Debug文件夹可以运行,无法调试
|
1月前
|
Web App开发 缓存 运维
应用研发平台EMAS产品常见问题之emas控制台访问非常慢如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
|
1月前
|
Java C# 开发工具
第一个C#程序
第一个C#程序
12 0
|
1月前
|
数据采集 存储 C#
抓取Instagram数据:Fizzler库带您进入C#程序的世界
在当今数字化的世界中,数据是无价之宝。社交媒体平台如Instagram成为了用户分享照片、视频和故事的热门场所。作为开发人员,我们可以利用爬虫技术来抓取这些平台上的数据,进行分析、挖掘和应用。本文将介绍如何使用C#编写一个简单的Instagram爬虫程序,使用Fizzler库来解析HTML页面,同时利用代理IP技术提高采集效率。
抓取Instagram数据:Fizzler库带您进入C#程序的世界