TRUCOS/EJEMPLOS - Visual Basic 6.0
Nota : si están mal puestos los símbolos "_" para separar juntarlos vosotros para que funcione, es muy sencillo y no necesita manual, pues se intuye.
Escribir/leer Archivos .ini
Crea un nuevo módulo y añade esto :
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"_
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As_
String ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As_
String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias_
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As_
Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Escribir *.ini :
Dim escribir As IntegerDim INI As StringINI = "Sección - Deejemplo"escribir = WritePrivateProfileString("Ejemplo", "nombreejemplo", INI, "INInombre.ini")
Leer *.ini :
Dim Leer As IntegerDim INI As StringINI= String$(50, " ")Leer = GetPrivateProfileString("Ejemplo", "nombreejemplo", "", INI, Len(INI), "INInombre.ini")If Leer > 0 Then 'Comprobación, porque si INI=1 significa que esa linea está vaciaMsgBox "Archivo INI, sección ejemplo : " & INIEnd If
Vaciar la carpeta 'Documentos' de Windows:
Private Declare Function SHAddToRecentDocs Lib "Shell32"_ (ByVal lFlags As Long, ByVal lPv As Long) As Long
Private Sub Form_Load()
SHAddToRecentDocs 0, 0
End Sub
Random da números aleatorios entre 1 y 0
Private Sub Form_Load()Dim azar DoubleRandomize 'Muy importanteazar = RndMsgBox azar'Donde azar es la variable resultanteEnd Sub
Textbox que solo acepta números, desde código
'Mira textbox + numero, y depende del número pones'text1_keypress o el numeroSub Text1_Keypress(KeyAscii As Integer)If KeyAscii <> Asc("9") ThenIf KeyAscii <> 8 Then 'RetrocesoKeyAscii = 0End IfEnd IfEnd sub
Saber si un archivo existe o no
En form_load poner lo siguiente :
On Error GoTo Nox = GetAttr("C:\User\Usuario\ejemplo.txt")MsgBox "el archivo existe!!!!"Exit Sub 'ImportanteNo:MsgBox "El archivo no existe =("
No mostrar puntero del ratón
Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As LongPrivate sub mostrar() 'Para mostrar llamamos a mostrarresult = ShowCursor(True)end sub
Private Sub ocultar() 'Para ocultar llamamos a ocultarresult = ShowCursor(False)end sub
Capturar toda la pantalla:
'Crear un botón llamado botoncapturar'y poner el siguiente códigoPrivate Declare Sub keybd_event Lib "user32"_ (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub botoncapturar_Click()'Captura toda la pantallakeybd_event 44, 1, 0&, 0&End Sub
Transparencia en el formulario
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _(ByVal hwnd As Long, ByVal nIndex As Long,
_ ByVal dwNewLong As Long) As LongPrivate Sub Form_Load()Dim trans As Long 'Declaramostrans = SetWindowLong(Me.hwnd, -20, &H20&) 'Lo hacemos transparenteForm1.Refresh 'Y actualizamosEnd Sub
Enviar archivo a papelera de reciclaje
Private Type SHFILEOPSTRUCThWnd As LongwFunc As LongpFrom As StringpTo As StringfFlags As IntegerfAnyOperationsAborted As BooleanhNameMappings As LonglpszProgressTitle As StringEnd Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA"
_ (lpFileOp As SHFILEOPSTRUCT) As Long
Read More »»Private Const FO_DELETE = &H3Private Const FOF_ALLOWUNDO = &H40
Public Sub Papelera(ByVal Fichero As String)Dim SHFileOp As SHFILEOPSTRUCTDim RetVal As LongWith SHFileOp.wFunc = FO_DELETE.pFrom = FileName.fFlags = FOF_ALLOWUNDOEnd WithRetVal = SHFileOperation(SHFileOp)End Sub
Private Sub Form_Load()Recycle "C:\User\Usuario\ejemplo.txt"End Sub




