[Mono-list] Howto clear the console

Timothy Parez tpsoftware@users.sourceforge.net
Mon, 30 Aug 2004 00:17:22 +0200


This is a multi-part message in MIME format.

------=_NextPart_000_0009_01C48E26.BB858E10
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Hello,
=20
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????)
Anyway how can I do this on linux, or better yet, platform independant.
=20
=20
using System;

using nsClearConsole;

using System.Runtime.InteropServices;

namespace ConsoleApplication1

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

Console.WriteLine("Hello");

ClearConsole cs =3D new ClearConsole();

Console.ReadLine();

cs.Clear();

Console.WriteLine("clear2");

Console.ReadLine();

}

}

}

=20

=20

namespace nsClearConsole

{

public class ClearConsole

{=20

private const int STD_OUTPUT_HANDLE =3D -11;

private const byte EMPTY =3D 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=3D"GetStdHandle", =
SetLastError=3Dtrue,
CharSet=3DCharSet.Auto, CallingConvention=3DCallingConvention.StdCall)]

private static extern int GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", EntryPoint=3D"FillConsoleOutputCharacter",
SetLastError=3Dtrue, CharSet=3DCharSet.Auto,
CallingConvention=3DCallingConvention.StdCall)]

private static extern int FillConsoleOutputCharacter(int hConsoleOutput,
byte cCharacter, int nLength, COORD dwWriteCoord, ref int
lpNumberOfCharsWritten);

[DllImport("kernel32.dll", EntryPoint=3D"GetConsoleScreenBufferInfo",
SetLastError=3Dtrue, CharSet=3DCharSet.Auto,
CallingConvention=3DCallingConvention.StdCall)]

private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, =
ref
CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

[DllImport("kernel32.dll", EntryPoint=3D"SetConsoleCursorPosition",
SetLastError=3Dtrue, CharSet=3DCharSet.Auto,
CallingConvention=3DCallingConvention.StdCall)]

private static extern int SetConsoleCursorPosition(int hConsoleOutput, =
COORD
dwCursorPosition);

private int hConsoleHandle;

public ClearConsole()

{

hConsoleHandle =3D GetStdHandle(STD_OUTPUT_HANDLE);

}

public void Clear()

{

int hWrittenChars =3D 0;

CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo =3D new
CONSOLE_SCREEN_BUFFER_INFO();=20

COORD Home;=20

Home.x =3D Home.y =3D 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
=20

------=_NextPart_000_0009_01C48E26.BB858E10
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3DWindows-1252">
<TITLE>Message</TITLE>

<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR></HEAD>
<BODY>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial=20
size=3D2>Hello,</FONT></SPAN></DIV>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial size=3D2>Under =
windows we=20
have to use interop in order to clear the console (why on earth didn't =
they=20
implement that in the console class????)</FONT></SPAN></DIV>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial size=3D2>Anyway =
how can I do=20
this on linux, or better yet, platform independant.</FONT></SPAN></DIV>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D921331622-29082004><FONT face=3DArial=20
size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D921331622-29082004><FONT color=3D#0000ff size=3D2>
<P>using</FONT><FONT size=3D2> System;</P></FONT><FONT color=3D#0000ff =
size=3D2>
<P>using</FONT><FONT size=3D2> nsClearConsole;</P></FONT><FONT =
color=3D#0000ff=20
size=3D2>
<P>using</FONT><FONT size=3D2> =
System.Runtime.InteropServices;</P></FONT><FONT=20
color=3D#0000ff size=3D2>
<P>namespace</FONT><FONT size=3D2> ConsoleApplication1</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>class</FONT><FONT size=3D2> =
Class1</P>
<P>{</P>
<P>[STAThread]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>static</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>void</FONT><FONT size=3D2> Main(</FONT><FONT =
color=3D#0000ff=20
size=3D2>string</FONT><FONT size=3D2>[] args)</P>
<P>{</P>
<P>Console.WriteLine("Hello");</P>
<P>ClearConsole cs =3D </FONT><FONT color=3D#0000ff =
size=3D2>new</FONT><FONT size=3D2>=20
ClearConsole();</P>
<P>Console.ReadLine();</P>
<P>cs.Clear();</P>
<P>Console.WriteLine("clear2");</P>
<P>Console.ReadLine();</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P>&nbsp;</P>
<P>&nbsp;</P></FONT><FONT color=3D#0000ff size=3D2>
<P>namespace</FONT><FONT size=3D2> nsClearConsole</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>class</FONT><FONT size=3D2> ClearConsole</P>
<P>{ </P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>const</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> STD_OUTPUT_HANDLE =3D -11;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>const</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>byte</FONT><FONT size=3D2> EMPTY =3D 32;</P>
<P>[StructLayout(LayoutKind.Sequential)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>struct</FONT><FONT size=3D2> =
COORD</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> x;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> y;</P>
<P>}</P>
<P>[StructLayout(LayoutKind.Sequential)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>struct</FONT><FONT size=3D2> =
SMALL_RECT</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> Left;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> Top;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> Right;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>short</FONT><FONT size=3D2> Bottom;</P>
<P>}</P>
<P></P>
<P>[StructLayout(LayoutKind.Sequential)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>struct</FONT><FONT size=3D2>=20
CONSOLE_SCREEN_BUFFER_INFO</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
COORD=20
dwSize;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
COORD=20
dwCursorPosition;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> wAttributes;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
SMALL_RECT=20
srWindow;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
COORD=20
dwMaximumWindowSize;</P>
<P>}</P>
<P>[DllImport("kernel32.dll", EntryPoint=3D"GetStdHandle",=20
SetLastError=3D</FONT><FONT color=3D#0000ff size=3D2>true</FONT><FONT =
size=3D2>,=20
CharSet=3DCharSet.Auto, =
CallingConvention=3DCallingConvention.StdCall)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>static</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>extern</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> GetStdHandle(</FONT><FONT =
color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> nStdHandle);</P>
<P>[DllImport("kernel32.dll", EntryPoint=3D"FillConsoleOutputCharacter", =

SetLastError=3D</FONT><FONT color=3D#0000ff size=3D2>true</FONT><FONT =
size=3D2>,=20
CharSet=3DCharSet.Auto, =
CallingConvention=3DCallingConvention.StdCall)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>static</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>extern</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> =
FillConsoleOutputCharacter(</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> hConsoleOutput, =
</FONT><FONT=20
color=3D#0000ff size=3D2>byte</FONT><FONT size=3D2> cCharacter, =
</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> nLength, COORD =
dwWriteCoord,=20
</FONT><FONT color=3D#0000ff size=3D2>ref</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> =
lpNumberOfCharsWritten);</P>
<P>[DllImport("kernel32.dll", EntryPoint=3D"GetConsoleScreenBufferInfo", =

SetLastError=3D</FONT><FONT color=3D#0000ff size=3D2>true</FONT><FONT =
size=3D2>,=20
CharSet=3DCharSet.Auto, =
CallingConvention=3DCallingConvention.StdCall)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>static</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>extern</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> =
GetConsoleScreenBufferInfo(</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> hConsoleOutput, =
</FONT><FONT=20
color=3D#0000ff size=3D2>ref</FONT><FONT size=3D2> =
CONSOLE_SCREEN_BUFFER_INFO=20
lpConsoleScreenBufferInfo);</P>
<P>[DllImport("kernel32.dll", EntryPoint=3D"SetConsoleCursorPosition",=20
SetLastError=3D</FONT><FONT color=3D#0000ff size=3D2>true</FONT><FONT =
size=3D2>,=20
CharSet=3DCharSet.Auto, =
CallingConvention=3DCallingConvention.StdCall)]</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>static</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>extern</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>int</FONT><FONT size=3D2> SetConsoleCursorPosition(</FONT><FONT =

color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> hConsoleOutput, COORD =

dwCursorPosition);</P>
<P></FONT><FONT color=3D#0000ff size=3D2>private</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> hConsoleHandle;</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2>=20
ClearConsole()</P>
<P>{</P>
<P>hConsoleHandle =3D GetStdHandle(STD_OUTPUT_HANDLE);</P>
<P>}</P>
<P></FONT><FONT color=3D#0000ff size=3D2>public</FONT><FONT size=3D2> =
</FONT><FONT=20
color=3D#0000ff size=3D2>void</FONT><FONT size=3D2> Clear()</P>
<P>{</P>
<P></FONT><FONT color=3D#0000ff size=3D2>int</FONT><FONT size=3D2> =
hWrittenChars =3D=20
0;</P>
<P>CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>new</FONT><FONT size=3D2> CONSOLE_SCREEN_BUFFER_INFO(); </P>
<P>COORD Home; </P>
<P>Home.x =3D Home.y =3D 0;</P>
<P>GetConsoleScreenBufferInfo(hConsoleHandle, </FONT><FONT =
color=3D#0000ff=20
size=3D2>ref</FONT><FONT size=3D2> strConsoleInfo);</P>
<P>FillConsoleOutputCharacter(hConsoleHandle, EMPTY, =
strConsoleInfo.dwSize.x *=20
strConsoleInfo.dwSize.y, Home, </FONT><FONT color=3D#0000ff =
size=3D2>ref</FONT><FONT=20
size=3D2> hWrittenChars);</P>
<P>SetConsoleCursorPosition(hConsoleHandle, Home);</P>
<P>}</P>
<P>}</P>
<P>}</P></FONT></SPAN></DIV></BODY></HTML>
<BR>

<P><FONT SIZE=3D2>---<BR>
Outgoing mail is certified Virus Free.<BR>
Checked by AVG anti-virus system (http://www.grisoft.com).<BR>
Version: 6.0.745 / Virus Database: 497 - Release Date: 27/08/2004<BR>
</FONT> </P>

------=_NextPart_000_0009_01C48E26.BB858E10--