Posted on 11-05-2008
Filed Under (Desarrollo, VB.NET) by Programlocura

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:
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class ClearConsole
  4.  
  5. Private Const STD_OUTPUT_HANDLE As Integer = &HFFFFFFF5
  6. Private Const EMPTY As Byte = 32
  7.  
  8. ' Structure defines the coordinates of a character cell in a console screen buffer.
  9. ' The origin of the coordinate system (0,0) is at the top-left cell of the buffer.
  10. _
  11. Structure COORD
  12. Dim X As Short
  13. Dim Y As Short
  14. End Structure
  15.  
  16. ' Structure defines the coordinates of the upper-left and lower-right corners of a rectangle
  17. _
  18. Structure SMALL_RECT
  19. Dim Left As Short
  20. Dim Top As Short
  21. Dim Right As Short
  22. Dim Bottom As Short
  23. End Structure
  24.  
  25. ' Structure containing information about the Console's screen buffer.
  26. _
  27. Structure CONSOLE_SCREEN_BUFFER_INFO
  28. Dim dwSize As COORD
  29. Dim dwCursorPosition As COORD
  30. Dim wAttributes As Integer
  31. Dim srWindow As SMALL_RECT
  32. Dim dwMaximumWindowSize As COORD
  33. End Structure
  34.  
  35. ' Win32 API Function declarations.
  36. Declare Auto Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Integer) As IntPtr
  37. Declare Auto Function FillConsoleOutputCharacter Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, ByVal cCharacter As Byte, _
  38. ByVal nLength As Integer, _
  39. ByVal dwWriteCoord As COORD, _
  40. ByRef lpNumberOfCharsWritten As IntPtr) As Integer
  41. Declare Auto Function GetConsoleScreenBufferInfo Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, _
  42. ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
  43. Declare Auto Function SetConsoleCursorPosition Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, ByVal dwCursorPosition As COORD) As Integer
  44.  
  45. ' Subroutine used to clear the Console screen.
  46. Public Sub Clear()
  47. Dim hConsoleHandle As IntPtr
  48. Dim hWrittenChars As IntPtr
  49. Dim strConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO
  50. Dim strOriginalLocation As COORD
  51. hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE) ' Get Handle for standard output
  52. GetConsoleScreenBufferInfo(hConsoleHandle, strConsoleInfo) ' Get information about the standard output buffer of the Console
  53. FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.X * strConsoleInfo.dwSize.Y, strOriginalLocation, hWrittenChars) ' Fill output buffer with Empty characters (ASCII 32)
  54. SetConsoleCursorPosition(hConsoleHandle, strOriginalLocation) ' Set the Console cursor back to the origin
  55. End Sub
  56.  
  57. End Class

Y Aqui les dejo una calculadora muy poco util pero que sirve de ejemplo :P.
Nombre: El_que_quieran.vb

Visual Basic:
  1. ' Inicio una Instancia de la Clase para limpiar la Consola
  2. Dim MyConsole As New ClearConsole()
  3. ' Declaramos dos variables que son los numeros para operar y la
  4. ' opcion que guardará la (Valga la redundancia) opcion del menu seleccionada
  5. Dim num_1, num_2 As Integer
  6. Dim opcion As Short
  7. Dim comando As String
  8.  
  9. Do
  10. MyConsole.Clear()
  11. Console.WriteLine("------------------------------")
  12. Console.WriteLine("Calculadora de Consola")
  13. Console.WriteLine("------------------------------")
  14. Console.WriteLine("1er Numero: ")
  15. num_1 = Console.Read()
  16. Console.WriteLine("2do Numero: ")
  17. num_2 = Console.Read()
  18. Console.WriteLine()
  19.  
  20. Do
  21. Console.WriteLine("1. Sumar")
  22. Console.WriteLine("2. Restar")
  23. Console.WriteLine("3. Multiplicar")
  24. Console.WriteLine("4. Dividir")
  25. Console.WriteLine("5. Resto de la Division")
  26.  
  27. opcion = Console.ReadLine()
  28. Loop While (opcion> 5)
  29.  
  30. Select Case opcion
  31. Case 1
  32. Console.WriteLine("La Suma de " & num_1 & " + " & num_2 & " = " & num_1 + num_2)
  33. Case 2
  34. Console.WriteLine("La Resta de " & num_1 & " - " & num_2 & " = " & num_1 - num_2)
  35. Case 3
  36. Console.WriteLine("La Multiplicacion de " & num_1 & " x " & num_2 & " = " & num_1 * num_2)
  37. Case 4
  38. Console.WriteLine("La Division de " & num_1 & " / " & num_2 & " = " & num_1 / num_2)
  39. Case 5
  40. Console.WriteLine("El Modulo de " & num_1 & " / " & num_2 & " = " & num_1 Mod num_2)
  41. End Select
  42.  
  43. Console.WriteLine()
  44. comando = Console.ReadLine()
  45. 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)

(2) Comments    Read More