NEWS

[C#] Hướng dẫn thay đổi màu chữ Console Application

[C#] Hướng dẫn thay đổi màu chữ Console Application
Đăng bởi: Thảo Meo - Lượt xem: 26731 15:06:50, 20/06/2018C#   In bài viết

Bình thường khi các bạn viết các ứng dụng Console Application C# thì giao diện mặc định là nền đen chữ trắng. Bài viết hôm nay, mình sẽ hướng dẫn các bạn cách lập trình thay đổi màu chữ trên màn hình console.

Dưới đây, là hai đoạn code thay đổi màu nền và màu chữ của Console App

ConsoleColor background = Console.BackgroundColor;  
ConsoleColor foreground = Console.ForegroundColor;  

Console.ForegroundColor = ConsoleColor.White;  
Console.BackgroundColor = ConsoleColor.Red;

Bây giờ, nếu các bạn muốn khôi phục màu chữ và nền gốc, các bạn thực hiện câu lệnh reset bên dưới:

Console.ResetColor();

Dưới đây là giao diện ứng dụng demo thay đổi màu chữ và nền window console C#

 

màu chữ giao diện console app c#

Source code C#:

using System;  
class ConsoleColorsClass {  
    static void Main(string[] args) {  
        // Let's go through all Console colors and set them as foreground  
        foreach(ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {  
            Console.ForegroundColor = color;  
            Console.WriteLine($ "Foreground color set to {color}");  
        }  
        Console.WriteLine("=====================================");  
        Console.ForegroundColor = ConsoleColor.White;  
        // Let's go through all Console colors and set them as background  
        foreach(ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {  
            Console.BackgroundColor = color;  
            Console.WriteLine($ "Background color set to {color}");  
        }  
        Console.WriteLine("=====================================");  
        // Restore original colors  
        Console.ResetColor();  
        Console.ReadKey();  
    }  
}  

HAVE FUN :)

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn thay đổi màu chữ Console Application
Đăng bởi: Thảo Meo - Lượt xem: 26731 15:06:50, 20/06/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.