Question Monitor out of sleep

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
Is there an event when the monitor get out of sleep?

How do i detect it and just put out a message box when that happen?
 
The short answer: RegisterPowerSettingNotification function (Windows)
WM_SYSCOMMAND and SC_MONITORPOWER if you encountered them would have been a lot easier, but can't be used for this.

Code example:
    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_POWERBROADCAST AndAlso m.WParam.ToInt32 = PBT_POWERSETTINGCHANGE Then
            Dim ps = CType(Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(POWERBROADCAST_SETTING)), POWERBROADCAST_SETTING)
            Debug.WriteLine((Date.Now.ToLongTimeString & " DisplayState " & ps.Data.ToString))
        End If
        MyBase.WndProc(m)
    End Sub

    Public Declare Function RegisterPowerSettingNotification Lib "user32.dll" (hRecipient As IntPtr, ByRef PowerSettingGuid As Guid, Flags As Int32) As IntPtr
    Public Declare Function UnregisterPowerSettingNotification Lib "user32.dll" (handle As IntPtr) As IntPtr
    Public GUID_CONSOLE_DISPLAY_STATE As New Guid("6fe69556-704a-47a0-8f24-c28d936fda47")
    Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
    Public Const WM_POWERBROADCAST = &H218
    Public Const PBT_POWERSETTINGCHANGE = &H8013
    Public Structure POWERBROADCAST_SETTING
        Public PowerSetting As Guid
        Public DataLength As UInteger
        Public Data As DisplayState '(Byte)
    End Structure
    Public Enum DisplayState As Byte
        Off
        [On]
        Dimmed
    End Enum

    Private hConsoleDisplay As IntPtr

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If hConsoleDisplay <> IntPtr.Zero Then
            UnregisterPowerSettingNotification(hConsoleDisplay)
        End If
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        hConsoleDisplay = RegisterPowerSettingNotification(Me.Handle, GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE)
        If hConsoleDisplay <> IntPtr.Zero Then
            Debug.WriteLine("registered for display state notification")
        End If
    End Sub
 
That's where window messages (WM) are received.
 

Latest posts

Back
Top