How to clear the Console window with Visual C++ .NET or Visual C++ 2005

Article translations Article translations
Article ID: 816180 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to clear the Console window programmatically by using Microsoft Visual C++ .NET.

Sample Program

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Managed C++ Apllication in Microsoft Visual Studio .NET 2002, create a new Console Application (.NET) in Microsoft Visual Studio .NET 2003, or create a new CLR Console Application in Visual Studio 2005.
  3. In the Name box, type TestProject, and then click OK.
  4. On the Project menu, click Add New Item.
  5. In the Add New Item dialog box, select Header File (.h) in the right pane, type the name as nsClearConsole, and then click Open.

    Note In Visual Studio 2005, click Add.
  6. Paste the following code in the nsClearConsole.h file:
    #pragma once
    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::Runtime::InteropServices;
    
    namespace nsClearConsole
    {
    	public __gc class ClearConsole
    	{		
    	private:
    		static int STD_OUTPUT_HANDLE  = -11;
    		static unsigned char EMPTY = 32;
    
    		[StructLayout(LayoutKind::Sequential)]
    		__value struct COORD
    		{
    		public:
    			short x;
    			short y;
    		};
    		
    		[StructLayout(LayoutKind::Sequential)]
    		__value struct SMALL_RECT
    		{
    		public:
    			short Left;
    			short Top;
    			short Right;
    			short Bottom;
    		};
    		
    		[StructLayout(LayoutKind::Sequential)]
    		__value struct CONSOLE_SCREEN_BUFFER_INFO
    		{
    		public:
    			COORD dwSize;
    			COORD dwCursorPosition;
    			int wAttributes;
    			SMALL_RECT srWindow;
    			COORD dwMaximumWindowSize;
    		};
    		
    
    		[DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
    		static int GetStdHandle(int nStdHandle);
    
    		[DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
    		static int FillConsoleOutputCharacter(int hConsoleOutput, Byte cCharacter, int nLength, COORD dwWriteCoord, int &lpNumberOfCharsWritten);
    		
    		[DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
    		static int GetConsoleScreenBufferInfo(int hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO *lpConsoleScreenBufferInfo);
    
    		[DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
    		static 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;
    			COORD Home;
    
    			GetConsoleScreenBufferInfo(hConsoleHandle, &strConsoleInfo);
    			FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, hWrittenChars);
    			SetConsoleCursorPosition(hConsoleHandle, Home);
    		}
    	};
    }
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
  7. Add the following header file declaration in the TestProject.cpp file:
    #include "nsClearConsole.h"
  8. Paste the following statement before the main function:
    using namespace nsClearConsole;
  9. Replace the existing code with the following code in the main function of the Console application:
    ClearConsole *ClearMyConsole = new ClearConsole();
    	Console::WriteLine(S"This is the first line.");  // Write some text.
    	Console::WriteLine(S"This is the second line."); // Write some text. 
    	Console::WriteLine(S"This is the third line.");  // Write some text.
    	Console::WriteLine(S"This is the fourth line."); // Write some text.  
    	Console::WriteLine(S"This is the fifth line.");  // Write some text.
    	Console::WriteLine(S"Press ENTER to clear.");  // Write some text.
    	Console::ReadLine();  // Wait for user input.
    
    	ClearMyConsole->Clear(); // Clear the screen.
    
    	Console::WriteLine(S"The console was cleared.");  // Write some text to clear the console.
    	Console::WriteLine(S"Press ENTER to quit. ");  // Write some text.
    	Console::ReadLine();  // Wait for user input.
    	return 0;
  10. Press CTRL+F5 to build and to run the application. Notice that the text in the Console window is cleared.

REFERENCES

For more information about console functions, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms682073.aspx

Properties

Article ID: 816180 - Last Review: November 26, 2007 - Revision: 3.2
APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB816180

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com