DOUBT IN CONSOLE APPLICATION.
HOW TO CLEAR THE SCREEN IN WHILE DOING C# PROGRAMS ON CONSOLE APPLICATION?IN C,WE WE USING clrscr() FUNCTION.HOW TO IMPLEMENT THE SAME IN C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
tim dinhPosted Oct 27, 2005, 3:07 AM
csharphelpPosted Oct 26, 2005, 5:56 AM
using System;
using nsClearConsole;
namespace ConsoleApplication1
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
ClearConsole ClearMyConsole = new ClearConsole();
Console.WriteLine("THIS IS FIRST LINE"); // Some text
Console.WriteLine("THIS IS SECOND LINE"); // Some text
Console.WriteLine("THIS IS THIRD LINE"); // Some text
Console.WriteLine("THIS IS FOURTH LINE"); // Some text
Console.WriteLine("THIS IS FIFTH LINE"); // Some text
Console.WriteLine("Hit Enter to Clear"); // Some text
Console.ReadLine(); // Wait for user input
ClearMyConsole.Clear(); // Clear the screen
Console.WriteLine("THE CONSOLE WAS CLEARED"); // Some text to clear console
Console.WriteLine("Hit Enter to Terminate"); //Some text
Console.ReadLine(); // Wait for user input
}
}
}
and this is the class that contain the nsClearConsole namespace ::
using System;
using System.Runtime.InteropServices;
namespace nsClearConsole
{
///
/// Summary description for ClearConsole.
///
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()
{
//
// TODO: Add constructor logic here.
//
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);
}
}
}
SolitairePosted Oct 15, 2005, 7:32 PM
They promised to add that capability to VS 2005, but don't know if they did.