elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Crear aplicaciones de consola VB {AVANZADO} [Clase]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Crear aplicaciones de consola VB {AVANZADO} [Clase]  (Leído 2,445 veces)
Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Crear aplicaciones de consola VB {AVANZADO} [Clase]
« en: 10 Octubre 2008, 14:17 pm »

Bueno, como prometi aqui:

Citar

Aqui traigo la Clase:

Código
  1. Option Explicit
  2.  
  3. '-----------------------------------------
  4. 'Autor: Karcrack                          |
  5. 'Creditos: MSDN                           |
  6. 'Fecha: 10/10/08                          |
  7. 'Web: http://foro.fire-software.net       |
  8. 'Utilidad: Ejemplo de uso de las APIs para|
  9. 'enviar y recibir informacion con         |
  10. 'aplicaciones de Command Line.            |
  11. '=========================================|
  12. 'Puedes distribuir libremente este codigo |
  13. 'Siempre que pongas el autor.             |
  14. '------------------------------------------
  15.  
  16. Enum Colors
  17.    Negro = &H0
  18.    Azul = &H1
  19.    Verde = &H2
  20.    AguaMarina = &H3
  21.    Red = &H4
  22.    Purpura = &H5
  23.    Amarillo = &H6
  24.    Blanco = &H7
  25.    Gris = &H8
  26.    AzulClaro = &H9
  27.    VerdeClaro = &HA&
  28.    AguamarinaClaro = &HB&
  29.    RojoClaro = &HC&
  30.    PurpuraClaro = &HD&
  31.    AmarilloClaro = &HE&
  32.    BlancoBrillante = &HF&
  33. End Enum
  34.  
  35. Private Const ENABLE_LINE_INPUT = &H2&
  36. Private Const ENABLE_ECHO_INPUT = &H4&
  37. Private Const ENABLE_MOUSE_INPUT = &H10&
  38. Private Const ENABLE_PROCESSED_INPUT = &H1&
  39. Private Const ENABLE_WINDOW_INPUT = &H8&
  40. Private Const ENABLE_PROCESSED_OUTPUT = &H1&
  41. Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2&
  42. Private Const STD_OUTPUT_HANDLE = -11&
  43. Private Const STD_INPUT_HANDLE = -10&
  44. Private Const STD_ERROR_HANDLE = -12&
  45. Private Const INVALID_HANDLE_VALUE = -1&
  46.  
  47. Private mvarTitle   As String
  48. Private mvarFColor  As Double
  49. Private mvarBColor  As Double
  50. Private hCMDIn      As Double
  51. Private hCMDOut     As Double
  52.  
  53. Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
  54. Private Declare Function FreeConsole Lib "kernel32" () As Long
  55. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  56. Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
  57. Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
  58. Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
  59. Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
  60. Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
  61.  
  62. Public Function GetData(Optional ByVal dCharacters As Double, Optional ByVal bLine As Boolean) As String
  63.    Dim lPos            As Long
  64.  
  65.    GetData = String$(IIf(dCharacters = 0, 500, dCharacters), 0)
  66.  
  67.    Call ReadConsole(hCMDIn, GetData, Len(GetData), lPos, vbNull)
  68.    GetData = Left$(GetData, lPos)
  69.  
  70.    If bLine = True Then
  71.        GetData = Mid$(GetData, InStrRev(GetData, vbCrLf))
  72.    End If
  73. End Function
  74.  
  75. Public Function SendData(ByVal sData As String, Optional ByVal dNewFColor As Colors, Optional ByVal dNewBColor As Colors) As Boolean
  76.    Dim dLenWritten     As Long
  77.    Dim Color1          As Long
  78.    Dim Color2          As Long
  79.  
  80.    If dNewFColor Then
  81.        Color1 = dNewFColor
  82.    Else
  83.        Color1 = mvarFColor
  84.    End If
  85.  
  86.    If dNewBColor Then
  87.        Color2 = dNewBColor
  88.    Else
  89.        Color2 = mvarBColor
  90.    End If
  91.  
  92.    Call SetConsoleTextAttribute(hCMDOut, Color1 Or Color2)
  93.    Call WriteConsole(hCMDOut, ByVal sData, Len(sData), dLenWritten, ByVal 0&)
  94.    If dLenWritten = Len(sData) Then
  95.        SendData = True
  96.    End If
  97.    Call SetConsoleTextAttribute(hCMDOut, mvarFColor Or mvarBColor)
  98. End Function
  99.  
  100. Public Property Let ForeColor(ByVal vData As Colors)
  101.    mvarFColor = vData
  102.    Call SetConsoleTextAttribute(hCMDOut, mvarFColor Or mvarBColor)
  103. End Property
  104.  
  105. Public Property Get ForeColor() As Colors
  106.    ForeColor = mvarFColor
  107. End Property
  108.  
  109. Public Property Let BackColor(ByVal vData As Colors)
  110.    mvarBColor = vData
  111.    Call SetConsoleTextAttribute(hCMDOut, mvarFColor Or mvarBColor)
  112. End Property
  113.  
  114. Public Property Get BackColor() As Colors
  115.    BackColor = mvarBColor
  116. End Property
  117.  
  118. Public Property Let Title(ByVal vData As String)
  119.    mvarTitle = vData
  120.    Call SetConsoleTitle(mvarTitle)
  121. End Property
  122.  
  123. Public Property Get Title() As String
  124.    Title = mvarTitle
  125. End Property
  126.  
  127. Private Sub Class_Initialize()
  128.    If App.LogMode = 0 Then AllocConsole
  129.    hCMDOut = GetStdHandle(STD_OUTPUT_HANDLE)
  130.    hCMDIn = GetStdHandle(STD_INPUT_HANDLE)
  131.    Call SetConsoleTitle(mvarTitle)
  132.    Call SetConsoleTextAttribute(hCMDOut, mvarFColor Or mvarBColor)
  133. End Sub
  134.  
  135. Private Sub Class_Terminate()
  136.    CloseHandle hCMDOut
  137.    CloseHandle hCMDIn
  138.    If App.LogMode = 0 Then FreeConsole
  139. End Sub
  140.  

