Utilisation de dwmapi.dll avec vb.net

Description

Ceci est le module pour utiliser les apis de DWM (nouveauté Vista) avec Visual Basic. Ce code est une adaptation du tutoriel pour C# disponible ici : http://msdn.microsoft.com/fr-fr/magazine/cc163435.aspx

Exemples d'utilisation :

' obtient l'état de la transparence
If DwmIsCompositionEnabled() = True Then MsgBox("Vous utilisez le thème Vista Aero")

' étend la transparence sur le haut de la fenêtre de 50 pixels
DwmExtendFrameIntoClientArea(Me.Handle, New Margins(-2, 0, 0, 50))

' désactive la transparence (Windows Aero)
DwmEnableComposition(False)

Pour plus d'exemples d'utilisation, téléchargez le projet ci-dessous

Source / Exemple :


Imports System.Runtime.InteropServices
Module DWM
    <StructLayout(LayoutKind.Sequential)> _
Public Structure Margins
        Public Sub New(ByVal left As Integer, ByVal right As Integer, ByVal top As Integer, ByVal bottom As Integer)
            _left = left
            _right = right
            _top = top
            _bottom = bottom
        End Sub
        Public Sub New(ByVal allMargins As Integer)
            _left = allMargins
            _right = allMargins
            _top = allMargins
            _bottom = allMargins
        End Sub
        Private _left As Integer
        Private _right As Integer
        Private _top As Integer
        Private _bottom As Integer
        Public Property Left() As Integer
            Get
                Return _left
            End Get
            Set(ByVal value As Integer)
                _left = value
            End Set
        End Property
        Public Property Right() As Integer
            Get
                Return _right
            End Get
            Set(ByVal value As Integer)
                _right = value
            End Set
        End Property
        Public Property Top() As Integer
            Get
                Return _top
            End Get
            Set(ByVal value As Integer)
                _top = value
            End Set
        End Property
        Public Property Bottom() As Integer
            Get
                Return _bottom
            End Get
            Set(ByVal value As Integer)
                _bottom = value
            End Set
        End Property
        Public ReadOnly Property IsMarginless() As Boolean
            Get
                Return (_left < 0 AndAlso _right < 0 AndAlso _top < 0 AndAlso _bottom < 0)
            End Get
        End Property
        Public ReadOnly Property IsNull() As Boolean
            Get
                Return (_left = 0 AndAlso _right = 0 AndAlso _top = 0 AndAlso _bottom = 0)
            End Get
        End Property
    End Structure
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure Rect
        Public stleft, sttop, stright, stbottom As Integer
        Public Sub Rect(ByVal left As Integer, ByVal top As Integer, ByVal right As Integer, ByVal bottom As Integer)
            stleft = left
            sttop = top
            stright = right
            stbottom = bottom
        End Sub
    End Structure
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure Dwm_ThumbnailProperties
        Public dwFlags As UInt64
        Public rcDestination As RECT
        Public rcSource As RECT
        Public opacity As Byte
        <MarshalAs(UnmanagedType.Bool)> _
        Public fVisible As Boolean
        <MarshalAs(UnmanagedType.Bool)> _
        Public fSourceClientAreaOnly As Boolean
        Public Const DWM_TNP_RECTDESTINATION As UInt32 = &H1
        Public Const DWM_TNP_RECTSOURCE As UInt32 = &H2
        Public Const DWM_TNP_OPACITY As UInt32 = &H4
        Public Const DWM_TNP_VISIBLE As UInt32 = &H8
        Public Const DWM_TNP_SOURCECLIENTAREAONLY As UInt32 = &H10
    End Structure
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure Dwm_BlurBehind
        Public dwFlags As UInt64
        <MarshalAs(UnmanagedType.Bool)> _
        Public fEnable As Boolean
        Public hRegionBlur As IntPtr
        <MarshalAs(UnmanagedType.Bool)> _
        Public fTransitionOnMaximized As Boolean
        Public Const DWM_BB_ENABLE As UInt32 = &H1
        Public Const DWM_BB_BLURREGION As UInt32 = &H2
        Public Const DWM_BB_TRANSITIONONMAXIMIZED As UInt32 = &H4
    End Structure
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmExtendFrameIntoClientArea(ByVal hWnd As IntPtr, ByRef pMarInset As Margins) As Integer
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmIsCompositionEnabled() As Boolean
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmEnableBlurBehindWindow(ByVal hWnd As IntPtr, ByVal pBlurBehind As Dwm_BlurBehind) As Integer
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmEnableComposition(ByVal bEnable As Boolean)
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmGetColorizationColor(ByVal pcrColorization As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal pfOpaqueBlend As Boolean)
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmRegisterThumbnail(ByVal dest As IntPtr, ByVal source As IntPtr) As IntPtr
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmUnregisterThumbnail(ByVal hThumbnail As IntPtr)
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmUpdateThumbnailProperties(ByVal hThumbnail As IntPtr, ByVal props As Dwm_ThumbnailProperties)
    End Function
    <DllImport("dwmapi.dll", PreserveSig:=False)> _
    Public Function DwmQueryThumbnailSourceSize(ByVal hThumbnail As IntPtr, ByVal size As Size)
    End Function
End Module

Conclusion :


Pour plus de documentation, visitez les sites web ci-dessous :
http://msdn.microsoft.com/fr-fr/magazine/cc163435.aspx
http://msdn.microsoft.com/en-us/library/aa969540.aspx
http://badger.developpez.com/tutoriels/dotnet/effet-glass-vista/

Note : pour l'instant, il semble avoir une erreur avec la fonction DwmEnableBlurBehindWindow

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.