Título: Duda con codigo efecto scroll
Publicado por: [Kayser] en 19 Mayo 2012, 22:41 pm
Hola vereis he encontrado un codigo que hace un efecto scroll sobre un texto y no termino de comprender como funciona. No entiendo porque se llama a la api dos veces para escribir el texto. Alguien me echa una mano? 'Declaraciones Api '------------------------------
'Dibuja el texto Private Declare Function DrawText Lib "user32" Alias "DrawTextA" ( _ ByVal hdc As Long, _ ByVal lpStr As String, _ ByVal nCount As Long, _ lpRect As RECT, _ ByVal wFormat As Long) As Long
' Para el intervalo de tiempo Private Declare Function GetTickCount Lib "kernel32" () As Long
'Estructura para usar con el api DrawText Private Type RECT Left As Long Top As Long Right As Long Bottom As Long
End Type
Public Finalizar As Boolean
Public Sub Comenzar_Scroll(Objeto_Scroll As PictureBox, _ Texto As String, _ Velocidad As Long, _ Color_Fuente As Long)
Dim r_Height_Texto As Long Dim ObjetoRect As RECT Dim t_Vel As Long Dim Ret As Long
'Propiedades para el picture o form With Objeto_Scroll .ScaleMode = vbPixels .AutoRedraw = True 'Cambiar las propiedades de la fuente .ForeColor = Color_Fuente .FontSize = 16 End With
Ret = DrawText(Objeto_Scroll.hdc, _ Texto, -1, ObjetoRect, &H400)
If Ret = 0 Then MsgBox " Error ", vbCritical: Exit Sub With ObjetoRect 'Posición inicial del área donde dibujar el texto .Top = Objeto_Scroll.ScaleHeight .Left = 0 .Right = Objeto_Scroll.ScaleWidth r_Height_Texto = .Bottom .Bottom = .Bottom + Objeto_Scroll.ScaleHeight End With 'Esta variable si está en True detiene el scroll Finalizar = False
While Finalizar = False If (GetTickCount() - t_Vel) > Velocidad Then ' Borra el contenido Objeto_Scroll.Cls ' Dibuja el texto Call DrawText(Objeto_Scroll.hdc, _ Texto, -1, _ ObjetoRect, &H1 Or &H10) With ObjetoRect .Top = .Top - 1 .Bottom = .Bottom - 1 ' Si llegó arriba de todo comienza de nuevo el scroll _ reseteando los valores top y bottom If .Top < -(r_Height_Texto) Then .Top = Objeto_Scroll.ScaleHeight .Bottom = r_Height_Texto + Objeto_Scroll.ScaleHeight End If End With t_Vel = GetTickCount() End If DoEvents Wend End Sub
Título: Re: Duda con codigo efecto scroll
Publicado por: Elemental Code en 21 Mayo 2012, 06:47 am
la primer llamada a la api escribe valores en las variables: ObjetoRect.Right ObjetoRect.Bottom ademas la primera api verifica si hay errores termina la ejecucion. La segunda llamada a la api se encarga de escribir le texto. Probablemente alguien de aca que entienda mas pueda explicarte bien, esto lo descubri con muuuuuuchos debug.print :P
Título: Re: Duda con codigo efecto scroll
Publicado por: noele1995 en 21 Mayo 2012, 19:11 pm
Bueno lo he revisado y lo he comentado creo que esta todo bien jejeje ;D si veis algo raro decirmelo o si alguien no lo entiende Public Sub Comenzar_Scroll(Objeto_Scroll As PictureBox, Texto As String, Velocidad As Long, Color_Fuente As Long) Dim r_Height_Texto As Long Dim ObjetoRect As RECT Dim t_Vel As Long Dim Ret As Long 'Aqui esta poniendo las propiedades pasa que se pueda With Objeto_Scroll ' escibir en el picture o form .ScaleMode = vbPixels 'modo pixel .AutoRedraw = True 'para que se actualizae el pintado .ForeColor = Color_Fuente 'color de fuente .FontSize = 16 'Tamano de la letra End With 'Aqui lo que hace es una primera llamada a la api para comprobar ' que puede pintar el texto sobre el objeto dado Ret = DrawText(Objeto_Scroll.hdc, Texto, -1, ObjetoRect, &H400) 'Si el retorno es 0 entonces no puede pintar correctamente el objeto 'Y por lo tanto sale If Ret = 0 Then MsgBox " Error ", vbCritical: Exit Sub With ObjetoRect 'Configura la posición inicial del área donde dibujar el texto .Top = Objeto_Scroll.ScaleHeight .Left = 0 .Right = Objeto_Scroll.ScaleWidth r_Height_Texto = .Bottom .Bottom = .Bottom + Objeto_Scroll.ScaleHeight End With 'Esta variable si está en True detiene el scroll Finalizar = False 'Si no cambias el valor de variable sigue dibujando el texto 'infinitamente While Finalizar = False 'Aqui hace una comprobacion de la velocidad If (GetTickCount() - t_Vel) > Velocidad Then ' Borra el contenido ya pintado en el objeto Objeto_Scroll.Cls ' Dibuja el texto Call DrawText(Objeto_Scroll.hdc, Texto, -1, ObjetoRect, &H1 Or &H10) With ObjetoRect 'Cambia la proxima posicion donde se escribira el texto .Top = .Top - 1 .Bottom = .Bottom - 1 ' Si llegó arriba de todo comienza de nuevo el scroll reseteando los valores top y bottom If .Top < -(r_Height_Texto) Then .Top = Objeto_Scroll.ScaleHeight .Bottom = r_Height_Texto + Objeto_Scroll.ScaleHeight End If End With 'Cambia la variable para futuras comprobaciones de velocidad t_Vel = GetTickCount() End If DoEvents Wend End Sub
Salu2, Noele1995
Título: Re: Duda con codigo efecto scroll
Publicado por: BlackZeroX en 21 Mayo 2012, 19:14 pm
el problema esta en el &H400 de la primera llamada el cual es DT_CALRECT y segun la MSDN dice: http://msdn.microsoft.com/en-us/library/ms901121.aspx Determines the width and height of the rectangle. If the rectangle includes multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the rectangle includes only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text. Before calling DrawText, an application must set the right and bottom members of the RECT structure pointed to by lpRect. These members are updated with the call to DrawText.
google translate: Determina la anchura y la altura del rectángulo. Si el rectángulo incluye varias líneas de texto, DrawText utiliza la anchura del rectángulo apuntada por el parámetro lpRect y se extiende la base del rectángulo para limitar la última línea de texto. Si el rectángulo incluye sólo una línea de texto, DrawText modifica el lado derecho del rectángulo de modo que delimita el último carácter de la línea. En cualquier caso, DrawText devuelve la altura del texto formateado pero no saca el texto. Antes de llamar a DrawText, una aplicación debe establecer los miembros derecho e inferior de la estructura RECT que apunta lpRect. Estos miembros se actualizan con la llamada a DrawText.
Dulces Lunas!¡.
Título: Re: Duda con codigo efecto scroll
Publicado por: [Kayser] en 21 Mayo 2012, 20:29 pm
Gracias por las respuestas creo que ya entiendo el codigo un saludo
|