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 );
Parameters:
- # 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 .
[DllImport(“user32.dll“)]
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 .
http://www.4shared.com/file/11593987/2d23f157/Turn_Your_Monitor_BY_Essawy.html
or you can use the power options 😛
Comment by sh@dy | March 3, 2007 |
yes ,but In my case it’s not a good solution 🙂 .
Comment by Ahmed Essawy | March 3, 2007 |
Yes, very nice Essawy
@Shady
But by Essawy’s way, you can use it peaceally as
In load event handler 😀 :-
for( ;;)
{
SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );
System.Threading.Thread.Sleep(100);
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 😀
Comment by Ramy Mahrous | March 3, 2007 |
I will try that Ramy 😀 .
Comment by Ahmed Essawy | March 6, 2007 |
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.
Comment by Rami | March 29, 2007 |
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?
Comment by kalpana | August 2, 2007 |
@kalpana
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.
Comment by Ramy Mahrous | August 3, 2007 |
Is there a way to turn off only one monitor? (I have two monitors connected to the same PC)
Comment by Ashwath Akirekadu | March 14, 2008 |
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 .
Comment by Ahmed Essawy | March 14, 2008 |
link is not working..plz send it to nikes_passion@Yahoo.co.in
thanks..
Comment by nikes-passion | March 27, 2008 |
@nikes-passion
here you are a new link for the article’s code :
click here
Comment by Ahmed Essawy | March 28, 2008 |
thanks..!!
Comment by Anonymous | April 2, 2008 |
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.
Comment by selmaguzel | June 17, 2008 |
welcome to the wow power leveling cheap wow power leveling service site, buy cheap wow gold,wotlk gold,world of warcraft power leveling buy wow gold
Comment by wow power leveling | February 1, 2009 |
Gooooooooood 😉
Thank you very much
Comment by LinDol | February 5, 2009 |
nice post!
Comment by WoW Gold Guides | May 7, 2009 |