你似乎从来没有在程序中写入控制台,这显然是你需要做的。除此之外,你还需要一种在运行时使用setters或类似函数来更改颜色的方法
ChangeColors()
。
这里有一个工作示例程序供参考:
namespace MyProgram;
class Program
{
static void Main(string[] args)
{
var coloredPrinter = new ColoredPrinter(ConsoleColor.White, ConsoleColor.Blue);
coloredPrinter.WriteLine("This is white text on blue background");
coloredPrinter.ChangeColors(ConsoleColor.Yellow, ConsoleColor.Red);
coloredPrinter.WriteLine("This is yellow text on red background");
Console.WriteLine("This is the default");
}
}
class ColoredPrinter
{
public ConsoleColor ForegroundColor { get; set; }
public ConsoleColor BackgroundColor { get; set; }
public ColoredPrinter(ConsoleColor foregroundColor, ConsoleColor backgroundColor)
{
ChangeColors(foregroundColor, backgroundColor);
}
public void ChangeColors(ConsoleColor foregroundColor, ConsoleColor backgroundColor)
{
ForegroundColor = foregroundColor;
BackgroundColor = backgroundColor;
}
public void WriteLine(string text)
{
Console.ResetColor();
Console.BackgroundColor = BackgroundColor;
Console.ForegroundColor = ForegroundColor;
Console.WriteLine(text);
Console.ResetColor();
}
}
这会打印出以下内容: