Thursday, April 28, 2011

Turn on/off monitor

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

From stackoverflow
  • 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...

    S.Lott : 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.
    Newtopian : I sooo wanted to answer this, +1 :-)
    Learning : Damn , where is my on off button. I only have one with a broken circle with a line in it. I want a refund.
    Hugo : if that works, then perfect answer!
  • 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.

  • 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.

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

  • Try something like this (joke ahead)

    ITAPPMONROBOT

  • 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!?!"

0 comments:

Post a Comment