Attribute VB_Name = "modOpacity"
Option Explicit

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal wNewWord As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const GWL_WNDPROC  As Long = (-4)
Public Const WM_ACTIVATE      As Long = &H6
Public Const WM_ACTIVATEAPP   As Long = &H1C
Public Const WA_INACTIVE      As Long = 0
Public Const WA_ACTIVE        As Long = 1
Public Const WA_CLICKACTIVE   As Long = 2

Public defWindowProc As Long

Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   On Error Resume Next
   Select Case hwnd
      Case frmBot.hwnd
         Select Case uMsg
            Case WM_ACTIVATEAPP
               Select Case LoWord(wParam)
               Case WA_INACTIVE:
                    If Options.Opacity2 > 0 Then ChangeOpacity frmBot, CByte(Options.Opacity2)
               Case WA_ACTIVE:
                    If Options.Opacity1 > 0 Then ChangeOpacity frmBot, CByte(Options.Opacity1)
               End Select
            Case Else
                'Call Default Window Handler
                WindowProc = CallWindowProc(defWindowProc, hwnd, uMsg, wParam, lParam)
                Exit Function
         End Select
      Case Else
        'Call Default Window Handler for other Handle
         WindowProc = CallWindowProc(defWindowProc, hwnd, uMsg, wParam, lParam)
   End Select
End Function

Public Sub UnSubClass(hwnd As Long)
  'Restore the default message handling before exiting
   If defWindowProc Then
      SetWindowLong hwnd, GWL_WNDPROC, defWindowProc
      defWindowProc = 0
   End If
End Sub

Public Sub SubClass(hwnd As Long)
  'Assign our own window message procedure (WindowProc)
   On Error Resume Next
   defWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Function LoWord(dw As Long) As Integer
   If dw And &H8000& Then
      LoWord = &H8000& Or (dw And &H7FFF&)
   Else
      LoWord = dw And &HFFFF&
   End If
End Function
