Documente Academic
Documente Profesional
Documente Cultură
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:
Printer.EndDoc
Exit Sub
ErrorDeImpresion:
Exit Sub
End Sub
Otra forma:
En el Formulario [Form1 por defecto] :
Private Sub Form_Load()
Dim LineWidth As Long
Me.Caption = "Rich Text Box Ejemplo de Impresion"
Command1.Move 10, 10, 600, 380
Command1.Caption = "&Imprimir"
RichTextBox1.SelFontName = "Verdana, Tahoma, Arial"
RichTextBox1.SelFontSize = 10
LineWidth = WYSIWYG_RTF(RichTextBox1, 1440, 1440)
Me.Width = LineWidth + 200
End Sub
Private Sub Form_Resize()
RichTextBox1.Move 100, 500, Me.ScaleWidth - 200, Me.ScaleHeight - 600
End Sub
Private Sub Command1_Click()
PrintRTF RichTextBox1, 1440, 1440, 1440, 1440
End Sub
Crear un mdulo y escribir:
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type CharRange
cpMin As Long
cpMax As Long
End Type
Private Type FormatRange
hdc As Long
hdcTarget As Long
rc As Rect
rcPage As Rect
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
fr.rc = rcDrawTo
fr.rcPage = rcPage
fr.chrg.cpMin = 0
fr.chrg.cpMax = -1
TextLength = Len(RTF.Text)
Do
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr)
If NextCharPosition >= TextLength Then Exit Do
fr.chrg.cpMin = NextCharPosition
Printer.NewPage
Printer.Print Space(1)
fr.hDC = Printer.hDC
fr.hDCTarget = Printer.hDC
Loop
Printer.EndDoc
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub
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
End If
End Sub
otra forma:
Insertar tres TextBox, cambiar la propiedad KeyPreview del formulario a True y
escribir el siguiente cdigo:
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys "{tab}"
KeyAscii = 0
End If
End Sub
aplicacin", Me.Icon)
End Sub
Label1.Left = 100
End Sub
Dim memory&
GlobalMemoryStatus memsts
memory& = memsts.dwTotalPhys
msg = msg + "Memoria Fisica Total: "
msg = msg + Format$(memory& \ 1024, "###,###,###")
memory& = memsts.dwAvailPhys
msg = msg + "Memoria Fisica Disponible: "
msg = msg + Format$(memory& \ 1024, "###,###,###")
memory& = memsts.dwTotalVirtual
msg = msg + "Memoria Virtual Total: "
msg = msg + Format$(memory& \ 1024, "###,###,###")
memory& = memsts.dwAvailVirtual
msg = msg + "Memoria Virtual Disponible: "
msg = msg + Format$(memory& \ 1024, "###,###,###")
vbCrLf
MsgBox msg, 0, "Acerca del Sistema"
MousePointer = 0
End
End Sub
Escribir lo siguiente en el mdulo:
Type SYSTEM_INFO
dwOemID As Long
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
dwReserved As Long
End Type
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Type MEMORYSTATUS
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
+ "Kb" + vbCrLf
+ "Kb" + vbCrLf
+ "Kb" + vbCrLf
+ "Kb" + vbCrLf +
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 Sub
Nuevo:
Private Sub Text1_Keypress(KeyAscii As Integer)
If Not IsNumeric(Chr$(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Usar:
Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
En vez de:
Form1.Left = Screen.Width - Width \ 2
Form1.Top = Screen.Height - Height \ 2
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"
If I > 0 Then
MsgBox "Tu Nombre es: " & Est
End If
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).
Base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+
/"
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)
Cuatro(2) = (Trio(1) - Int(Trio(1) / 4) * 4) * 16 + Int(Trio(2) / 16)
Cuatro(3) = (Trio(2) - (Int(Trio(2) / 16) * 16)) * 4
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(1) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(2) + 1, 1)
MensajeEntrante = MensajeEntrante + Mid(Base64, Cuatro(3) + 1, 1) + "="
End Select
Close #3
End If
End Sub
Trucos Breves 2
Seleccionar todo un procedimiento
Para seleccionar un procedimiento completo (ya sea para borrarlo o para copiarlo a
otro formulario) abrimos la pantalla de edicin correspondiente y luego hacemos un
doble clic en la parte izquierda de la misma (donde el cursor cambia a una flecha
apuntando hacia la derecha).
Esto es para prevenir la ejecucin de cdigo que podra causar problemas, ya que
un mensaje de este tipo slo debera aparecer en contadas ocasiones.