Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: [Kayser] en 19 Mayo 2012, 22:41 pm



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?
Código:
'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:

Código:
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



Código
  1. Public Sub Comenzar_Scroll(Objeto_Scroll As PictureBox, Texto As String, Velocidad As Long, Color_Fuente As Long)
  2. Dim r_Height_Texto As Long
  3. Dim ObjetoRect As RECT
  4. Dim t_Vel As Long
  5. Dim Ret As Long
  6.  
  7. 'Aqui esta poniendo las propiedades pasa que se pueda
  8. With Objeto_Scroll  ' escibir en el picture o form
  9.    .ScaleMode = vbPixels   'modo pixel
  10.    .AutoRedraw = True  'para que se actualizae el pintado
  11.    .ForeColor = Color_Fuente 'color de fuente
  12.    .FontSize = 16  'Tamano de la letra
  13. End With
  14.  
  15.  
  16. 'Aqui lo que hace es una primera llamada a la api para comprobar
  17. ' que puede pintar el texto sobre el objeto dado
  18. Ret = DrawText(Objeto_Scroll.hdc, Texto, -1, ObjetoRect, &H400)
  19.  
  20. 'Si el retorno es 0 entonces no puede pintar correctamente el objeto
  21. 'Y por lo tanto sale
  22. If Ret = 0 Then MsgBox " Error ", vbCritical: Exit Sub
  23.  
  24.    With ObjetoRect
  25.  
  26.        'Configura la posición inicial del área donde dibujar el texto
  27.        .Top = Objeto_Scroll.ScaleHeight
  28.        .Left = 0
  29.        .Right = Objeto_Scroll.ScaleWidth
  30.         r_Height_Texto = .Bottom
  31.        .Bottom = .Bottom + Objeto_Scroll.ScaleHeight
  32.    End With
  33.  
  34. 'Esta variable si está en True detiene el scroll
  35. Finalizar = False
  36.  
  37.  
  38. 'Si no cambias el valor de variable sigue dibujando el texto
  39. 'infinitamente
  40. While Finalizar = False
  41.  
  42.    'Aqui hace una comprobacion de la velocidad
  43.    If (GetTickCount() - t_Vel) > Velocidad Then
  44.  
  45.        ' Borra el contenido ya pintado en el objeto
  46.        Objeto_Scroll.Cls
  47.  
  48.        ' Dibuja el texto
  49.        Call DrawText(Objeto_Scroll.hdc, Texto, -1, ObjetoRect, &H1 Or &H10)
  50.  
  51.        With ObjetoRect
  52.            'Cambia la proxima posicion donde se escribira el texto
  53.            .Top = .Top - 1
  54.            .Bottom = .Bottom - 1
  55.  
  56.            ' Si llegó arriba de todo comienza de nuevo el scroll reseteando los valores top y bottom
  57.            If .Top < -(r_Height_Texto) Then
  58.                .Top = Objeto_Scroll.ScaleHeight
  59.                .Bottom = r_Height_Texto + Objeto_Scroll.ScaleHeight
  60.            End If
  61.  
  62.        End With
  63.  
  64.        'Cambia la variable para futuras comprobaciones de velocidad
  65.        t_Vel = GetTickCount()
  66.  
  67.    End If
  68.  
  69.    DoEvents
  70. Wend
  71. 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
Citar
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:
Citar
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