如何在C#控制台中突出显示并选择控制台文本

时间:2014-03-02 19:03:44

标签: c# user-interface console selection highlight

我希望创建一个旧式终端仿真,创建命令似乎很容易,但我想给用户一个非常复古的UI界面。我希望能够将文本打印到控制台,例如:“日志”,然后当用户按下箭头键时,我希望它被突出显示,一旦被选中,我希望能够输入并执行选定的命令。我正在使用Visual Studio Express 2012 for Desktop btw。

2 个答案:

答案 0 :(得分:3)

我认为你必须重写你已经放在屏幕上的线条来改变它们的颜色和背景作为箭头输入的响应

我认为你可以使用

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

我认为创建一个例程可以更容易地在屏幕上打印所有必要的文本,并在每次用户按下魔术键时重写整个文本,然后突出显示。

答案 1 :(得分:0)

我一直在寻找相同的东西,所以最终我自己做了,我通过反转前景和背景的颜色来模拟突出显示。

它使用箭头键浏览选项,光标也设置为不可见,以使其看起来不错。

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