Ejemplos/trucos VB6

.

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 Integer
Dim INI As String
INI = "Sección - Deejemplo"
escribir = WritePrivateProfileString("Ejemplo", "nombreejemplo", INI, "INInombre.ini")


Leer  *.ini :

Dim Leer As Integer
Dim INI As String
INI= 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á vacia
MsgBox "Archivo INI, sección ejemplo : " & INI
End 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 Double
Randomize 'Muy importante
azar = Rnd
MsgBox azar
'Donde azar es la variable resultante
End Sub

Textbox que solo acepta números, desde código


         
'Mira textbox + numero, y depende del número pones
         'text1_keypress o el numero
Sub Text1_Keypress(KeyAscii As Integer)
If KeyAscii <> Asc("9") Then
If KeyAscii <> 8 Then 'Retroceso
KeyAscii = 0
End If
End If
         End sub




Saber si un archivo existe o no

En form_load poner lo siguiente :

On Error GoTo No
x = GetAttr("C:\User\Usuario\ejemplo.txt")
MsgBox "el archivo existe!!!!"
Exit Sub 'Importante
No:
MsgBox "El archivo no existe =("

No mostrar puntero del ratón

Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Private sub mostrar() 'Para mostrar llamamos a mostrar
result = ShowCursor(True)
end sub

Private Sub ocultar() 'Para ocultar llamamos a ocultar
result = ShowCursor(False)
end sub

Capturar toda la pantalla:

'Crear un botón llamado botoncapturar
'y poner el siguiente código
Private 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 pantalla
keybd_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 Long
Private Sub Form_Load()
Dim trans As Long 'Declaramos
trans = SetWindowLong(Me.hwnd, -20, &H20&) 'Lo hacemos transparente
Form1.Refresh 'Y actualizamos
End Sub

Enviar archivo a papelera de reciclaje

Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA"
_ (lpFileOp As SHFILEOPSTRUCT) As Long
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40

Public Sub Papelera(ByVal Fichero As String)
Dim SHFileOp As SHFILEOPSTRUCT
Dim RetVal As Long
With SHFileOp
.wFunc = FO_DELETE
.pFrom = FileName
.fFlags = FOF_ALLOWUNDO
End With
RetVal = SHFileOperation(SHFileOp)
End Sub

Private Sub Form_Load()
Recycle "C:\User\Usuario\ejemplo.txt"
End Sub

Read More »»