How to clear the Console window with Visual C++ .NET or Visual C++ 2005 (816180)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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 TASKSUMMARYThis step-by-step article describes how to clear the
Console window programmatically by using Microsoft Visual C++ .NET. back to the topSample 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:
#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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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: - Add the following header file declaration in the TestProject.cpp file:
#include "nsClearConsole.h" - Paste the following statement before the main function:
using namespace nsClearConsole; - 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; - Press CTRL+F5 to build and to run
the application. Notice that the text in the Console window is
cleared.
back to the
topREFERENCESFor more information about console functions, visit the
following Microsoft Developer Network (MSDN) Web site:
back to the
top
| Modification Type: | Major | Last Reviewed: | 12/30/2005 |
|---|
| Keywords: | kbHOWTOmaster KB816180 kbAudDeveloper |
|---|
|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|