Jump to content

Recommended Posts

Posted (edited)

Hello!

Here is a short example to get the notification when the monitors are going to sleep.

Have fun!

#NoTrayIcon

Global Const $WM_POWERBROADCAST = 0x0218
Global Const $PBT_POWERSETTINGCHANGE = 0x8013

Global Const $WM_SYSCOMMAND = 0x0112
Global Const $SC_MONITORPOWER = 0xF170
Global Const $MONITOR_ON = -1
Global Const $MONITOR_OFF = 2
Global Const $MONITOR_STANDBY = 1

Global Const $tagGUID = "struct; ulong Data1;ushort Data2;ushort Data3;byte Data4[8]; endstruct"
Global Const $tagPOWERBROADCAST_SETTING = $tagGUID & ";DWORD DataLength;DWORD Data;"

Global Const $DEVICE_NOTIFY_WINDOW_HANDLE = 0
Global Const $DEVICE_NOTIFY_SERVICE_HANDLE = 1

Global Const $GUID_CONSOLE_DISPLAY_STATE = "{6fe69556-704a-47a0-8f24-c28d936fda47}" ; Win8
Global Const $GUID_MONITOR_POWER_ON = "{02731015-4510-4526-99e6-e5a17ebd1aea}" ; Vista and Win7

Global $hGui = GUICreate("Gui for registering Windows Message", 600, 400)
GUIRegisterMsg($WM_POWERBROADCAST, "_PowerSettingNotification")
Global $nBtnSleep = GUICtrlCreateButton("Hey monitors, please sleep!", 50, 50, 500, 100)
GUICtrlSetFont(-1, 25)
Global $nEdit = GUICtrlCreateEdit("Informations:", 50, 180, 500, 200, 0x200840) ;$WS_VSCROLL | $ES_AUTOVSCROLL | $ES_READONLY
GUICtrlSetFont(-1, 14)


Global $hNotify

If @OSBuild >= 5000 And @OSBuild < 9000 Then
    $hNotify = _RegisterPowerSettingNotification($hGui, $GUID_MONITOR_POWER_ON)
ElseIf @OSBuild >= 9000 Then
    $hNotify = _RegisterPowerSettingNotification($hGui, $GUID_CONSOLE_DISPLAY_STATE)
Else
    MsgBox(64, "Sorry", "PowerSettingNotification is only supported by Windows Vista and above!")
    Exit
EndIf
If @error Then
    MsgBox(16, "Sorry", "PowerSettingNotification failed with error: " & @error & " - " & @extended & " !")
    Exit
EndIf

GUISetState()

Global $iMsg
Do
    $iMsg = GUIGetMsg()
    If $iMsg = $nBtnSleep Then DllCall('user32.dll', 'int', 'SendMessage', 'hwnd', $hGui, 'int', $WM_SYSCOMMAND, 'int', $SC_MONITORPOWER, 'int', $MONITOR_OFF)
Until $iMsg = -3

_UnregisterPowerSettingNotification($hNotify)



Func _PowerSettingNotification($hWndGUI, $MsgID, $wParam, $lParam)
    Local $tSetting, $iSetting
    Local $sDateNow, $sTimeNow, $sMsg

    If $wParam = $PBT_POWERSETTINGCHANGE Then
        $tSetting = DllStructCreate($tagPOWERBROADCAST_SETTING, $lParam)
        $iSetting = DllStructGetData($tSetting, "Data")

        $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
        $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC

        Switch $iSetting
            Case 2 ;The display is dimmed --> Win8 and above
                $sMsg = $sDateNow & " " & $sTimeNow & " : " & "The display is dimmed now"
                ConsoleWrite("> " & $sMsg & @CRLF)
                GUICtrlSetData($nEdit, GUICtrlRead($nEdit) & @CRLF & $sMsg)
                ;do some stuff here
            Case 1 ;The monitor in on
                $sMsg = $sDateNow & " " & $sTimeNow & " : " & "The display is on now"
                ConsoleWrite("> " & $sMsg & @CRLF)
                GUICtrlSetData($nEdit, GUICtrlRead($nEdit) & @CRLF & $sMsg)
                ;do some stuff here
            Case 0 ;The monitor in off
                $sMsg = $sDateNow & " " & $sTimeNow & " : " & "The display is off now"
                ConsoleWrite("> " & $sMsg & @CRLF)
                GUICtrlSetData($nEdit, GUICtrlRead($nEdit) & @CRLF & $sMsg)
                ;do some stuff here
        EndSwitch
    EndIf
EndFunc   ;==>_PowerSettingNotification


Func _RegisterPowerSettingNotification($hGui, $GUID)
    Local $aRet
    Local $tGuid = DllStructCreate($tagGUID)
    $aRet = DllCall('ole32.dll', 'long', 'CLSIDFromString', 'wstr', $GUID, 'struct*', $tGuid)
    If @error Or $aRet[0] <> 0 Then Return SetError(1, @error, 0)
    $aRet = DllCall("user32.dll", "handle", "RegisterPowerSettingNotification", "handle", $hGui, "struct*", $tGuid, "DWORD", $DEVICE_NOTIFY_WINDOW_HANDLE)
    If @error Or $aRet[0] = 0 Then Return SetError(2, @error, 0)
    Return $aRet[0]
EndFunc   ;==>_RegisterPowerSettingNotification

Func _UnregisterPowerSettingNotification($hNotify)
    Local $aRet = DllCall("user32.dll", "BOOL", "UnregisterPowerSettingNotification", "handle", $hNotify)
    If @error Or $aRet[0] = 0 Then Return SetError(1, @error, 0)
    Return $aRet[0]
EndFunc   ;==>_UnregisterPowerSettingNotification
Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Posted

FYI:

_RegisterPowerSettingNotification <-> _WinAPI_RegisterPowerSettingNotification /

_UnregisterPowerSettingNotification <-> _WinAPI_UnregisterPowerSettingNotification

are already included in WinAPISys.au3.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

OMG! I already did a google search, but did not find it, sorry. Just see it as an another example how to use it.

BTW: I still use V3.3.8.1

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

  • 1 year later...
Posted

Hello, funkey! Can you give a more simple example please? I need to only register event then monitor is powerOFF or ON(one of this), but without buttons and info in GUI. I`m very new to AutoIt, and its hard to me found only needed strings in your script.

BTW! I don`t see any information about _WinAPI_RegisterPowerSettingNotification even on offsite help-page... So.. your post is much more informative about this ))))

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...