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();
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM