Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C++
Article

Turn on/off monitor

Rate me:
Please Sign up or sign in to vote.
4.77/5 (43 votes)
24 Jul 2005 348.1K   14.9K   75   61
Sets the state of the display.

Introduction

The SendMessage function is useful to handle monitor states - the display is going to low power, the display is being shut off and the display is turned on.

Code explanation

While using SendMessage function, you have to set four parameters:

  • hWnd

    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

    Specifies the message to be sent (WM_SYSCOMMAND).

  • wParam

    Specifies additional message-specific information (SC_MONITORPOWER).

  • lParam
    • 1 - the display is going to low power.
    • 2 - the display is being shut off.
    • -1 - the display is being turned on (undocumented value).
// Turn off monitor
Sleep(500); // Eliminate user's interaction for 500 ms
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

// Turn on monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);

// Low power monitor
Sleep(500); // Eliminate user's interaction for 500 ms
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 1);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Slovakia Slovakia
Wink | ;-)

Comments and Discussions

 
QuestionWhy my PC is getting locked if this executes? Pin
Member 145182231-Jul-19 22:01
Member 145182231-Jul-19 22:01 
QuestionTroyan found! Pin
PiligrimJob24-Nov-17 0:23
PiligrimJob24-Nov-17 0:23 
SuggestionRe: Troyan found! Pin
CHill6024-Nov-17 0:28
mveCHill6024-Nov-17 0:28 
QuestionSending these commands to a 2nd monitor? Pin
Member 113804936-Jun-17 13:05
Member 113804936-Jun-17 13:05 
If I have 2 monitors, and I want to send the command to sleep/wake up to only my second monitor, how can I do this? I'ma bit of a noob. Laugh | :laugh:
QuestionAvoid turn on Pin
Sh.H.8-Jun-16 0:42
Sh.H.8-Jun-16 0:42 
QuestionThis does not work on windows 10! Pin
jerbio15-Mar-16 7:07
jerbio15-Mar-16 7:07 
Questionexplanation for .rc file and batch file Pin
Member 1235707228-Feb-16 4:02
Member 1235707228-Feb-16 4:02 
Questionhow to generate .exe file from code Pin
Member 1235707228-Feb-16 3:56
Member 1235707228-Feb-16 3:56 
Question[Virus(Worm)] Download demo project - 2.81 Kb Pin
Member 1193673525-Aug-15 23:27
Member 1193673525-Aug-15 23:27 
AnswerRe: [Virus(Worm)] Download demo project - 2.81 Kb Pin
Member 1193673525-Aug-15 23:56
Member 1193673525-Aug-15 23:56 
QuestionRequest_To_Elaborate_Code Pin
V@ibhAV Patel25-Dec-13 2:51
V@ibhAV Patel25-Dec-13 2:51 
Questionmonitor blinks Pin
SanjeevCharla6-Feb-13 15:42
SanjeevCharla6-Feb-13 15:42 
QuestionCan this work under remote desktop? Pin
george0880722-Dec-12 4:56
george0880722-Dec-12 4:56 
GeneralMy vote of 5 Pin
sunny_rajwadi23-May-12 0:11
sunny_rajwadi23-May-12 0:11 
QuestionReally Turn Off Monitor Pin
nano2k20-Oct-11 3:08
nano2k20-Oct-11 3:08 
GeneralThank you so much! Pin
Member 211670621-May-09 14:52
Member 211670621-May-09 14:52 
Questionhow to calculate the time between monitor off and on Pin
srinag4911-Sep-08 17:37
srinag4911-Sep-08 17:37 
GeneralMonitior control--in Windows xp Pin
nnuurraa29-Feb-08 19:56
nnuurraa29-Feb-08 19:56 
QuestionShall it stay countinously? Pin
thamizhinpan15-Oct-07 7:57
thamizhinpan15-Oct-07 7:57 
AnswerRe: Shall it stay countinously? Pin
Dalibor Drzik16-Oct-07 1:39
Dalibor Drzik16-Oct-07 1:39 
GeneralRe: Shall it stay countinously? Pin
kingmax_00718-Nov-07 20:09
kingmax_00718-Nov-07 20:09 
GeneralKeeping the Monitor Turned ON Pin
Sukhjinder_K6-Sep-07 6:40
Sukhjinder_K6-Sep-07 6:40 
AnswerRe: Keeping the Monitor Turned ON Pin
Dalibor Drzik6-Sep-07 11:22
Dalibor Drzik6-Sep-07 11:22 
GeneralRe: Keeping the Monitor Turned ON Pin
Sukhjinder_K7-Sep-07 0:33
Sukhjinder_K7-Sep-07 0:33 
Generalkeeping the monitor turned off Pin
auldthief13-Jun-07 22:36
auldthief13-Jun-07 22:36 

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.