Quand setlayeredwindowattributes rencontre timer...

Description

On sait gérer la transparence des formulaires: avec la fonction SetLayeredWindowAttributes().
Associé à un Timer, on obtient un effet d'animation que l'on retrouve sur XP lors d'introductions.

-Copiez le code ci-dessous et enregistrez-le sous FrmTranspa.frm
-double-cliquez dessus,
-Dans VB, appuyez sur F5!

Source / Exemple :


VERSION 5.00
Begin VB.Form FrmTranspa 
   BackColor       =   &H00404040&
   BorderStyle     =   1  'Fixed Single
   Caption         =   "FrmTranspa"
   ClientHeight    =   4710
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6975
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4710
   ScaleWidth      =   6975
   StartUpPosition =   2  'CenterScreen
   Begin VB.ListBox List2 
      BackColor       =   &H00404040&
      ForeColor       =   &H00E0E0E0&
      Height          =   3570
      Left            =   4920
      TabIndex        =   6
      Top             =   360
      Width           =   1455
   End
   Begin VB.ListBox List1 
      BackColor       =   &H00404040&
      ForeColor       =   &H00E0E0E0&
      Height          =   3570
      Left            =   3360
      TabIndex        =   5
      Top             =   360
      Width           =   1455
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Quitter"
      Height          =   375
      Left            =   480
      TabIndex        =   0
      Top             =   3360
      Width           =   1575
   End
   Begin VB.Timer TimerStart 
      Enabled         =   0   'False
      Interval        =   20
      Left            =   480
      Top             =   2400
   End
   Begin VB.Timer TimerQuit 
      Enabled         =   0   'False
      Interval        =   20
      Left            =   960
      Top             =   2400
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      ForeColor       =   &H00008000&
      Height          =   255
      Index           =   3
      Left            =   480
      TabIndex        =   4
      Top             =   1440
      Width           =   1335
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      ForeColor       =   &H00008000&
      Height          =   255
      Index           =   2
      Left            =   480
      TabIndex        =   3
      Top             =   1080
      Width           =   1335
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      ForeColor       =   &H00008000&
      Height          =   255
      Index           =   1
      Left            =   480
      TabIndex        =   2
      Top             =   720
      Width           =   1335
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      ForeColor       =   &H00008000&
      Height          =   255
      Index           =   0
      Left            =   480
      TabIndex        =   1
      Top             =   360
      Width           =   1335
   End
   Begin VB.Shape Shape3 
      Height          =   2055
      Left            =   120
      Shape           =   4  'Rounded Rectangle
      Top             =   2280
      Width           =   2655
   End
   Begin VB.Shape Shape2 
      BackColor       =   &H00404040&
      BackStyle       =   1  'Opaque
      BorderColor     =   &H0080C0FF&
      Height          =   4215
      Left            =   2880
      Shape           =   4  'Rounded Rectangle
      Top             =   120
      Width           =   3975
   End
   Begin VB.Shape Shape1 
      BackColor       =   &H00000000&
      BackStyle       =   1  'Opaque
      Height          =   2055
      Left            =   120
      Shape           =   4  'Rounded Rectangle
      Top             =   120
      Width           =   2655
   End
End
Attribute VB_Name = "FrmTranspa"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_COLORKEY = &H1&
Const LWA_ALPHA = &H2&
Dim nx As Byte

Private Sub SetTransparence(hWnd As Long, Alpha As Byte)
    Dim Param As Long
    Param = GetWindowLong(hWnd, GWL_EXSTYLE)
    Call SetWindowLong(hWnd, GWL_EXSTYLE, Param Or WS_EX_LAYERED)
    Call SetLayeredWindowAttributes(hWnd, 0, Alpha, LWA_ALPHA)
End Sub

Private Sub Form_Load()
    Call SetTransparence(Me.hWnd, 0)
    nx = 0: TimerStart.Enabled = True
End Sub
Private Sub TimerStart_Timer()
    If nx <= 250 Then
        nx = nx + 5
        Call SetTransparence(Me.hWnd, nx)
    Else: TimerStart.Enabled = False
    End If
End Sub

Private Sub Command1_Click()
    nx = 255: TimerQuit.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
nx = 255: TimerQuit.Enabled = True
Cancel = 1
End Sub
Private Sub TimerQuit_Timer()
    If nx <= 37 Then
        End
    Else
        nx = nx - 5
        Call SetTransparence(Me.hWnd, nx)
    End If
End Sub

Conclusion :


Le byte Alpha dans SetTransparence() varie de 0 à 255 (255= opaque).

Pour changer la rapidité de la transition: modifier la propriété Interval du contrôle Timer.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.