Tipp 0405 Transparente Fenster (ab Windows 2000)
Autor/Einsender:
Datum:
  Sven Koitka
05.08.2004
Entwicklungsumgebung:   VB 6
Ab Windows 2000 ist es möglich, mit Hilfe der API-Funktion SetLayeredWindowAttributes den Grad der Transparenz für ein Fenster frei festzulegen. In folgendem Beispiel wird das Fenster beim Laden und Entladen ein- bzw. ausgeblendet, und auch gezeigt, wie man einen festen Alpha-Wert für die Transparenz eines Fensters festlegen kann.
Hinweis
Durch die Verwendung der API-Funktion SetLayeredWindowAttributes werden auch alle auf der Form platzierten Steuerelemente transparent, da die API-Funktion von Microsoft eigentlich zum Ein- und Ausblenden von Fenstern gedacht ist.
Code im Codebereich des Moduls
 
Option Explicit

Private Declare Function GetWindowLong Lib "user32.dll" Alias _
    "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _
    As Long) As Long

Private Declare Function SetWindowLong Lib "user32.dll" Alias _
    "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

Private Declare Function SetLayeredWindowAttributes Lib _
    "user32.dll" (ByVal hWnd As Long, ByVal crKey As Long, _
    ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000

Private Const LWA_ALPHA = &H2

Public Sub Form_SetStyle(ByVal hWnd As Long)
  Dim nStyle As Long

  nStyle = GetWindowLong(hWnd, GWL_EXSTYLE)

  nStyle = nStyle Or WS_EX_LAYERED
  SetWindowLong hWnd, GWL_EXSTYLE, nStyle
End Sub

Public Sub Form_SetAlphaValue(ByVal hWnd As Long, _
       ByVal bAlpha As Byte)
  SetLayeredWindowAttributes hWnd, 0, bAlpha, LWA_ALPHA
End Sub

Public Sub Form_FadeIn(ByVal hWnd As Long, _
       Optional ByVal iStep As Integer = 1)
  Dim iAlpha As Integer

  For iAlpha = 0 To 255 Step iStep
    SetLayeredWindowAttributes hWnd, 0, iAlpha, LWA_ALPHA
    DoEvents
  Next
End Sub

Public Sub Form_FadeOut(ByVal hWnd As Long, _
       Optional ByVal iStep As Integer = 1)
  Dim iAlpha As Integer

  For iAlpha = 255 To 0 Step -iStep
    SetLayeredWindowAttributes hWnd, 0, iAlpha, LWA_ALPHA
    DoEvents
  Next
End Sub
 
Code im Codebereich der Form
 
Option Explicit

Private Sub Form_Load()
  Form_SetStyle Me.hWnd
  Me.Visible = True
  Form_FadeIn Me.hWnd, 1
End Sub

Private Sub cmdSetAlphaValue_Click()
  Dim varAlpha As Variant

  varAlpha = Val(Text1.Text)
  Select Case varAlpha
    Case Is < 100: varAlpha = 100
    Case Is > 255: varAlpha = 255
    Case Else
  End Select

  Form_SetAlphaValue Me.hWnd, varAlpha
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Form_FadeOut Me.hWnd
  End
End Sub
 
Weitere Links zum Thema
Transparente Fenster

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (3,7 kB) Downloads bisher: [ 2065 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Dienstag, 9. August 2011