Microsoft KB Archive/816180

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

Article ID: 816180

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





For a Microsoft Visual C# .NET version of this article, see 319257.



For a Microsoft Visual Basic version of this article, see 319239.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System
 * System.Runtime.InteropServices

IN THIS TASK

 * SUMMARY
 * Sample Program
 * References



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

back to the top

Sample Program
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. 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. In the Name box, type TestProject, and then click OK. On the Project menu, click Add New Item. 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.  Paste the following code in the nsClearConsole.h file:
 * 1) pragma once
 * 2) 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(&quot;kernel32.dll&quot;, EntryPoint=&quot;GetStdHandle&quot;, SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)] static int GetStdHandle(int nStdHandle);

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;FillConsoleOutputCharacter&quot;, SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)] static int FillConsoleOutputCharacter(int hConsoleOutput, Byte cCharacter, int nLength, COORD dwWriteCoord, int &lpNumberOfCharsWritten); [DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;GetConsoleScreenBufferInfo&quot;, SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)] static int GetConsoleScreenBufferInfo(int hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO *lpConsoleScreenBufferInfo);

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;SetConsoleCursorPosition&quot;, 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li>  Add the following header file declaration in the TestProject.cpp file:  Paste the following statement before the main function: using namespace nsClearConsole; </li>  Replace the existing code with the following code in the main function of the Console application: ClearConsole *ClearMyConsole = new ClearConsole; Console::WriteLine(S&quot;This is the first line.&quot;); // Write some text. Console::WriteLine(S&quot;This is the second line.&quot;); // Write some text. Console::WriteLine(S&quot;This is the third line.&quot;); // Write some text. Console::WriteLine(S&quot;This is the fourth line.&quot;); // Write some text. Console::WriteLine(S&quot;This is the fifth line.&quot;); // Write some text. Console::WriteLine(S&quot;Press ENTER to clear.&quot;); // Write some text. Console::ReadLine; // Wait for user input.
 * 1) include &quot;nsClearConsole.h&quot; </li>

ClearMyConsole->Clear; // Clear the screen.

Console::WriteLine(S&quot;The console was cleared.&quot;); // Write some text to clear the console. Console::WriteLine(S&quot;Press ENTER to quit. &quot;); // Write some text. Console::ReadLine; // Wait for user input. return 0; </li> Press CTRL+F5 to build and to run the application. Notice that the text in the Console window is cleared.</li></ol>

back to the top

<div class="references_section">