Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic

Turn Monitors On and Off from a .NET Console Application

Rate me:
Please Sign up or sign in to vote.
3.83/5 (7 votes)
2 Oct 2006CPOL2 min read 58.9K   4   38   10
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

VB
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)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to break screensaver ? Pin
sanong2-Oct-06 7:43
sanong2-Oct-06 7:43 
AnswerRe: How to break screensaver ? Pin
DrJaymz2-Oct-06 9:08
DrJaymz2-Oct-06 9:08 
GeneralRe: How to break screensaver ? Pin
sanong2-Oct-06 22:38
sanong2-Oct-06 22:38 
GeneralShouldn't broadcast the message Pin
Leo Davidson2-Oct-06 4:54
Leo Davidson2-Oct-06 4: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 Pin
DrJaymz2-Oct-06 5:59
DrJaymz2-Oct-06 5:59 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz2-Oct-06 6:03
DrJaymz2-Oct-06 6:03 
GeneralRe: Shouldn't broadcast the message [modified] Pin
Victor Vogelpoel9-Oct-06 20:18
Victor Vogelpoel9-Oct-06 20:18 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz10-Oct-06 1:11
DrJaymz10-Oct-06 1:11 
GeneralRe: Shouldn't broadcast the message Pin
Victor Vogelpoel10-Oct-06 4:32
Victor Vogelpoel10-Oct-06 4:32 
GeneralRe: Shouldn't broadcast the message Pin
Leo Davidson26-Jul-08 21:44
Leo Davidson26-Jul-08 21:44 

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

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