Aero glass effect with vb6

Soyez le premier à donner votre avis sur cette source.

Snippet vu 15 482 fois - Téléchargée 22 fois

Contenu du snippet

Juste un petit tutoriel pour démontrer l'utilisation des APis du Desktop Windows Manager de Vista en Visual Basic 6.
Lancez VB6, choisissez Standard EXE, ajoutez une Form et copiez le code. F5 pour apercevoir le résultat.

Source / Exemple :


Option Explicit

Private m_ScaleX As Long
Private m_ScaleY As Long
Private m_hdc As Long
Private m_hwnd As Long
Dim m_hTheme As Long
Dim m_hFont As Long
Dim m_lFontSize As Long

Private Type MARGINS
  Left As Long
  Right As Long
  Top As Long
  Bottom As Long
End Type

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Type BITMAPINFOHEADER
  biSize As Long
  biWidth As Long
  biHeight As Long
  biPlanes As Integer
  biBitCount As Integer
  biCompression As Long
  biSizeImage As Long
  biXPelsPerMeter As Long
  biYPelsPerMeter As Long
  biClrUsed As Long
  biClrImportant As Long
End Type

Private Type RGBQUAD
  rgbBlue As Byte
  rgbGreen As Byte
  rgbRed As Byte
  rgbReserved As Byte
End Type

Private Type BITMAPINFO
  bmiHeader As BITMAPINFOHEADER
  bmiColors As RGBQUAD
End Type

Private Type DTTOPTS
    dwSize As Long
    dwFlags As Long
    crText As Long
    crBorder As Long
    crShadow As Long
    eTextShadowType As Long
    ptShadowOffset As POINTAPI
    iBorderSize As Long
    iFontPropId As Long
    iColorPropId As Long
    iStateId As Long
    fApplyOverlay As Long
    iGlowSize As Long
End Type

Private Type LOGFONT
  lfHeight As Long
  lfWidth As Long
  lfEscapement As Long
  lfOrientation As Long
  lfWeight As Long
  lfItalic As Byte
  lfUnderline As Byte
  lfStrikeOut As Byte
  lfCharSet As Byte
  lfOutPrecision As Byte
  lfClipPrecision As Byte
  lfQuality As Byte
  lfPitchAndFamily As Byte
  lfFaceName(31) As Byte
End Type

Private Type NONCLIENTMETRICS
  cbSize As Long
  iBorderWidth As Long
  iScrollWidth As Long
  iScrollHeight As Long
  iCaptionWidth As Long
  iCaptionHeight As Long
  lfCaptionFont As LOGFONT
  iSMCaptionWidth As Long
  iSMCaptionHeight As Long
  lfSMCaptionFont As LOGFONT
  iMenuWidth As Long
  iMenuHeight As Long
  lfMenuFont As LOGFONT
  lfStatusFont As LOGFONT
  lfMessageFont As LOGFONT
End Type

Const DIB_RGB_COLORS = 0
Const BI_RGB = 0
Const DTT_GLOWSIZE = 2048
Const DTT_COMPOSITED = 8192
Const SRCCOPY As Long = &HCC0020
Const DT_SINGLELINE = &H20
Const DT_CENTER = &H1
Const DT_VCENTER = &H4
Const DT_NOPREFIX = &H800
Const SPI_GETNONCLIENTMETRICS = 41
Const DEFAULT_QUALITY = 0
Const NONANTIALIASED_QUALITY = 3
Const ANTIALIASED_QUALITY = 4
Const CLEARTYPE_QUALITY = 5

Private Declare Function DwmExtendFrameIntoClientArea Lib "dwmapi.dll" (ByVal hWnd As Long, margin As MARGINS) As Long
Private Declare Function DwmIsCompositionEnabled Lib "dwmapi" (ByRef pfEnabled As Long) As Long
'
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As NONCLIENTMETRICS, ByVal fuWinIni As Long) As Long
'
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, ByRef pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (ByRef lpLogFont As LOGFONT) As Long
'
Private Declare Function OpenThemeData Lib "uxtheme" (ByVal hWnd As Long, ByVal pszClassList As String) As Long
Private Declare Function CloseThemeData Lib "uxtheme" (ByVal hTheme As Long) As Long
Private Declare Function DrawThemeTextEx Lib "uxtheme" (ByVal hTheme As Long, ByVal hdc As Long, ByVal iPartId As Long, _
                                                        ByVal iStateId As Long, ByVal pszText As String, ByVal iCharCount As Long, ByVal dwFlags As Long, pRect As RECT, pOptions As DTTOPTS) As Long

