[Mono-list] Howto clear the console

Ian MacLean ianm@ActiveState.com
Mon, 30 Aug 2004 12:06:54 +0900


Timothy Parez wrote:

>Hello,
> 
>Under windows we have to use interop in order to clear the console (why on
>earth didn't they implement that in the console class????)
>  
>
they did - just in version 2 of the framework. See
http://www.dotnet2themax.com/DotNetBrowser/ShowType.aspx?asm=mscorlib&ns=System&type=Console

for a list of the new console related functions.

>Anyway how can I do this on linux, or better yet, platform independant.
> 
>  
>
I think you'll probably need to P/Invoke to ncurses to do this on *nix

Ian

> 
>using System;
>
>using nsClearConsole;
>
>using System.Runtime.InteropServices;
>
>namespace ConsoleApplication1
>
>{
>
>class Class1
>
>{
>
>[STAThread]
>
>static void Main(string[] args)
>
>{
>
>Console.WriteLine("Hello");
>
>ClearConsole cs = new ClearConsole();
>
>Console.ReadLine();
>
>cs.Clear();
>
>Console.WriteLine("clear2");
>
>Console.ReadLine();
>
>}
>
>}
>
>}
>
> 
>
> 
>
>namespace nsClearConsole
>
>{
>
>public class ClearConsole
>
>{ 
>
>private const int STD_OUTPUT_HANDLE = -11;
>
>private const byte EMPTY = 32;
>
>[StructLayout(LayoutKind.Sequential)]
>
>struct COORD
>
>{
>
>public short x;
>
>public short y;
>
>}
>
>[StructLayout(LayoutKind.Sequential)]
>
>struct SMALL_RECT
>
>{
>
>public short Left;
>
>public short Top;
>
>public short Right;
>
>public short Bottom;
>
>}
>
>[StructLayout(LayoutKind.Sequential)]
>
>struct CONSOLE_SCREEN_BUFFER_INFO
>
>{
>
>public COORD dwSize;
>
>public COORD dwCursorPosition;
>
>public int wAttributes;
>
>public SMALL_RECT srWindow;
>
>public COORD dwMaximumWindowSize;
>
>}
>
>[DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true,
>CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
>
>private static extern int GetStdHandle(int nStdHandle);
>
>[DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter",
>SetLastError=true, CharSet=CharSet.Auto,
>CallingConvention=CallingConvention.StdCall)]
>
>private static extern int FillConsoleOutputCharacter(int hConsoleOutput,
>byte cCharacter, int nLength, COORD dwWriteCoord, ref int
>lpNumberOfCharsWritten);
>
>[DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo",
>SetLastError=true, CharSet=CharSet.Auto,
>CallingConvention=CallingConvention.StdCall)]
>
>private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref
>CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
>
>[DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition",
>SetLastError=true, CharSet=CharSet.Auto,
>CallingConvention=CallingConvention.StdCall)]
>
>private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD
>dwCursorPosition);
>
>private int hConsoleHandle;
>
>public ClearConsole()
>
>{
>
>hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
>
>}
>
>public void Clear()
>
>{
>
>int hWrittenChars = 0;
>
>CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new
>CONSOLE_SCREEN_BUFFER_INFO(); 
>
>COORD Home; 
>
>Home.x = Home.y = 0;
>
>GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
>
>FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x *
>strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
>
>SetConsoleCursorPosition(hConsoleHandle, Home);
>
>}
>
>}
>
>}
>
>
>---
>Outgoing mail is certified Virus Free.
>Checked by AVG anti-virus system (http://www.grisoft.com).
>Version: 6.0.745 / Virus Database: 497 - Release Date: 27/08/2004
> 
>
>  
>


-- 
Ian MacLean, Developer, 
ActiveState, a division of Sophos
http://www.ActiveState.com