Escolar Documentos
Profissional Documentos
Cultura Documentos
Uso de Random:
La funcin Rnd o Random posee la virtud de obtener nmeros
aleatorios entre 0 y 1:
El nico inconveniente a la hora de usar Rnd, es que hay que
inicializarlo, en otro caso,
el resultado de la funcin Rnd, ser siempre el mismo dentro de un
determinado ordenador.
Por ejemplo, el cdigo:
chrg As CharRange
End Type
Private Const WM_USER As Long = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113
Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias
"SendMessageA" _
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, lp As
Any) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
(ByVal lpDriverName As String, ByVal lpDeviceName As String, _
ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
Public Function WYSIWYG_RTF(RTF As RichTextBox,
LeftMarginWidth As Long, _
RightMarginWidth As Long) As Long
Dim LeftOffset As Long, LeftMargin As Long, RightMargin As Long
Dim LineWidth As Long
Dim PrinterhDC As Long
Dim r As Long
Printer.Print Space(1)
Printer.ScaleMode = vbTwips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
PHYSICALOFFSETX), vbPixels, vbTwips)
LeftMargin = LeftMarginWidth - LeftOffset
RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
LineWidth = RightMargin - LeftMargin
PrinterhDC = CreateDC(Printer.DriverName, Printer.DeviceName, 0,
0)
r = SendMessage(RTF.hWnd, EM_SETTARGETDEVICE, PrinterhDC, _
ByVal LineWidth)
Printer.KillDoc
WYSIWYG_RTF = LineWidth
End Function
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long,
_
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long
Printer.Print Space(1)
Printer.ScaleMode = vbTwips
LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
Cdigo:
Dim foo As Integer
foo = FreeFile
Open "C:\Archivo.txt" For Input As #foo
Text1.Text = Input(LOF(foo), #foo)
Close #foo
Para guardar:
Cdigo:
Dim foo As Integer
foo = FreeFile
Open "C:\Archivo.txt" For Output As #foo
Print #foo, Text1.Text
Close #foo
dialogos:
Ese es para Abrir
Cdigo:
Dim strOpen As String
CommonDialog1.ShowOpen
strOpen = CommonDialog1.FileName
Text1.LoadFile strOpen
Text1.LoadFile strClose
Ese para guardar
Cdigo:
Dim strNewFile As String
CommonDialog1.ShowSave
strNewFile = CommonDialog1.FileName
Text1.SaveFile strNewFile
SendKeys "{tab}"
KeyAscii = 0
End If
End Sub
Label1.Left = 100
End Sub
End If
End If
Loop Until allDrives$ = "" Or DriveType& = DRIVE_CDROM
If CDfound% Then
label1.Caption = "La unidad de CD-ROM corresponde a la
unidad: " & UCase$(JustOneDrive$)
Else
label1.Caption = "Su sistema no posee CD-ROM o unidad
no encontrada."
End If
End Sub
+ "Kb"
+ "Kb"
+ "Kb"
+ "Kb"
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type
Declare Function GetVersionEx Lib "kernel32"
Alias "GetVersionExA" (LpVersionInformation
As OSVERSIONINFO) As Long
Declare Sub GlobalMemoryStatus Lib "kernel32"
(lpBuffer As MEMORYSTATUS)
Declare Sub GetSystemInfo Lib "kernel32"
(lpSystemInfo As SYSTEM_INFO)
Public
Public
Public
Public
Public
Const
Const
Const
Const
Const
PROCESSOR_INTEL_386 = 386
PROCESSOR_INTEL_486 = 486
PROCESSOR_INTEL_PENTIUM = 586
PROCESSOR_MIPS_R4000 = 4000
PROCESSOR_ALPHA_21064 = 21064
End If
strOrg = Space$(256)
lngLen = Len(strOrg)
x = RegQueryValueEx(hKey, "RegisteredOrganization", 0,
lngType,
ByVal strOrg, lngLen)
If x = 0 And lngType = REG_SZ And lngLen > 1 Then
strOrg = Left$(strOrg, lngLen - 1)
Else
strOrg = "Unknown"
End If
Label1.Caption = "Usuario: " & strUser
Label2.Caption = "Empresa: " & strOrg
x = RegCloseKey(hKey)
End Sub
End Sub
Sub Pause(interval)
Dim atime
atime = Timer
Do While Timer - atime < Val(interval)
DoEvents
Loop
End Sub
Editor de texto:
Seleccionar todo el texto:
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Copiar texto:
Clipboard.Clear
Clipboard.SetText Text1.SelText
Text1.SetFocus
Pegar texto:
Text1.SelText = Clipboard.GetText()
Text1.SetFocus
Cortar texto:
Clipboard.SetText Text1.SelText
Text1.SelText = ""
Text1.SetFocus
Deshacer texto: (Nota: esta operacin slo es eficaz con el
control Rich TextBox).
En un mdulo copie esta lnea:
Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
As Long) As Long
Esta es la instruccin de la funcin deshacer:
UndoResultado = SendMessage(Text1.hwnd, &HC7, 0&, 0&)
If UndoResultado = -1 Then
Beep
MsgBox "Error al intentar recuperar.", 20, "Deshacer texto"
End If
Seleccionar todo el texto:
SendKeys "^A"
Copiar texto:
SendKeys "^C"
Pegar texto:
SendKeys "^V"
Cortar texto:
SendKeys "^X"
Deshacer texto:
SendKeys "^Z"
Sub Command1_Click()
Car As String * 128
Longitud, Es As Integer
Camino As String
Longitud = 128
Es = GetWindowsDirectory(Car, Longitud)
Camino = RTrim$(LCase$(Left$(Car, Es)))
Label1.Caption = Camino
Es = GetSystemDirectory(Car, Longitud)
Camino = RTrim$(LCase$(Left$(Car, Es)))
Label2.Caption = Camino
End Sub
End Sub
En las declaraciones "Generales" del formulario, escribimos:
Public Sub imprimeLineas(Texto As Object, Linea As Integer)
Dim Bloque As String
'Numero de caracteres = NumC
'Numero de Bloques = NumB
Dim NumC, NumB As Integer
NumC = Len(Texto.Text)
If NumC > Linea Then
NumB = NumC \ Linea
For I = 0 To NumB
Texto.SelStart = (Linea * I)
Texto.SelLength = Linea
Bloque = Texto.SelText
Printer.Print Bloque
Next I
Else
Printer.Print Texto.Text
End If
Printer.EndDoc
End Sub
Escribir en "Prueba.Ini":
Private Sub Form_Unload(Cancel As Integer)
Dim I As Integer
Dim Est As String
Est = "Ejemplo - Apartado"
I = WritePrivateProfileString("Ejemplo", "Nombre", Est,
"Ejemplo.ini")
End Sub
(Nota: si I=0 quiere decir que no existe Informacin en la lnea de
fichero Ini a la
que hacemos referencia. El fichero "Ejemplo.Ini" se crear
automticamente).
Unidad de disco:
Crear un mdulo y escribir:
Declare Function GetDiskFreeSpace Lib "kernel32" Alias
"GetDiskFreeSpaceA"_
(ByVal lpRootPathName As String, lpSectorsPerCluster As Long,
lpBytesPerSector_
As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters
As Long) As Long
Crear 7 Labels:
Escribir el cdigo siguiente:
Private Sub Form_Load()
Dim I1 As Long
Dim I2 As Long
Dim I3 As Long
Dim I4 As Long
Dim Unidad As String
Unidad = "C:/"
GetDiskFreeSpace Unidad, I1, I2, I3, I4
Label1.Caption = Unidad
Label2.Caption = I1 & " Sectores por cluster"
Label3.Caption = I2 & " Bytes por sector"
Label4.Caption = I3 & " Nmero de clusters libres"
Label5.Caption = I4 & " Nmero total de clusters"
Label6.Caption = "Espacio total en disco: " & (I1 * I2 * I4)
Label7.Caption = "Espacio libre en disco: " & (I1 * I2 * I3)
End Sub
NT:
Cree un nuevo proyecto e inserte dos ButtonClick y un Mdulo:
Pegue el siguiente cdigo en el formulario:
Private Sub Command1_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Dim nPC as String
Dim buffer As String
Dim estado As Long
buffer = String$(255, " ")
estado = GetComputerName(buffer, 255)
If estado <> 0 Then
nPC = Left(buffer, 255)
End If
MsgBox "Nombre del PC: " & nPC
End Sub
Private Sub Command2_Click()
Unload Form1
End Sub
Pegue el siguiente cdigo en el mdulo:
Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Dim
Dim
Dim
Dim
Cont As Integer
ContLinea As Integer
Cuatro(4) As Integer
Base64 As String
Base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx
yz0123456789+/"
ContLinea = 0
MensajeSaliente = ""
MensajeEntrante = ""
If Cfg.FicheroAnexo <> "" Then
Open NFich For Binary As #3 Len = 3
Cont = 0
ContTotal = 0
Progreso.Max = FileLen(NFich)
While Not ContTotal = LOF(3)
ContTotal = ContTotal + 1
Caracter = Input(1, 3)
Cont = Cont + 1
Trio(Cont) = Asc(Caracter)
'MensajeSaliente = MensajeSaliente + Caracter
If Cont = 3 Then
Cuatro(1) = Int(Trio(1) / 4)
Cuatro(2) = (Trio(1) - Int(Trio(1) / 4) * 4) * 16 + Int(Trio(2) / 16)
Cuatro(3) = (Trio(2) - (Int(Trio(2) / 16) * 16)) * 4 + Int(Trio(3) / 64)
Cuatro(4) = Trio(3) - Int(Trio(3) / 64) * 64
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(1) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(2) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(3) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(4) + 1, 1)
Cont = 0
ContLinea = ContLinea + 4
If ContLinea = 76 Then
MensajeEntrante = MensajeEntrante + vbCrLf
ContLinea = 0
End If
End If
DoEvents
Wend
Select Case Cont
Case 1
Cuatro(1) = Int(Trio(1) / 4)
Cuatro(2) = (Trio(1) - Int(Trio(1) / 4) * 4) * 16
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(1) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(2) + 1, 1) +
"=="
Case 2
Cuatro(1) = Int(Trio(1) / 4)
Trucos Breves 2