Private Sub Form_Load()
  Dim enabled As Long
  Dim x As Long
  Dim mg As MARGINS
  Dim lpFont As LOGFONT
  Dim ncm As NONCLIENTMETRICS
  
  With Screen
    m_ScaleX = .TwipsPerPixelX
    m_ScaleY = .TwipsPerPixelY
  End With
  
  With Me
    m_hdc = .hdc
    m_hwnd = .hWnd
    m_hTheme = OpenThemeData(.hWnd, StrConv("Window", vbUnicode))
  End With
  
  With mg
    .Left = -1
    .Right = -1
    .Top = -1
    .Bottom = -1
  End With
  
  With ncm
    .cbSize = Len(ncm)
    Call SystemParametersInfo(SPI_GETNONCLIENTMETRICS, Len(ncm), ncm, 0)
    lpFont = .lfMessageFont
  End With
  
  With lpFont
    .lfWeight = 700
    .lfHeight = .lfHeight * 1.5 ' FontSize == 18
    m_lFontSize = -.lfHeight
    .lfQuality = CLEARTYPE_QUALITY
  End With
  
  m_hFont = CreateFontIndirect(lpFont)
  
  Call DwmIsCompositionEnabled(enabled)
  If (enabled) Then Call DwmExtendFrameIntoClientArea(m_hwnd, mg)
End Sub

Private Sub Form_Paint()
  Dim obj As Long
  Dim hOld As Long
  Dim lpRect As RECT
  
  obj = CreateSolidBrush(RGB(0, 0, 0))
  hOld = SelectObject(m_hdc, obj)
  GetClientRect m_hwnd, lpRect
  FillRect m_hdc, lpRect, obj
  SelectObject m_hdc, hOld
  DeleteObject obj
  '
  Call DrawGlassEffect("Hello VISTA", lpRect)
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If m_hTheme Then CloseThemeData (m_hTheme)
  If m_hFont Then DeleteObject (m_hFont)
End Sub

Private Sub DrawGlassEffect(ByVal szText As String, lpRect As RECT)
  Dim bm As Long
  Dim hOld As Long
  Dim handle As Long
  Dim dib As BITMAPINFO
  Dim dto As DTTOPTS
  '
  handle = CreateCompatibleDC(m_hdc)
  '
  With dib.bmiHeader
    .biSize = 40 ' sizeof(BITMAPINFOHEADER)
    .biWidth = 50 * m_ScaleX
    .biHeight = -m_lFontSize * m_ScaleY
    .biPlanes = 1
    .biBitCount = 32
    .biCompression = BI_RGB
  End With
  '
  With dto
    .dwSize = Len(dto)
    .dwFlags = DTT_GLOWSIZE Or DTT_COMPOSITED
    .iGlowSize = 10
  End With
  '
  bm = CreateDIBSection(m_hdc, dib, DIB_RGB_COLORS, 0, 0, 0)
  '
  hOld = SelectObject(handle, bm)
  Call SelectObject(handle, m_hFont)
  '
  Call DrawThemeTextEx(m_hTheme, handle, 0, 0, StrConv(szText, vbUnicode), -1, DT_SINGLELINE Or DT_CENTER Or DT_VCENTER Or DT_NOPREFIX, lpRect, dto)
  Call BitBlt(m_hdc, 0, 0, 50 * m_ScaleX, m_lFontSize * m_ScaleY, handle, 0, 0, SRCCOPY)
  '
  Call SelectObject(handle, hOld)
  DeleteObject bm
  DeleteDC handle
End Sub

A voir également

Ajouter un commentaire Commentaires
Messages postés
2107
Date d'inscription
mercredi 21 août 2002
Statut
Contributeur
Dernière intervention
19 février 2021
2
J'ajoute aussi que si sur une form vierge ça a son petit effet, une fois qu'on l'utilise avec divers contrôles (testez avec un bouton banal vous verrez), ça devient tout simplement inutilisable. Ou alors j'ai zappé quelque chose ...
Messages postés
1
Date d'inscription
samedi 3 février 2007
Statut
Membre
Dernière intervention
17 mai 2007

Messages postés
1
Date d'inscription
jeudi 29 mars 2007
Statut
Membre
Dernière intervention
19 avril 2007

ok
Messages postés
14008
Date d'inscription
samedi 29 décembre 2001
Statut
Modérateur
Dernière intervention
28 août 2015
74
Salut
Intéressant.
Oui, une capture du rendu serait la bienvenue
+ idée : un zip de ce code avec, en ajout, le test de la version de windows au démarrage de ton appli afin de signaler si le système est compatible (vista) ou pas.
Messages postés
2107
Date d'inscription
mercredi 21 août 2002
Statut
Contributeur
Dernière intervention
19 février 2021
2
Oui mais une capture serait quand même appréciée en plus du code ^^
Afficher les 14 commentaires

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.