How do you clear the screen and start the cursor at the top of the screen in a console (DOS screen)
project?
It seems to be a well guarded secret.
I'm using MS Visual Studio .NET ver 1.0
--
JMG(no spam)2064@ao(hel)l.com
For the news.devx.com news server, try these
vb.dotnet.discussion
vb.dotnet.technical
For the microsoft news server, try these newsgroups...
microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
There are some others, but these should get you started.
Rick - MVP
"jmg" <j...@jmg.com> wrote in message
news:3U63a.4747$606.1...@news20.bellglobal.com...
You call call use P/Invoke. You need to use:
GetStdHandle, GetConsoleScreenBufferInfo, SetConsoleCursorPosition, and
FillConsoleOutputCharacter
The idea is basically to get the console screen buffer, fill it with spaces
and move the cursor to the home position - or were ever you want it :)
Tom Shelton
--
JMG(no spam)2064@ao(hel)l.com
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:gmmdncwh1-h...@comcast.com...
--
JMG(no spam)2064@ao(hel)l.com
(So many net nannies .... so few nurseries.)
To run, open a new console project and replace the entire code with the following code.
----------------------------------------------------------------------------------------------------
---------------------------------
Imports System.Runtime.InteropServices
Public Class ClearConsole
Private Const STD_OUTPUT_HANDLE As Integer = &HFFFFFFF5
Private Const EMPTY As Byte = 32
' Structure defines the coordinates of a character cell in a console screen buffer.
' The origin of the coordinate system (0,0) is at the top-left cell of the buffer.
<StructLayout(LayoutKind.Sequential)> _
Structure COORD
Dim X As Short
Dim Y As Short
End Structure
' Structure defines the coordinates of the upper-left and lower-right corners of a rectangle
<StructLayout(LayoutKind.Sequential)> _
Structure SMALL_RECT
Dim Left As Short
Dim Top As Short
Dim Right As Short
Dim Bottom As Short
End Structure
' Structure containing information about the Console's screen buffer.
<StructLayout(LayoutKind.Sequential)> _
Structure CONSOLE_SCREEN_BUFFER_INFO
Dim dwSize As COORD
Dim dwCursorPosition As COORD
Dim wAttributes As Integer
Dim srWindow As SMALL_RECT
Dim dwMaximumWindowSize As COORD
End Structure
' Win32 API Function declarations.
Declare Auto Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Integer) As IntPtr
Declare Auto Function FillConsoleOutputCharacter Lib "kernel32.dll" (ByVal hConsoleOutput As
IntPtr, ByVal cCharacter As Byte, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As
COORD, _
ByRef
lpNumberOfCharsWritten As IntPtr) As Integer
Declare Auto Function GetConsoleScreenBufferInfo Lib "kernel32.dll" (ByVal hConsoleOutput As
IntPtr, _
ByRef
lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Declare Auto Function SetConsoleCursorPosition Lib "kernel32.dll" (ByVal hConsoleOutput As
IntPtr, ByVal dwCursorPosition As COORD) As Integer
' Subroutine used to clear the Console screen.
Public Sub Clear()
Dim hConsoleHandle As IntPtr
Dim hWrittenChars As IntPtr
Dim strConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO
Dim strOriginalLocation As COORD
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE) ' Get Handle for standard output
GetConsoleScreenBufferInfo(hConsoleHandle, strConsoleInfo) ' Get information about the
standard output buffer of the Console
FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.X *
strConsoleInfo.dwSize.Y, strOriginalLocation, hWrittenChars) ' Fill output buffer with Empty
characters (ASCII 32)
SetConsoleCursorPosition(hConsoleHandle, strOriginalLocation) ' Set the Console cursor back
to the origin
End Sub
End Class
Module Module1
Sub Main()
Dim ClearMyConsole As New ClearConsole() ' Start an instance of class.
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.
End Sub
End Module