实用程序的彩色文本

白手套
    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;

代替

Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("White on blue.");
        Console.WriteLine("Another line.");

这会将所有写入该颜色的文本更改。我所希望的只是简单地更改颜色,然后返回白色。

鲁弗斯

您可以用于Console.ResetColor()将控制台重置为默认颜色。然后,我通常创建一个具有WriteWriteLine方法的帮助程序类,这些类可以让我自定义颜色:

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);
    }
}

然后,在主程序中,您可以执行以下操作:

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();
}

看起来像:

在此处输入图片说明

或者,如您的示例所示:

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

产生:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章