C + + Turn off display

Source: Internet
Author: User

Good sleepy, want to lie down, turn off the lights, go to bed, laptop screen is still on, good glare, can't sleep!

There is a question in the mind, how to write a small program to turn off the screen in C + +?

Refer to the examples available on the web for the most streamlined:

 1  #include <windows.h>2  3  int   main ()  4  {  5  /*   The display is being shut off  
   
    */ 
    6  SendMessage (FindWindow (
    0 , 
    0 ), Wm_syscommand, Sc_monitorpower, 
    2  
     7  8  return< /span> 0  ;  9 } 
   

After execution, the monitor's power is turned off, and the others do not seem to be affected (not verified at the moment). If you want to wake up the screen, move the mouse, or tap the keyboard to do it.

Reference to "Wm_syscommand message": https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms646360 (v=vs.85 ). aspx

There is a note about sc_monitorpower :

Sc_monitorpower
0xf170

Sets the state of the display. This command supports devices that has power-saving features, such as a battery-powered personal computer.

The lParam parameter can has the following values:

  • -1 (the display is powering on)
  • 1 (the display is going to low power)
  • 2 (the display is being shut off)

The sc_monitorpower function is used to set the status of the monitor. This directive supports devices with energy-saving features, such as a battery-powered PC.

The value of the parameter lparam can be the following values:

-1 Turn on the monitor power

1 bringing the monitor into low-power mode

2 Power off the monitor

A little episode, I use my laptop to debug can, can turn off the power of the monitor. However, I asked my friend to use the desktop test, do not realize, monitor off, and then re-open.

About functions SendMessage () and FindWindow () are not understood for the time being, not explained.

C + + Turn off display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.