Click here to Skip to main content
13,597,887 members
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 23 Jan 2006
Licenced CPOL

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

, 23 Jan 2006
Rate this:
Please Sign up or sign in to vote.
How to turn on/off the monitor


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

static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, 
              IntPtr wParam, IntPtr lParam);


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

    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.

    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.

    Form frm = new Form();
  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 !

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [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:

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:

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:


More specifically, turn the monitor ON, using:


Turn the monitor OFF, using:


Put the monitor to STAND BY, using:


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:

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

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.


  • 24th January, 2006: Initial post


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


About the Author

Gelu Vac
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralKeep Monitor On During Apps Lifetime Pin
Sukhjinder_K5-Sep-07 2:26
memberSukhjinder_K5-Sep-07 2:26 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 24 Jan 2006
Article Copyright 2006 by Gelu Vac
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid