2017-12-29 34 views
0
public class Utils 
    { 

     public static void LogDebug(string debuglog) 
     { 
      Console.WriteLine($"[Debug] {debuglog}", System.Drawing.Color.Yellow;); //That $ passes the arg(string log) into the string function thus printing it into console 
     } 

     public static void InfoLog(string infolog) 
     { 
      Console.WriteLine($"[Info] {infolog}",); 
     } 

     public static void WarningLog(string warning) 
     { 
      Console.WriteLine($"[Warn] {warning}",); 
     } 
    } 
} 

나는이 코드를 사용하여 오류를 식별하고 주위를 둘러싼 것들을 식별 할 수있게 도와 주었지만, 모두 흰색이라면 정말 도움이되지 않습니다. 당신이 System.Drawing.Color.Yellow;Util을위한 컬러 텍스트

대신

해당 색상에 기록 된 모든 텍스트를 변경
Console.BackgroundColor = ConsoleColor.Blue; 
     Console.ForegroundColor = ConsoleColor.White; 
     Console.WriteLine("White on blue."); 
     Console.WriteLine("Another line."); 

같이 입력 쉽게 무언가에 대해 알고 있다면 내가 당신을 부탁 해요 이유입니다. 내가 색을 바꾼다는 단순한 요청이 모두 흰색으로 돌아 가기를 바랄뿐입니다.

답변

1

이 기능을 사용해보십시오. 그런 다음

class ConsoleHelper 
{   
    public static void Write(string message, ConsoleColor foreColor, ConsoleColor backColor) 
    { 
     Console.ForegroundColor = foreColor; 
     Console.BackgroundColor = backColor; 
     Console.Write(message); 
     Console.ResetColor(); 
    } 

    public static void WriteLine(string message, ConsoleColor foreColor, ConsoleColor backColor) 
    { 
     Write(message + Environment.NewLine, foreColor, backColor); 
    } 
} 

메인 프로그램에서, 당신은 같은 것을 수행 할 수 있습니다 : 그럼, 나는 보통 나 색상을 사용자 정의 할 것입니다 WriteWriteLine 방법이 헬퍼 클래스를 만들

private static void Main() 
{ 
    Console.Write("If the text is "); 
    ConsoleHelper.Write("green", ConsoleColor.Green, ConsoleColor.Black); 
    Console.WriteLine(" then it's safe to proceed."); 

    Console.Write("\nIf the text is "); 
    ConsoleHelper.Write("yellow", ConsoleColor.Yellow, ConsoleColor.Black); 
    Console.Write(" or "); 
    ConsoleHelper.Write("highlighted yellow", ConsoleColor.White, ConsoleColor.DarkYellow); 
    Console.WriteLine(" then proceed with caution."); 

    Console.WriteLine("\nDone!\nPress any key to exit..."); 
    Console.ReadKey(); 
} 

을 어느 보이는 같은 :

enter image description here

또는 귀하의 예제와 같이

ConsoleHelper.WriteLine("White on blue.", ConsoleColor.White, ConsoleColor.Blue); 
Console.WriteLine("Another line."); 

는 생산 :

enter image description here

+0

굉장! 그것은 Utils를위한 제 코드에서 정말 잘 맞습니다. – WhiteGlove

0

당신은 다시 기본 색상에 콘솔을 다시 Console.ResetColor()을 사용할 수 있습니다

static void WriteConsoleAndRestore(string text, ConsoleColor background, ConsoleColor foreground) 
    { 
     ConsoleColor currentBackground = Console.BackgroundColor; 
     ConsoleColor currentForeground = Console.ForegroundColor; 
     Console.BackgroundColor = background; 
     Console.ForegroundColor = foreground; 
     Console.WriteLine(text); 
     Console.BackgroundColor = currentBackground; 
     Console.ForegroundColor = currentForeground; 
    } 
+0

배경 및 전경 대소 문자를 구분? – WhiteGlove

+0

그것은 열거 자입니다. 당신은 거기있는 것을 선택할 수 있습니다. 그것은 문자열이 아니에요. ConsoleColor - https://msdn.microsoft.com/en-us/library/system.consolecolor(v=vs.110).aspx – Ctznkane525