简体   繁体   English

C#控制台-将光标位置设置为最后一个可见行

[英]C# Console - set cursor position to the last visible line

I would like to set the position of the cursor in the Console to the last visible line. 我想将光标在控制台中的位置设置为最后一个可见行。 How can I do this? 我怎样才能做到这一点?

Cheers, 干杯,

Pete 皮特

If you mean the last line of the window, you can use a mixture of Console.CursorTop , and Console.WindowHeight and Console.WindowTop . 如果您指的是窗口的最后一行,则可以混合使用Console.CursorTopConsole.WindowHeightConsole.WindowTop Sample code: 样例代码:

using System;

class Test
{
    static void Main()
    {
        Console.Write("Hello");
        WriteOnBottomLine("Bottom!");
        Console.WriteLine(" there");
    }

    static void WriteOnBottomLine(string text)
    {
        int x = Console.CursorLeft;
        int y = Console.CursorTop;
        Console.CursorTop = Console.WindowTop + Console.WindowHeight - 1;
        Console.Write(text);
        // Restore previous position
        Console.SetCursorPosition(x, y);
    }
}

Note that this has to take account of Console.WindowTop to find out where you are within the buffer... 请注意,这必须考虑Console.WindowTop才能找出您在缓冲区中的位置...

I also had to solve this problem and came out with this: 我还必须解决此问题,并得出以下结论:

public class Program
{
  static int id = 0 , idOld = 0, idSelected = -1;
  static string[] samples;

  public static void Main()
  {
    Console.BackgroundColor = ConsoleColor.DarkBlue;
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WindowWidth = 90;
    Console.WindowHeight = 36;
    Console.WindowTop = 5;
    Console.Title = "My Samples Application";
    Console.InputEncoding = Encoding.GetEncoding("windows-1251");

    samples = new string[50];
    for (int i = 0; i < samples.Length; i++)
      samples[i] = "Sample" + i;
    LoopSamples();
  }

  static void SelectRow(int y, bool select)
  {
    Console.CursorTop = y + 1;
    Console.ForegroundColor = select ? ConsoleColor.Red : ConsoleColor.Yellow;
    Console.WriteLine("\t{0}", samples[y]);
    Console.CursorTop = y;
  }

  static void LoopSamples()
  {
    int last = samples.Length - 1;
    ShowSamples();
    SelectRow(0, true);
    while (true)
    {
      while (idSelected == -1)
      {
        idOld = id;
        ConsoleKey key = Console.ReadKey(true).Key;
        switch (key)
        {
          case ConsoleKey.UpArrow:
          case ConsoleKey.LeftArrow: if (--id < 0) id = last; break;
          case ConsoleKey.DownArrow:
          case ConsoleKey.RightArrow: if (++id > last) id = 0; break;
          case ConsoleKey.Enter: idSelected = id; return;
          case ConsoleKey.Escape: return;
        }
        SelectRow(idOld, false);
        SelectRow(id, true);
      }
    }
  }

  static void ShowSamples()
  {
    Console.Clear();
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("Use arrow keys to select a sample. Then press 'Enter'. Esc - to Quit");
    for (int i = 0; i < samples.Length; i++)
      Console.WriteLine("\t{0}", samples[i]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM