How to turn off monitor programmatically using C#

Today while playing around send message WIN32 api calls, I found a nice option with WM_SENDCOMMAND, which helps to turn off monitor using C#. Later I found some my friend Shobanis using the same code in his dark project in the codeplex.

Here is the code.

using System.Runtime.InteropServices;

private const int HWND_BROADCAST = 0xFFFF;
private const int SC_MONITORPOWER = 0xF170;
private const int WM_SYSCOMMAND = 0x112;

private const int MONITOR_ON = -1;
private const int MONITOR_OFF = 2;
private const int MONITOR_STANBY = 1;

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

And you need to call the following line to turn off the monitor.

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

You can get more details about WM_SYSCOMMAND from MSDN

5 thoughts on “How to turn off monitor programmatically using C#

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>