ホームページ  >  記事  >  バックエンド開発  >  C#コンソールアプリケーションでカラーフォントを出力する方法を詳しく紹介

C#コンソールアプリケーションでカラーフォントを出力する方法を詳しく紹介

黄舟
黄舟オリジナル
2017-05-28 10:01:242588ブラウズ

この記事では主にC#コンソールアプリケーションでカラーフォントを出力する方法を詳しく紹介しますので、興味のある方は参考にしてください

この記事の例はC#コンソールでカラーフォントを出力する方法を皆さんに共有します。フォントの具体的なコードは次のとおりです


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();
  }
 }
}

Rendering:

以上がC#コンソールアプリケーションでカラーフォントを出力する方法を詳しく紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。