Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C#
Article

Complete Guide on How To Turn A Monitor On/Off/Standby

Rate me:
Please Sign up or sign in to vote.
4.12/5 (18 votes)
23 Jan 2006CPOL4 min read 217.2K   47   31
How to turn on/off the monitor

Introduction

It's no more news that handling the monitor can be done using SendMessage().

My goal here is to show a complete manual to handle this function for the above mentioned task, as I have tested different situations from different authors and once again I have discovered the lack of proper documentation for an actually simple task.

API Declaration

One first issue is declaring the SendMessage API function in the desired programming language – the present one being Visual .NET C#.

C#
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, 
              IntPtr wParam, IntPtr lParam);

Where:

  • hwnd is the Handle to the window whose window procedure will receive the message. It should be a valid Windows handler.
  • Msg specifies the message to be sent.
  • wParam specifies additional message-specific information.
  • lParam specifies monitor state.

Handling the Handle Argument

Considering the above, here are your options: The best way is to use the active handler from the current Window Form, if you are developing a Windows-based application.
So get it using the public Handle property: this.Handle

But what happens when you are developing a *.dll Library or a Console Application, where you don’t have any currently used Windows Form????? Where you can't use the keyword this?? :)
It’s quite simple.

Go back to the root of the problem, and remember that you need a valid HANDLER to accomplish the task. What options does one have??

  1. Use the system pre-defined MSDN constants that can be used by the SendMessage() function:

    C#
    int HWND_BROADCAST = 0xffff;
    //the message is sent to all 
    //top-level windows in the system
    
    int HWND_TOPMOST    = -1;
    //the message is sent to one 
    //top-level window in the system
    
    int HWND_TOP        = 0;        //
    int HWND_BOTTOM     = 1;        //limited use
    int HWND_NOTOPMOST  = -2;       //
  2. Use the current Desktop handler that you can get also using APIs.

    C#
    [DllImport("user32.dll")]
    private static extern IntPtr GetDesktopWindow();

    [Although this does not return a valid value on every OS – Windows XP is one exception I have tested - maybe due to security permissions.]

  3. Why not instantiate a general-basic Form?? So what if I am not developing a Windows-based application?? Is it a crime to add the System.Windows.Forms reference?? It’s certainly not best-practice to mix things up, but hey! It's just like going offroad with your town-car.

    C#
    Form frm = new Form();
    Frm.Handle();
  4. Simply try to find an active window on your system and use its handle to accomplish what you first wanted. How do I do that? I'm almost falling in love with APIs! – thanks pHritz for this one !

    C#
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    //Or 
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, 
           IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    These functions will basically search for the top-level form whose class name is lpClassName and has the lpWindowName title. The good news is that if I will use null on the call, it will simply retrieve the top-most window.

Accessing the Monitor

So, we need to access the monitor.

Considering the above, we will need to provide the SendMessage Interop service, specific values so that the message reaches the desired target. For this, we will use the following predefined values and name them meaningfully for the job:

C#
const int SC_MONITORPOWER = 0xF170;
const int WM_SYSCOMMAND = 0x0112;

Monitor State Switch

So far, we have discussed about how to create a valid call of the function, and how to tell the Interop engine the target we aim at.

To complete the task, we still need to specify the verb symbolizing the action to be taken on the target.

As you might guess, we will need a switch option: turn the monitor on/off. Good guess, with the correction that we actually have three states: on / off / stand-by:

C#
const int MONITOR_ON = -1;
const int MONITOR_OFF = 2;
const int MONITOR_STANBY = 1;

Final Call Structure

The final generic call will look like this:

C#
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STATE);

More specifically, turn the monitor ON, using:

C#
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);

Turn the monitor OFF, using:

C#
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

Put the monitor to STAND BY, using:

C#
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY);

Similar API Functions

It shouldn't surprise you that turning the monitor on/off can be done using several other API function calls specially when you know that sending an interop message is up to certain engine needs.

Here are the rest of the functions (with API declarations) that can have a similar effect:

C#
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, 
              IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern bool PostThreadMessage(uint idThread, uint Msg, 
              UIntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SendMessageCallback(IntPtr hWnd, uint Msg, 
              UIntPtr wParam, IntPtr lParam, 
              SendMessageDelegate lpCallBack, UIntPtr dwData);

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, 
              UIntPtr wParam, IntPtr lParam);

