C++ 關閉顯示器

來源:互聯網
上載者:User

標籤:鍵盤   style   monit   des   pow   開啟   滑鼠   http   turn   

好睏,想躺一下,關燈、上床,筆記本的螢幕還亮著,好刺眼,睡不著!

腦子裡出現一個疑問,怎麼用C++寫一個關閉螢幕的小程式呢?

 

參考了網上已有的例子,最簡化:

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 0;9 }

執行後,會關閉顯示器的電源,其它的似乎不受影響(暫時沒有驗證)。如果要喚醒螢幕,動動滑鼠,或是敲敲鍵盤就可以了。

 

 

關於“WM_SYSCOMMAND message”的參考:https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms646360(v=vs.85).aspx

裡面有關於SC_MONITORPOWER的說明:

SC_MONITORPOWER
0xF170

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

The lParam parameter can have the following values:

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

 

 

 

 

 

 

SC_MONITORPOWER的作用是用於設定顯示器的狀態。這條指令支援那些帶有節能特性的裝置,比如,由電池供電的個人電腦。

參數lParam的值可以為下列值:

-1 開啟顯示器電源

 1 使顯示器進入低功耗模式

 2 關閉顯示器的電源

 

一個小插曲,我用我的筆記本調試可以,能關閉顯示器的電源。但是,我讓我的朋友用台式機測試,實現不了,顯示器關了,緊接著又重開。

 

關於函數SendMessage()和FindWindow(),暫時沒有看懂,不解釋。

C++ 關閉顯示器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.