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


  • Elshan

    Thanks it works fine
    Elshan form Srilanka

  • Mano

    I used turn off monitor programmatic using C# code but i can’t get any result .no reaction appear

    • http://www.dotnetthoughts.net/ Anuraj P

      @Mano: Are you getting any error? You can get last error by setting SetLastError=true in the DllImport attribute and then call Marshal.GetLastWin32Error.

  • Sunny

    Thanks, works nice !!

  • Pingback: How to turn off your monitor when you lock your machine | dotnet thoughts