rss

[VB6] Rendre une forme transparente

Publié par lermite222, dernière mise à jour le samedi 28 juin 2008 à 19:40:57 par fleursdespyrenees


Introduction


Cette astuce explique comment se servir de la transparence..
  • Sur l'ensemble de la forme, contrôles constituants compris, suivant un ratio
  • Le fond de la forme invisible et les contrôles constituants suivant un ratio

Restriction


Ne fonctionne pas sur des couleurs avec signe négatif
Il faut mettre une couleur de la palette

Dans un module


Option Explicit
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 bDefaut As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE       As Long = (-20)
Private Const LWA_COLORKEY      As Long = &H1
Private Const LWA_Defaut         As Long = &H2
Private Const WS_EX_LAYERED     As Long = &H80000

'
Public Function Transparence(ByVal hWnd As Long, Optional ByVal Coul As Long = vbBlack, _
    Optional ByVal PcTransp As Byte = 255, Optional ByVal TrMode As Boolean = True) As Boolean
' Retourne : True s'il n'y a pas eu d'erreur.
' hWnd   : hWnd de la fenêtre à rendre transparente
' Coul : Couleur à rendre transparente si TrMode=False
' PcTransp  : 0 à 255 >> 0 = transparent  -:- 255 = Opaque
Dim VoirStyle As Long
    On Error GoTo Sortie
    VoirStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
    If VoirStyle <> (VoirStyle Or WS_EX_LAYERED) Then
        VoirStyle = (VoirStyle Or WS_EX_LAYERED)
        Call SetWindowLong(hWnd, GWL_EXSTYLE, VoirStyle)
    End If
    Transparence = (SetLayeredWindowAttributes(hWnd, Coul, PcTransp, IIf(TrMode, LWA_COLORKEY Or LWA_Defaut, LWA_COLORKEY)) <> 0)
    
Sortie:
    If Not Err.Number = 0 Then Err.Clear
End Function

Public Sub ActiveTransparence(M As Form, d As Boolean, F As Boolean, _
     T_Transparence As Integer, Optional Couleur As Long)
Dim B As Boolean
        If d And F Then
        'Rend la couleur (ici la couleur du fond de la forme) transparente
        'au taux de T_Transparence
            B = Transparence(M.hWnd, Couleur, T_Transparence, False)
        ElseIf d Then
            'Rend toute la forme, y compris les composants, transparente
            'au taux de T_Transparence
            B = Transparence(M.hWnd, 0, T_Transparence, True)
        Else
            'Restaure la forme opaque.
            B = Transparence(M.hWnd, , 255, True)
        End If
End Sub

Exemple dans une forme


Private Sub Form_Load()
Dim i As Integer
    'Ex: tout transparent à 140/255ème
    'ActiveTransparence Me, True, False, 140, Me.BackColor
    'Ex: Forme transparent, composant visible à 140/255ème
    'ActiveTransparence Me, True, True, 140, Me.BackColor
    
    'Exemple d'affichage de la forme par gradation de la transparence
    ActiveTransparence Me, True, False, 0
    Me.Show
    For i = 0 To 255 Step 3
        ActiveTransparence Me, True, False, i
        Me.Refresh
    Next i
End Sub

Autres Astuces dans la catégorie Visual Basic

Logiciels pertinents trouvés dans les téléchargements
Télécharger HP USB Disk Storage Format Tool 2.0.6HP USB Disk Storage Format Tool - HP USB Disk Storage Format Tool est un petit utilitaire permettant de formater une clé USB ou un lecteur MP4 . Il permet...Catégorie: Formatage/Partitionnement
Licence: Freeware/gratuit
Télécharger Switch Sound Format Converter 1.05Switch Sound Format Converter - Switchest un convertisseur de formats audio pour Windows ou Mac, capable de convertir des fichiers audio à partir de...Catégorie: Conversion audio
Licence: Freeware/gratuit
Télécharger PerfectToolsXP 2PerfectToolsXP - PerfectToolsXP 2 vous permettra d'obtenir le maximum de Windows XP. Il rassemble 42 modules différents, un tout en un, se...Catégorie: Optimisation
Licence: Freeware/gratuit
Télécharger VLC 0.8.6iVLC - VLC Media Player est un lecteur multimédia sous capable de lire de nombreux formats audio et vidéo (MPEG-1, MPEG-2, MPEG-4,...Catégorie: Lecteurs vidéo
Licence: Open Source
Plus de logiciels gratuits sur « Rendre une forme transparente »