Turn off your monitor via code (C#)
In the name off Allah , Start the coding now :
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:
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.
Specifies the message to be sent (WM_SYSCOMMAND).
Specifies additional message-specific information (SC_MONITORPOWER).
* 1 - the display is going to low power.
* 2 - the display is being shut off.
* –1 - the display is being turned on (undocumented value).
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
19 comments:
or you can use the power options :P
yes ,but In my case it's not a good solution :) .
Yes, very nice Essawy
@Shady
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 );
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 :D
I will try that Ramy :D .
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.
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?
@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.
Is there a way to turn off only one monitor? (I have two monitors connected to the same PC)
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 .
link is not working..plz send it to nikes_passion@Yahoo.co.in
thanks..
@nikes-passion
here you are a new link for the article's code :
click here
thanks..!!
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.
Gooooooooood ;-)
Thank you very much
Thanks, works great!
Great... But can U show how this could work in a windows service instead
I want to track Monitor off event if triggered by OS or any other external application. Can you help me?
I was using this successfully on Windows XP, but the same code does not work on Windows 7. Does anybody have any ideas?
Likewise, cannot get it to work in W7
Post a Comment