我希望创建一个老式终端仿真,创建命令似乎很容易,但我想给用户提供一个非常古老的UI界面。
我希望能够向控制台打印文本,例如:“日志”,然后当用户按下箭头键时,我希望它被突出显示,一旦选择了它,我希望能够按回车并执行所选的命令。
顺便说一下,我正在使用Visual Studio Express 2012 for Desktop。
我希望创建一个老式终端仿真,创建命令似乎很容易,但我想给用户提供一个非常古老的UI界面。
我希望能够向控制台打印文本,例如:“日志”,然后当用户按下箭头键时,我希望它被突出显示,一旦选择了它,我希望能够按回车并执行所选的命令。
顺便说一下,我正在使用Visual Studio Express 2012 for Desktop。
我认为您需要重新编写已经显示在屏幕上的行,以便根据箭头输入更改它们的颜色和背景。
我认为您可以使用
Console.SetCursorPosition
将光标放回您想更改颜色的行,然后
Console.BackgroundColor
Console.ForegroundColor
Console.ResetColor()
修改你所写内容的颜色
基本上你需要:
请记得在重写高亮部分后将光标设置回原始位置。这只是一个简单的示例,用于说明我的意思。
Console.Clear();
Console.WriteLine("Option 1");
Console.WriteLine("Option 2");
Console.WriteLine("Option 3");
Console.WriteLine();
Console.Write("input: ");
var originalpos = Console.CursorTop;
var k = Console.ReadKey();
var i = 2;
while (k.KeyChar != 'q')
{
if (k.Key == ConsoleKey.UpArrow)
{
Console.SetCursorPosition(0, Console.CursorTop - i);
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine("Option " + (Console.CursorTop + 1));
Console.ResetColor();
i++;
}
Console.SetCursorPosition(8, originalpos);
k = Console.ReadKey();
}
class Program
{
static int win_W;
static int win_H;
static void Main(string[] args)
{
Boolean running = true;
win_W = Console.WindowWidth;
win_H = Console.WindowHeight;
Console.CursorVisible = false;
int slct = 0;
// int1 is posX, int2 is posY, string is the text you want to show as the option and boolean shows if its selected
List<Tuple<int, int, string, Boolean>> opts = new List<Tuple<int, int, string, Boolean>>
{
new Tuple<int, int, string, Boolean>((win_W/2)-4, (win_H / 2) - 5, "OPTION 1", true),
new Tuple<int, int, string, Boolean>((win_W/2)-4, (win_H / 2) - 4, "OPTION 2", false),
new Tuple<int, int, string, Boolean>((win_W/2)-4, (win_H / 2) - 3, "OPTION 3", false),
};
while (running == true)
{
foreach (Tuple<int,int, string, Boolean> tupe in opts)
{
if (tupe.Item4 == true)
{
//sets the variable 'slct' to be equal to the index of the tuple value with the true value
slct = opts.FindIndex(t => t.Item3 == tupe.Item3);
Console.SetCursorPosition(tupe.Item1, tupe.Item2);
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.Write(tupe.Item3);
}
else if (tupe.Item4 == false)
{
Console.SetCursorPosition(tupe.Item1, tupe.Item2);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(tupe.Item3);
}
}
//Weird glitch when you take this out
Console.SetCursorPosition(opts[2].Item1 + 1, opts[2].Item2);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("");
//A little question mark appears on the bottom option when you press escape
//And when you go from option 3 to option 2 it leaves one block to the right highlighted
string inp = Console.ReadKey().Key.ToString();
if (inp == "UpArrow" && slct > 0)
{
opts[slct] = new Tuple<int, int, string, bool>(opts[slct].Item1, opts[slct].Item2, opts[slct].Item3, false);
slct -= 1;
opts[slct] = new Tuple<int, int, string, bool>(opts[slct].Item1, opts[slct].Item2, opts[slct].Item3, true);
}
else if (inp == "DownArrow" && slct < 2)
{
opts[slct] = new Tuple<int, int, string, bool>(opts[slct].Item1, opts[slct].Item2, opts[slct].Item3, false);
slct += 1;
opts[slct] = new Tuple<int, int, string, bool>(opts[slct].Item1, opts[slct].Item2, opts[slct].Item3, true);
}
}
}
}