Snippet for June 24, 20001

This time the code is for Visual Basic. It will only run under Windows 2000. This sample code shows how to use a function in the "Windows-API' to make your window or any other window semi transparent. Every line is documented as to what it does so enjoy. To use this code simply create a new project and place on it a slider bar and a label, give them the appropriate names and then run the application.

What you need

  1. Windows 2000
  2. Visual Basic
'---------------------------Paste this code into a module

Option Explicit

Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT = &H20&
Public Const LWA_ALPHA = &H2&

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

'---------------------------Paste this code into a form

Dim NormalWindowStyle As Long

Private Sub Form_Load()

'Get the normal window style to or layered property to
NormalWindowStyle = GetWindowLong(Me.hwnd, GWL_EXSTYLE)

'Make windows 200 recognize window as layered window
SetWindowLong frmMain.hwnd, GWL_EXSTYLE, NormalWindowStyle Or WS_EX_LAYERED

'Make windows 200 change transparency level
SetLayeredWindowAttributes frmMain.hwnd, 0, 255, LWA_ALPHA

End Sub

Private Sub Slider_Scroll()

Dim Transparency As Byte '0-255 255 being 100% visible


'Get new trnasparency level
Transparency = (255 * Slider.Value) / 100

'Set the new transparency level
SetLayeredWindowAttributes frmMain.hwnd, 0, Transparency, LWA_ALPHA

'Change the caption of the label
lblTransLevel.Caption = CStr(CInt((Transparency / 255) * 100)) & "%"

End Sub

Send questions or comments to webmaster@sentinel-software.net
This page was last updated June 22, 2001