2010-08-08 39 views
3

我正在创建一个控制台应用程序,我希望有两个输出和一个输入。原因是,一个输出始终可见。将控制台分成两部分用于两个输出

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

但我怎么会去两个部分分裂控制台的?它甚至有可能吗?

回答

4

如果我正确理解你,你可以使用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("> "); 

     } 

    } 
} 

我假设你想填充每个区域不仅用户输入?如果是这样,您需要在单独的线程中设置控制台的图形,并让该线程在需要时更新屏幕。

+0

这真的很整洁!这真的是我正在寻找的格式:)虽然,问题是,我已经有很多使用Console.Out编写的代码,所以我需要以某种方式重定向输出并手动输出它。需要一些修补:) +1虽然 – Default 2010-08-08 15:20:30

+0

这很好!需要注意的是 - 如果你正在写大量的控制台,Console.Clear()会导致闪烁。 – Onisemus 2012-04-13 21:06:53

0

标准Windows控制台不提供此类功能。你将不得不编写你自己的窗口来做到这一点。

1

如果我理解正确的话,这可能会帮助:

Console.WriteLine("Head"); 
Console.WriteLine("Message"); 
Console.ReadKey(); 
Console.SetCursorPosition(0, 1); 
Console.WriteLine("Message2"); 
0

独特的想法,但到目前为止,我还没有发现用普通的控制台应用程序类似的东西。

那么,什么阻止你使用表单应用程序并将程序分成两个不同的部分?

您可能想要尝试的另一种方法是输出到2个不同的控制台,这很可能是您不想要的。

+1

如果您想到ssh/putty,表单应用并不总是可行的。 (很好,你可以将X11转发和填充,但有时*只在服务器上提供一个提示,而不是其他任何东西) – Patrick 2010-08-11 11:45:28