Tengo que admitir que a pesar de no ser un "gran fanatico" de las tecnologias de Microsoft, es muy utilizada y en muchos casos es bastante util. Hace poco comenzé con mi carrera (Analista de Sistemas) y hemos comenzado el primer modulo: VB.NET. Luego de haber hecho un par de aplicaciones de bajo "calibre" un problema con el profesor hizo que apareciera una profesora, la cual comenzó con una clase un poco mas amena y con mucha mas "logica de programacion" y no tanta "aplicacion", ya que al fin y al cabo el curso es para saber programar no para saber VB.NET nada mas.
Para no extenderme más comenzamos programando aplicaciones de consola (Si si, se han dejado de usar, pero para el aprendizaje es excelente) y tuvimos que hacer la tipica Calculadora. Ante la imposibilidad de hacer un "CLEAR" de pantalla "nativo" desde el Visual busqué y encontré la clase que pasaré a continuación para hacer un borrado de pantalla de la consola.
Clase: Clear.vb
Visual Basic:
-
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.
-
_
-
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
-
_
-
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.
-
_
-
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
Y Aqui les dejo una calculadora muy poco util pero que sirve de ejemplo :P.
Nombre: El_que_quieran.vb
Visual Basic:
-
' Inicio una Instancia de la Clase para limpiar la Consola
-
Dim MyConsole As New ClearConsole()
-
' Declaramos dos variables que son los numeros para operar y la
-
' opcion que guardará la (Valga la redundancia) opcion del menu seleccionada
-
Dim num_1, num_2 As Integer
-
Dim opcion As Short
-
Dim comando As String
-
-
Do
-
MyConsole.Clear()
-
Console.WriteLine("------------------------------")
-
Console.WriteLine("Calculadora de Consola")
-
Console.WriteLine("------------------------------")
-
Console.WriteLine("1er Numero: ")
-
num_1 = Console.Read()
-
Console.WriteLine("2do Numero: ")
-
num_2 = Console.Read()
-
Console.WriteLine()
-
-
Do
-
Console.WriteLine("1. Sumar")
-
Console.WriteLine("2. Restar")
-
Console.WriteLine("3. Multiplicar")
-
Console.WriteLine("4. Dividir")
-
Console.WriteLine("5. Resto de la Division")
-
-
opcion = Console.ReadLine()
-
Loop While (opcion> 5)
-
-
Select Case opcion
-
Case 1
-
Console.WriteLine("La Suma de " & num_1 & " + " & num_2 & " = " & num_1 + num_2)
-
Case 2
-
Console.WriteLine("La Resta de " & num_1 & " - " & num_2 & " = " & num_1 - num_2)
-
Case 3
-
Console.WriteLine("La Multiplicacion de " & num_1 & " x " & num_2 & " = " & num_1 * num_2)
-
Case 4
-
Console.WriteLine("La Division de " & num_1 & " / " & num_2 & " = " & num_1 / num_2)
-
Case 5
-
Console.WriteLine("El Modulo de " & num_1 & " / " & num_2 & " = " & num_1 Mod num_2)
-
End Select
-
-
Console.WriteLine()
-
comando = Console.ReadLine()
-
Loop While (comando <> "exit")
P.D: No está depurado, es un ejemplo MUY basico de el uso de la clase.
Via: Microsoft (La definición de la Clase)