Win2K Transparency

This is a demonstration of how to set up alpha transparency and hotkeys on the Windows desktop. It will only work if you have Windows 2000/XP because alpha transparency was not implemented in earlier versions, so it wont work.

Form1 code
Private Sub cmdOK_Click()
  Form1.Hide
End Sub

Private Sub cmdExit_Click()
  End
End Sub

Private Sub Form_Load()
  Me.Hide
  StartHook Me.hWnd

End Sub

Private Sub HScroll1_Change()
  Label3 = HScroll1.Value
  MakeTransparent fhWndHScroll1.Value

End Sub

Generated using PrettyCode.Encoder
Hotkey module
'Declare functions
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As LongByVal id As LongByVal fsModifiers As LongByVal vk As LongAs Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As LongByVal id As LongAs Long
Private Declare Function CallWindowProc Lib "user32" (ByVal lpPrevWndFunc As LongByVal hWnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As LongAs Long
Private Declare Function PostMessage Lib "user32" (ByVal hWnd As LongByVal wMsg As LongByVal wParam As LongByVal lParam As LongAs Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
'Define constants
Private Const WM_HOTKEY = &H312
Private Const WM_NCDESTROY = &H82
Private Const WM_MOUSEMOVE = &H200
Private Const GWL_WNDPROC = (-4)
'Dim variables
Dim fhWnd As Long
Private m_lpPrevWndProc As Long
Private m_hWndMain As Long

Public Sub StartHook(hWnd As Long)

  m_hWndMain = hWnd
  'Register Ctrl-T as a hotkey
  RegisterHotKey hWnd0vbCtrlMaskvbKeyT
  'Sub-class the window, so it can intercept hotkey presses
  m_lpPrevWndProc = SetWindowLong(hWndGWL_WNDPROCAddressOf WndProc)

End Sub

Public Sub StopHook()
  'Unregister the hotkey
  UnregisterHotKey m_hWndMain0
  'Stop sub-classing
  SetWindowLong m_hWndMainGWL_WNDPROCm_lpPrevWndProc

End Sub

'This is where the window messages are analysed
Public Function WndProc(ByVal hWnd As LongByVal uMsg As Long_
    ByVal wParam As LongByVal lParam As LongAs Long

  Select Case uMsg
    Case WM_NCDESTROY
      'If the window gets an NCDESTROY message (ie: it is being closed)
      'Stop sub-classing
      StopHook
    Case WM_HOTKEY
      'If the window hets a HOTKEY message (ie: the hotkey has been pressed)
      'Get the handle to the top window
      fhWnd = GetForegroundWindow
      'Load the form and give it focus
      Form1.HScroll1.Value = 255
      Form1.Show
      Form1.SetFocus
  End Select

  'Send the window messages on to be processed as normal
  WndProc = CallWindowProc(m_lpPrevWndProchWnduMsgwParamlParam)

End Function


Generated using PrettyCode.Encoder
Transparency Module
'Define the required constants
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const WS_EX_TRANSPARENT = &H20&
Public Const LWA_ALPHA = &H2&
'Declare the required functions
Private Declare Function GetWindowLong Lib "user32" (ByVal hWnd As LongByVal nIndex As LongAs Long
Private Declare Function SetWindowLong Lib "user32" (ByVal hWnd As LongByVal nIndex As LongByVal dwNewLong As LongAs Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As LongByVal crey As ByteByVal bAlpha As ByteByVal dwFlags As LongAs Long

'Pass this a valid window handle and an amount between 0(transparent) and 255(solid)
Public Sub MakeTransparent(hWnd As LongAmount As Integer)
  Dim NormalWindowStyle As Long
  'Get the 32bit Extended Style value
  NormalWindowStyle = GetWindowLong(hWndGWL_EXSTYLE)
  On Error GoTo Er
  'Make the window a layered window, by adding the LAYERED attribute to its extended style
  SetWindowLong hWndGWL_EXSTYLENormalWindowStyle Or WS_EX_LAYERED
  'Set the transparency level of the window
  SetLayeredWindowAttributes hWnd0AmountLWA_ALPHA
  Exit Sub

  Er:
  MsgBox "Your version of Windows does not support alpha transparency! You must be using Win 2000 or XP for this to work."
End Sub

Public Sub FadeOut()
  'Fades the window out in 256 steps
  For t = 255 To 0 Step -1
    MakeTransparent Form1.hWndt
    DoEvents
  Next t
End Sub
Public Sub FadeIn()
  'Fades the window in in 256 steps
  For t = 0 To 255
    MakeTransparent Form1.hWndt
    DoEvents
  Next t
End Sub


Generated using PrettyCode.Encoder