C#

콘솔 글자색 변경

잼잼재미 2023. 11. 13. 15:16

콘솔에 출력되는 글자색을 변경

 

 

사용 예시


1.

public void ShowHighlightedText (string text)
{
	Console.ForegroundColor = ConsoleColor.Magenta;
	Console.WriteLine(text);
	Console.ResetColor();
}

 

 

2.

public enum Color
{
    Black,
    DarkBlue,
    DarkGreen,
    DarkCyan,
    DarkRed,
    DarkMagenta,
    DarkYellow,
    Gray,
    DarkGray,
    Blue,
    Green,
    Cyan,
    Red,
    Magenta,
    Yellow,
    White,
}

	Color color;

public void WriteColorFont(string text, Color color)
{
    switch (color)
    {
        case Color.Black:
            Console.ForegroundColor = ConsoleColor.Black;
            break;

        case Color.DarkBlue:
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            break;

        case Color.DarkGreen:
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            break;

        case Color.DarkCyan:
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            break;

        case Color.DarkRed:
            Console.ForegroundColor = ConsoleColor.DarkRed;
            break;

        case Color.DarkMagenta:
            Console.ForegroundColor = ConsoleColor.Magenta;
            break;

        case Color.DarkYellow:
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            break;

        case Color.Gray:
            Console.ForegroundColor = ConsoleColor.Gray;
            break;

        case Color.DarkGray:
            Console.ForegroundColor = ConsoleColor.DarkGray;
            break;

        case Color.Blue:
            Console.ForegroundColor = ConsoleColor.Blue;
            break;

        case Color.Green:
            Console.ForegroundColor = ConsoleColor.Green;
            break;

        case Color.Cyan:
            Console.ForegroundColor = ConsoleColor.Cyan;
            break;

        case Color.Red:
            Console.ForegroundColor = ConsoleColor.Red;
            break;

        case Color.Magenta:
            Console.ForegroundColor = ConsoleColor.Magenta;
            break;

        case Color.Yellow:
            Console.ForegroundColor = ConsoleColor.Yellow;
            break;

        case Color.White:
            Console.ForegroundColor = ConsoleColor.White;
            break;

        default:
            break;
    }

    Console.Write(text);
    Console.ResetColor();
}

 

 

색상


 

'C#' 카테고리의 다른 글

값형과 참조형 (박싱과 언박싱)  (0) 2023.11.13
범위 내 입력값 받기  (1) 2023.11.13
문자 앞뒤로 특정 문자 입력 (RightPad, LeftPad)  (0) 2023.11.13
열거형 (Enums)  (0) 2023.11.10
인터페이스(Interface)  (0) 2023.11.09