您好,登錄后才能下訂單哦!
本篇文章展示了C#控制臺輸出彩色字體的具體代碼,代碼簡明扼要容易理解,如果在日常工作遇到這個(gè)疑問。希望大家通過這篇文章,找到解決疑問的辦法。
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(); } } }
效果圖:
關(guān)于C#控制臺輸出彩色字體的代碼就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。