我正在用c#编写一个小型控制台应用程序,我想用箭头键浏览我的菜单。为了使用户可以看到所选的选项,我想用白色背景和黑色文本突出显示它,但只有单词而不是整行。
我试图将光标定位在单词的开头,因此只有单词会突出显示但不起作用。有人可以把我放在正确的方向吗?
for (int i = 0; i < filesArray.Length; i++)
{
Console.WriteLine();
if (i == 0)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Program.WriteAtTheMiddleOfTheScreen(filesArray[0]);
Console.ResetColor();
}
else
{
Program.WriteAtTheMiddleOfTheScreen(filesArray[i]);
}
}
Console.WriteLine();
Program.WriteAtTheMiddleOfTheScreen("Exit Program");
Console.SetCursorPosition((Console.WindowWidth/2)-filesArray[0].Length+2, 2);
That is what I want to achieve
public static void WriteAtTheMiddleOfTheScreen(string message)
{
message = String.Format("{0," + ((Console.WindowWidth / 2) + (message.Length / 2)) + "}", message);
Console.WriteLine(message);
}
答案 0 :(得分:2)
ForegroundColor
类支持两个名为BackgroundColor
和Console
的属性。
请参阅library cn1-css和MSDN: System.Console.ForegroundColor
(提示:使用Microsoft Visual Studio,您可以将光标放在Console
上,然后按Ctrl + Space以获取System.ConsoleColor
提供的所有属性的列表。)
您可以使用White
枚举的成员设置这些属性,其中包含Blue
,BackgroundColor
等成员。
请参阅MSDN: System.Console.BackgroundColor
因此,您将System.Console.Write()
属性设置为某种颜色,执行BackgroundColor
,然后将BackgroundColor
设置为其他颜色,依此类推。
请记住,空格是使用背景颜色绘制的,因此为了防止屏幕区域被涂上不需要的背景颜色,请不要将POST
属性设置为不需要的值并在那里写入空格。 / p>
答案 1 :(得分:0)
C# Console Color Tutorial 此页面提供了更改控制台应用程序颜色所需的一切。
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
以上代码将更改控制台的背景和前景色。
答案 2 :(得分:0)
这将有助于
for (int i = 0; i < 100; i++)
{
Console.WriteLine();
string Text = "MAP" + i;
Console.SetCursorPosition((Console.WindowWidth - Text.Length) / 2, Console.CursorTop);
if (i == 0)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(Text);
Console.ResetColor();
}
else
{
Console.WriteLine(Text);
}
}