Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Timers.au3>
- #include <MsgBoxConstants.au3>
- Const $idletime = 900000
- Global Const $WM_POWERBROADCAST = 0x0218
- ; GUID_LIDSWITCH_STATE_CHANGE GUID
- ; 0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3
- Global Const $GUID_LIDSWITCH_STATE_CHANGE = DllStructCreate("byte[16]")
- DllStructSetData($GUID_LIDSWITCH_STATE_CHANGE, 1, Binary("0x4D0F3EBA17B89440A2D1D56379E6A0F3"))
- ; create gui and register notification
- Global $GUI = GUICreate("")
- GUIRegisterMsg($WM_POWERBROADCAST, "__WM_POWER")
- Global $hPower = DllCall("user32.dll", "handle", "RegisterPowerSettingNotification", "handle", $GUI, "ptr", DllStructGetPtr($GUID_LIDSWITCH_STATE_CHANGE), "dword", 0)
- GUISetState()
- _GuiMinimizeToTray($GUI)
- HotKeySet ( "^!L", "_Exit" )
- Do
- ; Mouse/Keyboard action during this delay will change reported idle time
- Sleep(300000)
- If _Timer_GetIdleTime() > $idletime Then
- Beep();
- MsgBox($MB_TOPMOST, "Going sleep in 3 seconds ...", "Any activity to cancel", 3)
- If _Timer_GetIdleTime() > $idletime Then
- Run ( "schtasks.exe /run /tn Sleep")
- EndIf
- EndIf
- Until GUIGetMsg() = -3
- ; unregister notification
- DllCall("user32.dll", "bool", "UnregisterPowerSettingNotification", "handle", $hPower[0])
- Func __WM_POWER($hwnd, $msg, $wparam, $lparam)
- #forceref $hwnd
- Local Const $PBT_POWERSETTINGCHANGE = 0x8013
- ; Data member is variable depending on notification
- ; the size of the Data member is returned in DataLength
- Local Const $POWERBROADCAST_SETTING = "byte[16];dword DataLength;byte Data"
- Switch $msg
- Case $WM_POWERBROADCAST
- Switch $wparam
- Case $PBT_POWERSETTINGCHANGE
- Local $PBT_PSC = DllStructCreate($POWERBROADCAST_SETTING, $lparam)
- ; GUID of registered notification is the first member of the structure
- Switch DllStructGetData($PBT_PSC, 1)
- Case DllStructGetData($GUID_LIDSWITCH_STATE_CHANGE, 1)
- ; for this notification, Data is an int representing the lid state
- Local $data = DllStructCreate("int", DllStructGetPtr($PBT_PSC, "Data"))
- _LidStateChange(DllStructGetData($data, 1))
- ;
- Return 1
- EndSwitch
- EndSwitch
- EndSwitch
- Return "GUI_RUNDEFMSG"
- EndFunc
- Func _LidStateChange($iState)
- Static $iPrevious = -1
- If $iPrevious = -1 Then
- ; first fire, current state
- $iPrevious = $iState
- Return
- ElseIf $iPrevious = $iState Then
- Return
- Else
- $iPrevious = $iState
- EndIf
- ; lid state:
- ; 0 -> closed
- ; 1 -> open
- Switch $iState
- Case 0
- ;ConsoleWrite("lid closed" & @CRLF)
- ;DllCall("user32.dll", "bool", "LockWorkStation")
- run ( "schtasks.exe /run /tn Sleep")
- Case 1
- ;ConsoleWrite("lid opened" & @CRLF)
- EndSwitch
- EndFunc
- Func _GuiMinimizeToTray($h_wnd)
- ; check if 1 = Window exists, then check to see if its 16 = Window is minimized
- If BitAND(WinGetState($h_wnd), 1) Or Not BitAND(WinGetState($h_wnd), 16) Then
- ; change the window state to hide
- WinSetState($h_wnd, "", @SW_HIDE)
- EndIf
- EndFunc ;==>_GuiMinimizeToTray
- Func _Exit()
- Exit 0
- EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement