Pregunta

I wish to create an old style terminal emulation, creating commands seems to be easy, but I want to give the user a very vintage UI interface.
I want to be able to print text to the console, Ex: "Logs" and then when the user presses an arrow key, I want it to be highlighted and once it is selected I would like to be able to hit enter and execute the selected command.
I am using Visual Studio Express 2012 for Desktop btw.

¿Fue útil?

Solución

I think you will have to rewrite the lines you already put on the screen to change their color and background as a reponse to arrow inputs

I think you will be able to use

Console.SetCursorPosition

to put your cursor back on the line you want to change color and then

Console.BackgroundColor
Console.ForegroundColor
Console.ResetColor()

to modify the colors of what you are writing

So basically you need to

  • clear the screen when you start up to know the positions of each option
  • respond to magic keypresses
  • rewrite the color/background of the item your magic keypress highlights

Remember to set the cursor back to its original position after you rewrite the highlighted part. This is a crude sample to show what I mean.

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

I think it might be easier to create a routine that prints all the necessary text on-screen and rewrite the entire text each time the user presses a magic key, highlighting as you go.

Otros consejos

I was looking for the same thing, so I ended up making it up myself I emulated the highlighting by inverting the color of the foreground and background.

It uses the arrow keys to navigate the options, cursor is also set to not visible to make it look nice.

    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);
            }
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top