Visual Basic Screen & System Metrics
SendMessage: Change the Display Power State
Posted:   Sunday March 28, 2004
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Rubén Vigón, VBnet - Randy Birch

Another straightforward API call, this time to turn monitors off (or on) when those devices support and utilize power-saving features, such as newer energy-saving monitors and laptops.

When the demo's button is pressed the monitor(s) attached to the system will go to a black screen. The monitor's "no input" timeout message may also be displayed. 

Mouse or keyboard activity while display is suspended should reactivate the display. In case it doesn't, the demo utilizes a timer to turn the monitors back on after the preset time has elapsed (5 seconds in this demo).

This call affects all monitors - in a multiple-monitor setup you cannot specify the on/off state of individual monitors.

 BAS Module Code

 Form Code
Add the following code to a form containing a command button (Command1) and a timer (Timer1):

Option Explicit
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const MONITOR_ON = -1&
Private Const MONITOR_LOWPOWER = 1&
Private Const MONITOR_OFF = 2&
Private Const SC_MONITORPOWER = &HF170&
Private Const WM_SYSCOMMAND = &H112

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hWnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Sub Form_Load()

   Command1.Caption = "Turn off monitors"
End Sub

Private Sub Command1_Click()

  'shut monitor(s) off, and enable
  'a timer that will turn them on again
  'after 5 seconds (just in case!)
   With Timer1
      .Interval = 5000
      .Enabled = True
   End With
End Sub

Private Sub Timer1_Timer()

   Timer1.Enabled = False
  'turn monitor on

End Sub


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter