VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CPowerEvent"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private Declare Function GetSystemPowerStatus Lib "kernel32" (SystemPowerStatus As SYSTEM_POWER_STATUS) As Long

Public Enum ApmQuerySuspend
    BROADCAST_QUERY_ALLOW = 1
    BROADCAST_QUERY_DENY = &H424D5144
End Enum
Public Enum PowerEventType
    PBT_APMQUERYSUSPEND = &H0
    PBT_APMQUERYSTANDBY = &H1
    PBT_APMQUERYSUSPENDFAILED = &H2
    PBT_APMQUERYSTANDBYFAILED = &H3
    PBT_APMSUSPEND = &H4
    PBT_APMSTANDBY = &H5
    PBT_APMRESUMECRITICAL = &H6
    PBT_APMRESUMESUSPEND = &H7
    PBT_APMRESUMESTANDBY = &H8
    PBT_APMBATTERYLOW = &H9
    PBT_APMPOWERSTATUSCHANGE = &HA
    PBT_APMOEMEVENT = &HB
    PBT_APMRESUMEAUTOMATIC = &H12
End Enum
Private Type SYSTEM_POWER_STATUS
    ACLineStatus As Byte
    BatteryFlag As Byte
    BatteryLifePercent As Byte
    Reserved1 As Byte
    BatteryLifeTime As Long
    BatteryFullLifeTime As Long
End Type

Private m_lpOldWindowProcAddress As Long
Private m_hHookWnd As Long
Private m_udtPowerStatus As SYSTEM_POWER_STATUS
Public Event PowerEvent(ByVal plt As PowerEventType, ByVal lDetailData As Long, ByRef lReturn As Long)

Private Sub Class_Initialize()
    m_hHookWnd = 0
    GetSystemPowerStatus m_udtPowerStatus
End Sub
Private Sub Class_Terminate()
    UnHook
End Sub
Public Sub Hook(frmHook As Form)
    If m_hHookWnd = 0 Then
        With frmHook
            m_hHookWnd = .hWnd
            SetProp m_hHookWnd, CStr(m_hHookWnd), ObjPtr(Me)
            m_lpOldWindowProcAddress = SetWindowLong(m_hHookWnd, _
                                                     GWL_WNDPROC, _
                                                     AddressOf NewWindowProc)
        End With
    End If
End Sub
Public Sub UnHook()
    If m_hHookWnd Then
        SetWindowLong m_hHookWnd, GWL_WNDPROC, m_lpOldWindowProcAddress
        RemoveProp m_hHookWnd, CStr(m_hHookWnd)
        m_lpOldWindowProcAddress = 0
        m_hHookWnd = 0
    End If
End Sub
Friend Function CallBackWindowProc(hWnd As Long, uMsg As Long, wParam As Long, lParam As Long) As Long
    Const WM_POWERBROADCAST = &H218
    Select Case uMsg
           Case WM_POWERBROADCAST
                If wParam = PBT_APMPOWERSTATUSCHANGE Then
                    GetSystemPowerStatus m_udtPowerStatus
                End If
                RaiseEvent PowerEvent(wParam, lParam, CallBackWindowProc)
           Case Else
                CallBackWindowProc = CallWindowProc(m_lpOldWindowProcAddress, hWnd, uMsg, wParam, ByVal lParam)
    End Select
End Function
Public Property Get ACLineStatus() As Byte
    ACLineStatus = m_udtPowerStatus.ACLineStatus
End Property
Public Property Get BatteryFlag() As Byte
    BatteryFlag = m_udtPowerStatus.BatteryFlag
End Property
Public Property Get BatteryLifePercent() As Byte
    BatteryLifePercent = m_udtPowerStatus.BatteryLifePercent
End Property
Public Property Get BatteryLifeTime() As Long
    BatteryLifeTime = m_udtPowerStatus.BatteryLifeTime
End Property
Public Property Get BatteryFullLifeTime() As Long
    BatteryFullLifeTime = m_udtPowerStatus.BatteryFullLifeTime
End Property
