Results 1 to 4 of 4

Thread: Clearing the console window in c#

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    long island, new york
    Posts
    32

    Clearing the console window in c#

    how do you clear the console window in c#?

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Dead easy (if you already know the code, otherwise it a tough one).

    I have the code at work and I'll post it here tomorrow, but if you cant wait that long, go to

    http://support.microsoft.com/default...b;en-us;319257

    and use the code they have there. It works fine.
    I don't live here any more.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Sorry, I completely forgot.

    Paste this into a new class file and create an instance of it from your main program then call its Clear() method.

    Code:
    //from Microsoft website
    
    using System;
    using System.Runtime.InteropServices;
    
    namespace AWEngLib
    {
    	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() //Constructor
    		{
    			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);
    		}
    	}
    }
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width