C#控制台-将光标位置设置为最后可见的行
我想将控制台中光标的位置设置为最后一行。我该怎么做 干杯C#控制台-将光标位置设置为最后可见的行,c#,console,printing,position,C#,Console,Printing,Position,我想将控制台中光标的位置设置为最后一行。我该怎么做 干杯 Pete如果您指的是窗口的最后一行,则可以混合使用控制台.CursorTop、控制台.WindowHeight和控制台.WindowTop。示例代码: using System; class Test { static void Main() { Console.Write("Hello"); WriteOnBottomLine("Bottom!"); Console.Wr
Pete如果您指的是窗口的最后一行,则可以混合使用
控制台.CursorTop
、控制台.WindowHeight和控制台.WindowTop
。示例代码:
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);
}
}
请注意,这必须考虑控制台。WindowTop以找出缓冲区中的位置…我还必须解决此问题,并得出以下结论:
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]);
}
}
公共类程序
{
静态int id=0,idOld=0,idSelected=-1;
静态字符串[]个样本;
公共静态void Main()
{
Console.BackgroundColor=ConsoleColor.DarkBlue;
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WindowWidth=90;
控制台高度=36;
Console.WindowTop=5;
Console.Title=“我的示例应用程序”;
Console.inpunecoding=Encoding.GetEncoding(“windows-1251”);
样本=新字符串[50];
for(int i=0;ilast)id=0,则中断;
case ConsoleKey.Enter:idSelected=id;return;
凯斯康塞基。逃脱:返回;
}
选择行(idOld,false);
选择行(id,true);
}
}
}
静态void ShowSamples()
{
Console.Clear();
Console.ForegroundColor=ConsoleColor.Yellow;
Console.WriteLine(“使用箭头键选择样本,然后按“回车”.Esc-退出”);
for(int i=0;i
Define“last visible line.”你是指最后一行文本(光标应自动显示)还是在可见窗口的底部,而不管存在什么文本?在可见窗口的底部,而不管已经存在什么文本。谢谢,我本打算写这篇文章,但我很高兴我用谷歌搜索了它。我可以建议您编辑函数,将光标向左移动=0吗?我假设任何需要写到屏幕底部的人可能也希望写到屏幕左侧。@Sidney:你是说Console.CursorLeft=Console.WindowLeft
?(目前我没有时间在各种配置中测试这一点。)