C# 将控制台分为两部分以获得两个输出
我正在创建一个控制台应用程序,其中我希望有两个输出和一个输入。原因是一个输出总是可见的C# 将控制台分为两部分以获得两个输出,c#,redirect,console,io,C#,Redirect,Console,Io,我正在创建一个控制台应用程序,其中我希望有两个输出和一个输入。原因是一个输出总是可见的 This is the first output Text flows upwards just like a regular console application, however... --------- This is a second output This is placed at the bottom of the console // also input goes here. 我想把它叫
This is the first output
Text flows upwards just like a regular console application, however...
---------
This is a second output
This is placed at the bottom of the console // also input goes here.
我想把它叫做这样的东西
Console.Out.Writeline("This is the first output");
Console.Out.Writeline("Text flows upwards just like a regular console application, however...");
MyTextWriter.WriteLine("This is a second output");
MyTextWriter.WriteLine("This is placed at the bottom of the console");
但是我如何将控制台分成两部分呢?甚至可能吗?标准Windows控制台不提供任何此类功能。您必须编写自己的窗口来完成此操作。如果我理解正确,这可能会有帮助:
Console.WriteLine("Head");
Console.WriteLine("Message");
Console.ReadKey();
Console.SetCursorPosition(0, 1);
Console.WriteLine("Message2");
这个想法很独特,但到目前为止,我还没有注意到使用普通控制台应用程序会出现类似的情况 那么,是什么阻止您使用表单应用程序并将程序分为两个不同的部分呢
您可能希望尝试的另一种方法是将数据输出到两个不同的控制台中,这可能是您不想要的。如果我理解正确,您可以使用console.SetCursorPosition在需要的位置绘制文本。下面是一个粗略的示例,它将控制台分成两个区域,添加文本时文本向上流动
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static List<string> area1 = new List<string>();
static List<string> area2 = new List<string>();
static int areaHeights = 0;
static void Main(string[] args)
{
// Number of rows for each area
areaHeights = (Console.WindowHeight - 2) / 2;
drawScreen();
int i = 0;
while (true)
{
i++;
// jumb between areas
if (i % 2 == 0)
AddLineToBuffer(ref area1, Console.ReadLine());
else
AddLineToBuffer(ref area2, Console.ReadLine());
drawScreen();
}
}
private static void AddLineToBuffer(ref List<string> areaBuffer, string line)
{
areaBuffer.Insert(0, line);
if (areaBuffer.Count == areaHeights)
{
areaBuffer.RemoveAt(areaHeights - 1);
}
}
private static void drawScreen()
{
Console.Clear();
// Draw the area divider
for (int i = 0; i < Console.BufferWidth; i++)
{
Console.SetCursorPosition(i, areaHeights);
Console.Write('=');
}
int currentLine = areaHeights - 1;
for (int i = 0; i < area1.Count; i++)
{
Console.SetCursorPosition(0, currentLine - (i + 1));
Console.WriteLine(area1[i]);
}
currentLine = (areaHeights * 2);
for(int i = 0; i < area2.Count; i++)
{
Console.SetCursorPosition(0, currentLine - (i + 1));
Console.WriteLine(area2[i]);
}
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write("> ");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态列表区域1=新列表();
静态列表区域2=新列表();
静态面积高度=0;
静态void Main(字符串[]参数)
{
//每个区域的行数
区域高度=(控制台窗口高度-2)/2;
纱窗();
int i=0;
while(true)
{
i++;
//地区之间的混乱
如果(i%2==0)
AddLineToBuffer(参考区域1,Console.ReadLine());
其他的
AddLineToBuffer(参考区域2,Console.ReadLine());
纱窗();
}
}
专用静态无效AddLineToBuffer(参考列表areaBuffer,字符串行)
{
区域缓冲区。插入(0,行);
如果(areaBuffer.Count==areaHeights)
{
区域缓冲区。移除(区域高度-1);
}
}
私有静态void drawScreen()
{
Console.Clear();
//绘制面积分割线
对于(int i=0;i”;
}
}
}
我假设你想用用户输入填充每个区域?如果是这样,您需要在单独的线程中设置控制台的绘图,并在需要时让该线程更新屏幕。这真是太好了!这确实是我想要的格式:)虽然,问题是,我已经有很多代码使用Console.Out来编写,所以我需要以某种方式重定向输出,并像这样手动输出。需要一些修补:)+1个想法真是太好了!但是需要注意的是,如果您对控制台进行大量写入,console.Clear()会导致闪烁。例如,如果您考虑ssh/putty,表单应用程序并不总是可能的。(很好,您可以使用X11转发之类的方式,但有时服务器上只提供一个提示,其他什么都不提供)