2013-10-24 67 views
1

我在控制檯窗口中多次寫入。隱藏控制檯中的滾動條而不閃爍

我想出如何刪除滾動條:

Console.BufferWidth = Console.WindowWidth = 35; 
Console.BufferHeight = Console.WindowHeight; 

到目前爲止,它的罰款。但是當我想寫在一行的最後一列時,它會添加一個新行!這是合乎邏輯的,但如何避免這種情況?

我試圖調整控制檯:

Console.BufferWidth++; 
Console.BufferHeight++; 

// Write to the last column of a line 

Console.BufferWidth--; 
Console.BufferHeight--; 

但這閃爍,因爲這些線路將在第二獲得多次執行!

任何想法或我將不得不與滾動條?

回答

2

我使用本地方法使用繪圖進行管理。

#region Native methods 

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
static extern SafeFileHandle CreateFile(
    string fileName, 
    [MarshalAs(UnmanagedType.U4)] uint fileAccess, 
    [MarshalAs(UnmanagedType.U4)] uint fileShare, 
    IntPtr securityAttributes, 
    [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, 
    [MarshalAs(UnmanagedType.U4)] int flags, 
    IntPtr template); 

[StructLayout(LayoutKind.Sequential)] 
public struct Coord 
{ 
    public short X; 
    public short Y; 

    public Coord(short X, short Y) 
    { 
     this.X = X; 
     this.Y = Y; 
    } 
}; 

[DllImport("kernel32.dll", SetLastError = true)] 
    static extern bool WriteConsoleOutputCharacter(
    SafeFileHandle hConsoleOutput, 
    string lpCharacter, 
    int nLength, 
    Coord dwWriteCoord, 
    ref int lpumberOfCharsWritten); 

#endregion 


public static void Draw(int x, int y, char renderingChar) 
{ 
    // The handle to the output buffer of the console 
    SafeFileHandle consoleHandle = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); 

    // Draw with this native method because this method does NOT move the cursor. 
    int n = 0; 
    WriteConsoleOutputCharacter(consoleHandle, renderingChar.ToString(), 1, new Coord((short)x, (short)y), ref n); 
} 

WriteConsoleOutputCharacter不移動光標。因此,即使在繪製最後一行的最後一列時,光標也不會跳到下一行(窗口大小之外)並破壞視圖。

+0

調用方法'Draw(int x,int y,char renderingChar)'? – GooliveR

6

試一試Console.SetBufferSize(width,height);我認爲這會有所幫助。

+0

這很好。 –

1

問題不是光標直接進入下一行。它真正做的是,它向右移動一個字符,並且因爲緩衝區結束了,它會進入下一行。所以我試圖做一些像Console.WriteLine(「C \ r」);要設置光標回來,但它仍然閃爍

比我想過使用一些偷偷摸摸的技巧和使用阿拉伯語從右到左標記,但沒有奏效。

,所以我可以想出最好的辦法是字符與移動到右下角Console.MoveBufferArea方法,因爲這並不光標移動到下一個合適的位置,避免了新行