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

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

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??

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; //

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

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();
Frm.Handle();

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 !

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:

constint SC_MONITORPOWER = 0xF170;
constint 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:

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.