2010-08-08 37 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