So, knowing this now, you should be able to choose between SendMessage and all these other ones, and use them according to your own needs, considering the following observations:

  • SendMessage() – Sends the specified message to a window or windows, calls the window procedure for the specified window and does not return until the window procedure has processed the message.
  • PostMessage() – Posts a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.
  • PostThreadMessage() – Posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
  • SendNotifyMessage() – Sends the specified message to a window or windows. If the window was created by the calling thread, SendNotifyMessage calls the window procedure for the window and does not return until the window procedure has processed the message. If the window was created by a different thread, SendNotifyMessage passes the message to the window procedure and returns immediately; it does not wait for the window procedure to finish processing the message.
  • SendMessageCallback() – Calls the window procedure for the specified window and returns immediately. After the window procedure processes the message, the system calls the specified callback function, passing the result of the message processing and an application-defined value to the callback function.

History

  • 24th January, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
alexbk6618-Feb-19 2:04
alexbk6618-Feb-19 2:04 
QuestionComplete console application here Pin
Member 1201870225-Oct-15 0:15
Member 1201870225-Oct-15 0:15 
QuestionThe code listed only partially works - Real Solution Here Pin
Member 1165780611-May-15 10:09
Member 1165780611-May-15 10:09 
QuestionProblem monitor does not stay off after return from deepsleep Pin
jho196527-Nov-14 4:48
jho196527-Nov-14 4:48 
QuestionIt seems this doesn't work under remote desktop? Pin
george0880722-Dec-12 4:53
george0880722-Dec-12 4:53 
GeneralGetting a vaild handle for a console application Pin
Diamonddrake28-May-11 23:09
Diamonddrake28-May-11 23:09 
QuestionCompatibility in Windows 7 Pin
jananij9-Feb-10 18:01
jananij9-Feb-10 18:01 
AnswerRe: Compatibility in Windows 7 Pin
Michael Kugler13-Apr-10 2:35
Michael Kugler13-Apr-10 2:35 
AnswerRe: Compatibility in Windows 7 Pin
Ian Sawlor21-Jan-11 8:12
Ian Sawlor21-Jan-11 8:12 
QuestionRe: Compatibility in Windows 7 Pin
Member 39828032-Aug-11 14:16
Member 39828032-Aug-11 14:16 
AnswerFix Pin
Patricker20-Oct-11 9:21
Patricker20-Oct-11 9:21 
GeneralSome questions and one proble Pin
p2rn415-Dec-09 23:37
p2rn415-Dec-09 23:37 
GeneralPower on PinPopular
Piccadilly Yum Yum23-Dec-08 5:30
Piccadilly Yum Yum23-Dec-08 5:30 
GeneralProblem while executing Off/On the monitor in Embedded XP Environment Pin
sanjibmail13-Mar-08 17:17
sanjibmail13-Mar-08 17:17 
GeneralRe: Problem while executing Off/On the monitor in Embedded XP Environment Pin
bart.burkhardt7-Mar-10 21:02
bart.burkhardt7-Mar-10 21:02 
GeneralKeep Monitor On During Apps Lifetime Pin
Sukhjinder_K5-Sep-07 2:26
Sukhjinder_K5-Sep-07 2:26 
QuestionPredefined Values Pin
evol200014-Aug-07 8:10
evol200014-Aug-07 8:10 
GeneralGet the Monitor State Pin
SGerstacker19-Jul-07 3:57
SGerstacker19-Jul-07 3:57 
GeneralRe: Get the Monitor State Pin
Gelu Vac19-Jul-07 20:46
Gelu Vac19-Jul-07 20:46 
GeneralRe: Get the Monitor State Pin
Gelu Vac19-Jul-07 20:47
Gelu Vac19-Jul-07 20:47 
QuestionRe: Get the Monitor State Pin
SGerstacker20-Jul-07 3:53
SGerstacker20-Jul-07 3:53 
GeneralDDC/CI Pin
jjmartinez3-Jan-07 7:58
jjmartinez3-Jan-07 7:58 
GeneralAnd what about finding out the current status... Pin
argrithmag19-Jun-06 10:33
argrithmag19-Jun-06 10:33 
GeneralControl your monitor by software Pin
Robsori13-Jun-06 4:54
Robsori13-Jun-06 4:54 
GeneralRe: Control your monitor by software Pin
Gelu Vac19-Jun-06 22:57
Gelu Vac19-Jun-06 22:57 

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.