Click here to Skip to main content
Click here to Skip to main content

Turn Monitors On and Off from a .NET Console Application

, 2 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Turn monitors on and off from a .NET Console Application.

Introduction

I needed to be able to turn the monitors on and off at will, based on a set time. I could have written a bunch of code that allowed me to set the schedule etc., but instead, I wrote this console app. The app runs on the command line, and accepts a couple of arguments so that its then a simple case of setting a schedule via the Windows scheduler. If you put the EXE in the Windows system32 folder, you can call it directly, so to turn the monitors off at night, you'd just call [monitors off], and to turn on in the morning, you'd call [monitors on]. Obviously, a test is helpful, since by definition you cannot see what is happening! [Monitors test].

To do this, you will need to call on the Windows API, and a function called SendMessage supports all sorts of weird and wonderful things, so we need to import this function call. The SendMessage function takes arguments that are simple constants, which can be found if you're lucky on the MSDN website.

Using the Code

Const HWND_BROADCAST As Integer = &HFFFF
Const SC_MONITORPOWER As Integer = &HF170
Const WM_SYSCOMMAND As Short = &H112S

Sub Main()
    Dim instr As String = Command()
    Select Case 
      Command().ToLower
      'Make sure it matches - and look at the commandline switch 
      Case "off"
            TurnOff()
      Case "on"
            TurnOn()
      Case "test"
            TurnOff()
            'turn off monitor 
            System.Threading.Thread.Sleep(10000)
            'wait 10 seconds
            TurnOn()
            'turn on monitor 
      Case Else
            MsgBox("Usage, [on]/[off]/[test] ", _
                   MsgBoxStyle.Information, _
                   "Need Command Switch")
    End Select
End Sub

_ Private Function SendMessage( ByVal Handle As Int32, _ 
          ByVal wMsg As Int32, ByVal wParam As Int32, _ 
          ByVal lParam As Int32) As Int32

End Function

Sub StandBy()
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 1)
End Sub

Sub TurnOff() 
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub

Sub TurnOn()
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1)
End Sub

Points of Interest

This code works with dual monitors, and you can use it via VNC - which is handy if you are running a kiosk or information display. This code works fine, but you might want to look at Raymond Chen's code (one of the Windows developers). He says that it's wrong to broadcast the monitor power messages. Instead, you should create your own window, post the message to it (only), then run a message pump, and finally exit. I would argue that they should write the documentation of these mystical functions a little better, but you might like to look here for more information: http://blogs.msdn.com/oldnewthing/archive/2006/06/13/629451.aspx. As far as I can gather, it's safe to use this broadcast for this message, but if you delve into the SendMessage() function, you need to be careful, as all top level windows will process the message which could lead to some interesting issues.

History

  • V1.00: Works fine, I cobbled it together from some code written in C, and I adapted it for .NET; presumably, you can import into C# easily.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DrJaymz
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionHow to break screensaver ? Pinmembersanong2-Oct-06 8:43 
AnswerRe: How to break screensaver ? PinmemberDrJaymz2-Oct-06 10:08 
GeneralRe: How to break screensaver ? Pinmembersanong2-Oct-06 23:38 
GeneralShouldn't broadcast the message PinmemberLeo Davidson2-Oct-06 5:54 
Raymond Chen (one of the Windows developers) says that it's wrong to broadcast the monitor power messages. Instead you should create your own window, post the message to it (only), then run a message pump and finally exit.
 
http://blogs.msdn.com/oldnewthing/archive/2006/06/13/629451.aspx
 
(I'm just as guilty of this mistake as you are. In fact, I still haven't gotten around to updating
my similar C++ program to do the right thing, since I've been too busy and it does still work, at least for now, on Windows XP, on my machine... Eventually I'll fix it, though, and I figured I'd pass on the info.)
 
I wrote my thing so I could turn off my monitors when I go to bed without having to wait for the screensaver to kick in. I added the ability to specify a timeout so I could turn the light off and still have 10 seconds of bright screen so I could see my way into bed. Works like a charm. Smile | :)
 
All the best,
Leo

GeneralRe: Shouldn't broadcast the message PinmemberDrJaymz2-Oct-06 6:59 
GeneralRe: Shouldn't broadcast the message PinmemberDrJaymz2-Oct-06 7:03 
GeneralRe: Shouldn't broadcast the message [modified] PinmemberVictor Vogelpoel9-Oct-06 21:18 
GeneralRe: Shouldn't broadcast the message PinmemberDrJaymz10-Oct-06 2:11 
GeneralRe: Shouldn't broadcast the message PinmemberVictor Vogelpoel10-Oct-06 5:32 
GeneralRe: Shouldn't broadcast the message PinmemberLeo Davidson26-Jul-08 22:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 2 Oct 2006
Article Copyright 2006 by DrJaymz
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid