locked
Power ON/OFF Screen Programatically RRS feed

  • Question

  • I need to be able to power OFF and back ON the screen from my program, and have not been able to find an API to do so.

    Can anyone help?

         Ed

    Thursday, January 25, 2007 11:11 PM

Answers

  • You can do this by sending a WM_SYSCOMMAND message to the "top most" window in your system passing in a "wparam" value of SC_MONITORPOWER. If you use an lparam value of -1, the monitor will turn on, and if you use an lparam value of 2 the monitor will turn off.

    Here is a sample that does this:

    Module Module1
       
    Private Const HWND_TOPMOST As Integer = -1
       
    Private Const WM_SYSCOMMAND As UInt32 = &H112
       
    Private Const SC_MONITORPOWER As UInt32 = &HF170

       
    Enum MonitorState
           
    [On] = -1
           
    Off = 2
       
    end enum

        Private Declare Auto Sub SendMessage Lib "user32.dll" (ByVal hWnd As Integer, ByVal msg As UInt32, ByVal wParam As UInt32, ByVal lparam As Integer)

       
    Public Sub SetMonitorState(ByVal state As MonitorState)
           
    SendMessage(HWND_TOPMOST, WM_SYSCOMMAND, SC_MONITORPOWER, state)
       
    End Sub

        Sub Main()
           
    SetMonitorState(MonitorState.Off)
           
    System.Threading.Thread.Sleep(5000)
           
    SetMonitorState(MonitorState.On)
        
    End Sub

    End Module

    -Scott Wisniewski

    Friday, January 26, 2007 1:09 AM
    Moderator

All replies

  • You can do this by sending a WM_SYSCOMMAND message to the "top most" window in your system passing in a "wparam" value of SC_MONITORPOWER. If you use an lparam value of -1, the monitor will turn on, and if you use an lparam value of 2 the monitor will turn off.

    Here is a sample that does this:

    Module Module1
       
    Private Const HWND_TOPMOST As Integer = -1
       
    Private Const WM_SYSCOMMAND As UInt32 = &H112
       
    Private Const SC_MONITORPOWER As UInt32 = &HF170

       
    Enum MonitorState
           
    [On] = -1
           
    Off = 2
       
    end enum

        Private Declare Auto Sub SendMessage Lib "user32.dll" (ByVal hWnd As Integer, ByVal msg As UInt32, ByVal wParam As UInt32, ByVal lparam As Integer)

       
    Public Sub SetMonitorState(ByVal state As MonitorState)
           
    SendMessage(HWND_TOPMOST, WM_SYSCOMMAND, SC_MONITORPOWER, state)
       
    End Sub

        Sub Main()
           
    SetMonitorState(MonitorState.Off)
           
    System.Threading.Thread.Sleep(5000)
           
    SetMonitorState(MonitorState.On)
        
    End Sub

    End Module

    -Scott Wisniewski

    Friday, January 26, 2007 1:09 AM
    Moderator
  • This works well, but when I am in fullscreen mode in a DirectX application, the screen is turned back on by itself.

     

    Is this something Windows XP might be doing? Or the DirectX game engine that I am using?

     

    To solve it, right now I am sending the "turn off" message at every iteration of my render-loop, but it doesn't feel like a very elegant solution...

     

    Any suggestions? Thanks :-)

    Monday, April 23, 2007 5:11 PM