VB6 - Een formulier transparant maken
![](http://img.brin-designs.com/img/games/480/vb6-making-form-transparent.png)
Ermee beginnen
Hier is een kort voorbeeld van hoe je een formulier transparant kunt maken in Visual Basic.Opmerking: er moet een kleurenpalet zijn om dit te laten werken.
In een module
Private Declare-functie GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate 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 Transparency (ByVal hWnd As Long, Optionally ByVal Col As As Long = vbBlack, _
Optioneel ByVal PcTransp als byte = 255, optioneel ByVal TrMode als Boolean = True) als Boolean
'Return: waar als er geen fout is.
'hWnd: hWnd van het venster om transparant te maken
'Col: Kleur om transparant te maken als TrMode = False
'PcTransp: 0 Ã 255 >> 0 = transparant -: - 255 = ondoorzichtig
Dim DisplayStyle As Long
On Error GoTo Exit
VoirStyle = GetWindowLong (hWnd, GWL_EXSTYLE)
Als DisplayStyle (DisplayStyle of WS_EX_LAYERED) dan
DisplayStyle = (DisplayStyle of WS_EX_LAYERED)
Call SetWindowLong (hWnd, GWL_EXSTYLE, DisplayStyle)
Stop als
Transparency = (SetLayeredWindowAttributes (hWnd, Col, PcTransp, IIf (TrMode, LWA_COLORKEY of LWA_Defaut, LWA_COLORKEY)) 0)
Uitgang:
If Not Err.Number = 0 Then Err.Clear
Eindfunctie
Public Sub ActiveTransparency (M As Form, d As Boolean, F As Boolean, _
T_Transparantie als geheel getal, optionele kleur zo lang)
Dim B As Boolean
Als d en F dan
'Maakt kleur (hier de achtergrondkleur van de vorm) transparant
'op waarde van T_Transparency
B = Transparantie (M.hWnd, Color, T_Transparency, False)
Anders Dan
'Maakt vorm, inclusief alle componenten, transparant
'op waarde van T_Transparency
B = Transparantie (M.hWnd, 0, T_Transparency, True)
Anders
'Herstelt de vorm ondoorzichtig.
B = Transparantie (M.hWnd,, 255, True)
Stop als
End Sub
Zoals gebruikt in een formulier
Private Sub Form_Load ()Dim i As Integer
'Vb: allemaal transparant in verhouding 140/255
'ActiveTransparency Me, True, False, 140, Me.BackColor
'Ex: Vorm transparant, zichtbaar component in verhouding 140/255
'ActiveTransparency Me, True, True, 140, Me.BackColor
'Voorbeeld geeft de verslechtering van de vormtransparantie weer
ActiveTransparency Me, True, False, 0
Me.Show
Voor i = 0 tot 255 Stap 3
ActiveTransparency Me, True, False, i
Me.Refresh
Volgende ik
End Sub