• Viernes 26 de Abril de 2024, 22:00

Autor Tema:  Armando Formularios Transparentes.  (Leído 895 veces)

L3andro

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Armando Formularios Transparentes.
« en: Miércoles 18 de Julio de 2007, 17:21 »
0
Hola muchach@s, bueno este es mi primer post aqui en Solo Codigo, llegue hasta aqui por recomendacion de unos amigos que al ver que quiero introducirme un poco mas en ASM e ir aprendiendo me recomendaron este sitio. Para que mi primer post no sean preguntas quiero colaborar con esto quizas a alguien le sea de utilidad.

 A continuación veremos como podemos armar un formulario "invisible", esto podria tener varias utilidades pero eso ya dependera de la que ustedes quieran darle, por ejemplo podriamos dejar un text o un command volando, asi que bueno comenzemos.

Código: Text
  1.  
  2. Option Explicit
  3. 'Declaraciones de los diferentes tipos de regiones a crear
  4. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  5. Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  6. Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
  7. Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
  8. 'POINTAPI tipo requerido para CreatePolygonRgn
  9. Private Type POINTAPI
  10.         X As Long
  11.         Y As Long
  12. End Type
  13. 'Fija la region
  14. Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
  15. 'Combina la region
  16. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
  17. 'Tipo de combinacion
  18. Const RGN_XOR = 3
  19. '----------------------------------------------------------------------------------------------------
  20. Public Sub MakeTransparent(TransForm As Form)
  21. Dim ErrorTest As Double
  22.     'en caso de que haya un error, se ignora
  23.     On Error Resume Next
  24.     
  25.     Dim Regn As Long
  26.     Dim TmpRegn As Long
  27.     Dim TmpControl As Control
  28.     Dim LinePoints(4) As POINTAPI
  29.     
  30.     'Puesto que las API trabajan en pixels, cambiamos el modo de escala a pixels
  31.     TransForm.ScaleMode = 3
  32.     
  33.     'Debe ejecutarse sobre un formulario son bordes.
  34.     If TransForm.BorderStyle <> 0 Then MsgBox "Cambia el borderstyle a 0!", vbCritical, "ACK!": End
  35.     
  36.     'Hace todo invisible
  37.     Regn = CreateRectRgn(0, 0, 0, 0)
  38.     
  39.     'Un bucle para controlar cada control en el formulario
  40.     For Each TmpControl In TransForm
  41.     
  42.         'Si el control es una linea...
  43.         If TypeOf TmpControl Is Line Then
  44.             'Comprueba la inclinacion
  45.             If Abs((TmpControl.Y1 - TmpControl.Y2) / (TmpControl.X1 - TmpControl.X2)) > 1 Then
  46.                 'Si es mas vertical que horizontal entonces..
  47.                 'Fija los puntos
  48.                 LinePoints(0).X = TmpControl.X1 - 1
  49.                 LinePoints(0).Y = TmpControl.Y1
  50.                 LinePoints(1).X = TmpControl.X2 - 1
  51.                 LinePoints(1).Y = TmpControl.Y2
  52.                 LinePoints(2).X = TmpControl.X2 + 1
  53.                 LinePoints(2).Y = TmpControl.Y2
  54.                 LinePoints(3).X = TmpControl.X1 + 1
  55.                 LinePoints(3).Y = TmpControl.Y1
  56.             Else
  57.                 'Si es mas horizontal que vertical, entonces...
  58.                 'Fija los puntos
  59.                 LinePoints(0).X = TmpControl.X1
  60.                 LinePoints(0).Y = TmpControl.Y1 - 1
  61.                 LinePoints(1).X = TmpControl.X2
  62.                 LinePoints(1).Y = TmpControl.Y2 - 1
  63.                 LinePoints(2).X = TmpControl.X2
  64.                 LinePoints(2).Y = TmpControl.Y2 + 1
  65.                 LinePoints(3).X = TmpControl.X1
  66.                 LinePoints(3).Y = TmpControl.Y1 + 1
  67.             End If
  68.             'Crea el nuevo poligono con los puntos
  69.             TmpRegn = CreatePolygonRgn(LinePoints(0), 4, 1)
  70.             
  71.         'Si el control es una figura...
  72.         ElseIf TypeOf TmpControl Is Shape Then
  73.             
  74.             'si es asi, comprobamos el tipo
  75.             If TmpControl.Shape = 0 Then
  76.             'Es un rectangulo
  77.                 TmpRegn = CreateRectRgn(TmpControl.Left, TmpControl.Top, TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height)
  78.             ElseIf TmpControl.Shape = 1 Then
  79.             'Es un cuadrado
  80.                 If TmpControl.Width < TmpControl.Height Then
  81.                     TmpRegn = CreateRectRgn(TmpControl.Left, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2, TmpControl.Left + TmpControl.Width, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width)
  82.                 Else
  83.                     TmpRegn = CreateRectRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2, TmpControl.Top, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height, TmpControl.Top + TmpControl.Height)
  84.                 End If
  85.             ElseIf TmpControl.Shape = 2 Then
  86.             'Es un ovalo
  87.                 TmpRegn = CreateEllipticRgn(TmpControl.Left, TmpControl.Top, TmpControl.Left + TmpControl.Width + 0.5, TmpControl.Top + TmpControl.Height + 0.5)
  88.             ElseIf TmpControl.Shape = 3 Then
  89.             'Es un circulo
  90.                 If TmpControl.Width < TmpControl.Height Then
  91.                     TmpRegn = CreateEllipticRgn(TmpControl.Left, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2, TmpControl.Left + TmpControl.Width + 0.5, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width + 0.5)
  92.                 Else
  93.                     TmpRegn = CreateEllipticRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2, TmpControl.Top, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height + 0.5, TmpControl.Top + TmpControl.Height + 0.5)
  94.                 End If
  95.             ElseIf TmpControl.Shape = 4 Then
  96.             'Es un rectangulo redondeado
  97.                 If TmpControl.Width > TmpControl.Height Then
  98.                     TmpRegn = CreateRoundRectRgn(TmpControl.Left, TmpControl.Top, TmpControl.Left + TmpControl.Width + 1, TmpControl.Top + TmpControl.Height + 1, TmpControl.Height / 4, TmpControl.Height / 4)
  99.                 Else
  100.                     TmpRegn = CreateRoundRectRgn(TmpControl.Left, TmpControl.Top, TmpControl.Left + TmpControl.Width + 1, TmpControl.Top + TmpControl.Height + 1, TmpControl.Width / 4, TmpControl.Width / 4)
  101.                 End If
  102.             ElseIf TmpControl.Shape = 5 Then
  103.        'Es un cuadrado redondeado
  104.                 If TmpControl.Width > TmpControl.Height Then
  105.                     TmpRegn = CreateRoundRectRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2, TmpControl.Top, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height + 1, TmpControl.Top + TmpControl.Height + 1, TmpControl.Height / 4, TmpControl.Height / 4)
  106.                 Else
  107.                     TmpRegn = CreateRoundRectRgn(TmpControl.Left, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2, TmpControl.Left + TmpControl.Width + 1, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width + 1, TmpControl.Width / 4, TmpControl.Width / 4)
  108.                 End If
  109.             End If
  110.             
  111.             'Si el control es una figura con fondo transparente
  112.             If TmpControl.BackStyle = 0 Then
  113.                 
  114.                 'Combinamos la region en memoria y creamos una nueva
  115.                 CombineRgn Regn, Regn, TmpRegn, RGN_XOR
  116.                 
  117.                 If TmpControl.Shape = 0 Then
  118.                 'Rectangulo
  119.                     TmpRegn = CreateRectRgn(TmpControl.Left + 1, TmpControl.Top + 1, TmpControl.Left + TmpControl.Width - 1, TmpControl.Top + TmpControl.Height - 1)
  120.                 ElseIf TmpControl.Shape = 1 Then
  121.                 'Cuadrado
  122.                     If TmpControl.Width < TmpControl.Height Then
  123.                         TmpRegn = CreateRectRgn(TmpControl.Left + 1, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + 1, TmpControl.Left + TmpControl.Width - 1, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width - 1)
  124.                     Else
  125.                         TmpRegn = CreateRectRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + 1, TmpControl.Top + 1, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height - 1, TmpControl.Top + TmpControl.Height - 1)
  126.                     End If
  127.                 ElseIf TmpControl.Shape = 2 Then
  128.                 'Ovalo
  129.                     TmpRegn = CreateEllipticRgn(TmpControl.Left + 1, TmpControl.Top + 1, TmpControl.Left + TmpControl.Width - 0.5, TmpControl.Top + TmpControl.Height - 0.5)
  130.                 ElseIf TmpControl.Shape = 3 Then
  131.                 'Circulo
  132.                     If TmpControl.Width < TmpControl.Height Then
  133.                         TmpRegn = CreateEllipticRgn(TmpControl.Left + 1, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + 1, TmpControl.Left + TmpControl.Width - 0.5, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width - 0.5)
  134.                     Else
  135.                         TmpRegn = CreateEllipticRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + 1, TmpControl.Top + 1, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height - 0.5, TmpControl.Top + TmpControl.Height - 0.5)
  136.                     End If
  137.                 ElseIf TmpControl.Shape = 4 Then
  138.          'Rectangulo redondeado
  139.                     If TmpControl.Width > TmpControl.Height Then
  140.                         TmpRegn = CreateRoundRectRgn(TmpControl.Left + 1, TmpControl.Top + 1, TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height, TmpControl.Height / 4, TmpControl.Height / 4)
  141.                     Else
  142.                         TmpRegn = CreateRoundRectRgn(TmpControl.Left + 1, TmpControl.Top + 1, TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height, TmpControl.Width / 4, TmpControl.Width / 4)
  143.                     End If
  144.                 ElseIf TmpControl.Shape = 5 Then
  145.                 'Cuadrado redondeado
  146.                     If TmpControl.Width > TmpControl.Height Then
  147.                         TmpRegn = CreateRoundRectRgn(TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + 1, TmpControl.Top + 1, TmpControl.Left + (TmpControl.Width - TmpControl.Height) / 2 + TmpControl.Height, TmpControl.Top + TmpControl.Height, TmpControl.Height / 4, TmpControl.Height / 4)
  148.                     Else
  149.                         TmpRegn = CreateRoundRectRgn(TmpControl.Left + 1, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + 1, TmpControl.Left + TmpControl.Width, TmpControl.Top + (TmpControl.Height - TmpControl.Width) / 2 + TmpControl.Width, TmpControl.Width / 4, TmpControl.Width / 4)
  150.                     End If
  151.                 End If
  152.             End If
  153.         Else
  154.                 'Crea una region rectangular con estos parametros
  155.                 TmpRegn = CreateRectRgn(TmpControl.Left, TmpControl.Top, TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height)
  156.             
  157.         End If
  158.             
  159.             'Comprueba que el control tiene ancho o conseguiremos extraños resultados
  160.             
  161.             ErrorTest = 0
  162.             ErrorTest = TmpControl.Width
  163.             If ErrorTest <> 0 Or TypeOf TmpControl Is Line Then
  164.                 'Combina las regiones
  165.                 CombineRgn Regn, Regn, TmpRegn, RGN_XOR
  166.             End If
  167.         
  168.     Next TmpControl
  169.     
  170.     'Crea las regiones
  171.     SetWindowRgn TransForm.hwnd, Regn, True
  172.     
  173.  
  174. End Sub
  175.  

 Bueno todo corresponde a las declaraciones del form, y deben tener en cuenta que el "Option Explicit" va primero si lo queremos incluir en otras declaraciones.
Ahora, para hacer el formulario transparente, basta que en el form_load coloquemos: --> MakeTransparent Me

 Bueno con eso basta tendremos un buen efecto en nuestros formularios de suspencion de los objetos y textos.

Saludos y espero que a alguien le sea de utilidad  ;)

L3andro.
Single I Want To learn as much that Likes
Studying ASM, You can Help Me?