【问题标题】:How to Highlight and select Console Text in C# console如何在 C# 控制台中突出显示和选择控制台文本
【发布时间】:2014-04-03 15:04:40
【问题描述】:

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

【问题讨论】:

    标签: c# user-interface console selection highlight


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      我认为你将能够使用

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-04
        • 1970-01-01
        相关资源
        最近更新 更多