tags:

views:

1109

answers:

6

Is it programmatically possible to turn a monitor on/off through code (C#)?

+1  A: 

Press the on/off button


If you want to do it in code, apparently this is possible in the Win32 API:

SendMessage hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, param

where WM_SYSCOMMAND = 0x112 and SC_MONITORPOWER = 0xF170 and param indicates the mode to put the monitor in: -1 : on 2 : off 1 : energy saving mode

hWnd can be a handle for any window - so if you have a Form, something like this should work

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

public static void Main(string[] args)
{
    Form f = new Form();
    bool turnOff;   //set true if you want to turn off, true if on
    SendMessage(f.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, turnOff ? 2 : -1);
}

Note I haven't actually tried this...

Lee
If the power is out, it may be too dark to see the on/off button, so you may need to have a flashlight handy for those conditions.
S.Lott
I sooo wanted to answer this, +1 :-)
Newtopian
Damn , where is my on off button. I only have one with a broken circle with a line in it. I want a refund.
Learning
if that works, then perfect answer!
Hugo
+13  A: 

Did you even try googling it?

First hit: http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx

I am not surprised you need to use some DLL's supplied by Windows.

(I guessed you needed a C# solution, because that's the only tag you applied).

Edit: I tested this in a quick app and all seems to work nicely. This should solve your problems.

Wim Haanstra
+10  A: 

Actually, it appears you can in C#: http://fci-h.blogspot.com/2007/03/turn-off-your-monitor-via-code-c.html. Haven't tested this, though.

Razzie
A: 

You could use one of these to control the monitor. They have .NET assemblies available to get up and running quickly.

Dave Van den Eynde
+2  A: 

Try something like this (joke ahead)

ITAPPMONROBOT

Barn
A: 

I can see this functionality finding its way into my custom inventory agent that i use at the company i work for.

It would be fun to put peoples monitors in sleep mode randomly while i watch from a distance listening to the "WTF!?!"

Jason Miesionczek