This blog is subject the DISCLAIMER below.

Saturday, March 03, 2007

Turn off your monitor via code (C#)

In the name off Allah ,

How to turn off your monitor via code?
This is an important question at least to me , because I use my computer for long time daily and frequently turn on and off the screen as a way to save energy and power .
I hate to make that by using the monitor button(off/on) , because I hear sound from the monitor feel me that the screen may damage .
So I will inform you here , How to turn off your monitor via code ( C# ) ?

I will code that depending in API called SendMessage function it’s useful to handle monitor states - the display is going to low power, the display is being shut off and the display is turned on .

Syntax :
LRESULT SendMessage( HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam );


  • # HWnd (first paramter)
    Handle to the window whose window procedure will receive the message. If you don't want to bother creating a window to send the message to, you can send the message to all top level windows (HWND_BROADCAST) or you can use GetDesktopWindow function sending the message to the desktop window.
  • # Msg (second paramter)
    Specifies the message to be sent (WM_SYSCOMMAND).
  • # wParam(Third paramter)
    Specifies additional message-specific information (SC_MONITORPOWER).
  • # LParam (last paramter)
    * 1 - the display is going to low power.
    * 2 - the display is being shut off.
    * –1 - the display is being turned on (undocumented value).

Start the coding now :

First of all you should use
using System.Runtime.InteropServices; //to DllImport

public int WM_SYSCOMMAND = 0x0112;
public int SC_MONITORPOWER = 0xF170; //Using the system pre-defined MSDN constants that can be used by the SendMessage() function .

private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
//To call a DLL function from C#, you must provide this declaration .

private void button1_Click(object sender, System.EventArgs e)

SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );//DLL function
attached at this link the source code .


sh@dy said...

or you can use the power options :P

Ahmed Essawy said...

yes ,but In my case it's not a good solution :) .

Ramy Mahrous said...

Yes, very nice Essawy

But by Essawy's way, you can use it peaceally as

In load event handler :D :-
for( ;;)
SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );


SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,1 );

Make your application not appear in startbar and override Alt + Control + Delete

send to your friend :D

Ahmed Essawy said...

I will try that Ramy :D .

Rami said...

Nice idea. I would just want to point out that SendMessage is the basic Windows API for sending messages to other windows applications.
the receiver application must have a valid handle to a window even if it is invisible.
It is a Sync. method.
There's another similar API called PostMessage which is the same but Async.

kalpana said...

its a good solution,but the SendMessage function is valid for OS versions Windows CE 1.0 and above..what is the alternative is the OS version does not support the SendMessage function?

Ramy Mahrous said...

Please refer to specific Windows documentation to see if there is method like SendMessage or not, but I don't think there is alternative method for SendMessage. For all Windows versions there is SendMessage method.

Ashwath Akirekadu said...

Is there a way to turn off only one monitor? (I have two monitors connected to the same PC)

Ahmed Essawy said...

Really , I don't know if it is applicable or not ...
if you depend on some hardware to connect the two monitor ,I think it's better to refer to the hardware documentation .. you may get a way ...
the another way is to refer to Windows API documentation .

nikes-passion said...

link is not working..plz send it to


Ahmed Essawy said...

here you are a new link for the article's code :
click here

Anonymous said...


selmaguzel said...

Thanks but i get the error below whan i run under wince5:
Can't find PInvoke DLL 'user32.dll'.

Where can i find this dll?
Thanks again.

LinDol said...

Gooooooooood ;-)
Thank you very much

Lars Brandt said...


Anonymous said...

Thanks, works great!

csr said...

Great... But can U show how this could work in a windows service instead

vikas said...

I want to track Monitor off event if triggered by OS or any other external application. Can you help me?

Ravi Gupta said...

Hi All,
Good one,but I wanted to make one of my monitors sleep which are connected in serial(Daisy).
how to do this ??

Anonymous said...

I was using this successfully on Windows XP, but the same code does not work on Windows 7. Does anybody have any ideas?

Anonymous said...

Likewise, cannot get it to work in W7