Aqui la adjunto con algunos ejemplos:

Citar

Saludos :D


En línea

achernar_

Desconectado Desconectado

Mensajes: 117



Ver Perfil
Re: Crear aplicaciones de consola VB {AVANZADO} [Clase]
« Respuesta #1 en: 10 Octubre 2008, 21:30 pm »

Porque una clase? En serio lo pregunto, si en un modulo simplón se pueden poner las funciones IniciarConsola (que retorne True si todo sale bien), EscribirEnConsola (que retorne el numero de caracteres escritos), LeerDesdeConsola (que guarda en un buffer el texto ingresado a la consola y retorne el numero de caracteres ingresados), TerminarConsola (retorno True o False y TituloDeConsola (puede ser solo una subrutina)


En línea

Tengo una habilidad sorprendente para hacer cosas que no sorprenden.
Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Re: Crear aplicaciones de consola VB {AVANZADO} [Clase]
« Respuesta #2 en: 10 Octubre 2008, 21:38 pm »

Simplemente porque es mas comodo al menos a mi parecer. Si cualquier persona tiene problemas con las clases que lo pasen a modulo, que como tu dices se puede hacer perfectamente  ;)

Saludos :D
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
crear aplicaciones visuales con c
Programación C/C++
linux1 4 3,963 Último mensaje 19 Diciembre 2010, 14:56 pm
por Garfield07
[Python] -Crear clase
Scripting
Meta 0 2,702 Último mensaje 22 Enero 2011, 14:54 pm
por Meta
Aplicaciones al inicio por consola
GNU/Linux
risto 0 1,788 Último mensaje 6 Febrero 2012, 11:02 am
por risto
Crear una consola por hilo.
.NET (C#, VB.NET, ASP)
SARGE553413 4 2,447 Último mensaje 12 Julio 2014, 21:16 pm
por Eleкtro
Crear una instancia de clase para calculo de sueldo de trabajadores
.NET (C#, VB.NET, ASP)
Maria Alejandra 4 3,114 Último mensaje 17 Abril 2016, 19:23 pm
por Maria Alejandra
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines