Click here to Skip to main content
15,884,995 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 349K   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

 
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 
Generalplz help!!! Pin
misha2dope3-Apr-07 21:51
misha2dope3-Apr-07 21:51 
AnswerRe: plz help!!! Pin
Dalibor Drzik4-Apr-07 7:50
Dalibor Drzik4-Apr-07 7:50 
QuestionDifference between standby and off? Pin
oeriksen11-Oct-06 12:01
oeriksen11-Oct-06 12:01 
AnswerRe: Difference between standby and off? Pin
Dalibor Drzik4-Apr-07 7:37
Dalibor Drzik4-Apr-07 7:37 
I have to say I don't ever know... Sigh | :sigh:
Questioncan't keep blank when mediaplayer playing next song Pin
Xiao Chong6-Jul-06 7:18
Xiao Chong6-Jul-06 7:18 
AnswerRe: can't keep blank when mediaplayer playing next song Pin
Dalibor Drzik4-Apr-07 7:38
Dalibor Drzik4-Apr-07 7:38 
GeneralControl your monitor by software Pin
Robsori13-Jun-06 21:25
Robsori13-Jun-06 21:25 
GeneralRe: Control your monitor by software Pin
Siva_the_coolest2-Jun-08 18:12
Siva_the_coolest2-Jun-08 18:12 
GeneralRe: Control your monitor by software Pin
Siva_the_coolest2-Jun-08 22:08
Siva_the_coolest2-Jun-08 22:08 
GeneralFYI Pin
Michael Dunn13-Jun-06 8:07
sitebuilderMichael Dunn13-Jun-06 8:07 
AnswerRe: FYI Pin
Dalibor Drzik4-Apr-07 7:29
Dalibor Drzik4-Apr-07 7:29 
QuestionAnd how to turn off harddisk? Pin
andreasm8211-Jun-06 1:26
andreasm8211-Jun-06 1:26 
QuestionDetect monitor changes? Pin
Judge423-Apr-06 8:43
Judge423-Apr-06 8:43 
AnswerRe: Detect monitor changes? Pin
Dalibor Drzik24-Apr-06 3:11
Dalibor Drzik24-Apr-06 3:11 
GeneralGood Job! Pin
John R. Shaw16-Mar-06 16:46
John R. Shaw16-Mar-06 16:46 
AnswerRe: Good Job! Pin
Dalibor Drzik17-Mar-06 2:08
Dalibor Drzik17-Mar-06 2:08 
QuestionHow to turn off monitor Pin
uyencpham16-Dec-05 10:56
uyencpham16-Dec-05 10:56 
AnswerRe: How to turn off monitor Pin
Dalibor Drzik18-Dec-05 4:05
Dalibor Drzik18-Dec-05 4:05 
GeneralI tried using C# but it wont work , where i am missing Pin
Lalit N Dubey16-Aug-05 1:38
Lalit N Dubey16-Aug-05 1:38 
GeneralRe: I tried using C# but it wont work , where i am missing Pin
OMalleyW16-Aug-05 3:12
OMalleyW16-Aug-05 3:12 
GeneralRe: I tried using C# but it wont work , where i am missing Pin
Lalit N Dubey16-Aug-05 18:48
Lalit N Dubey16-Aug-05 18:48 

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.