Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Turn off monitor in VB.NET & WinXP

Status
Not open for further replies.

CSpannos

IS-IT--Management
Mar 21, 2001
32
US
Hello

I need code that works in VB.NET and WINDOWS XP:

I'm trying to find the code that will put a monitor in a powered-off or suspended state, like power save mode. I'd like to actually turn off the monitor, and also have the option to put it in standby mode. I have the code that works in VB6 and Windows 9x. It does not work in NT/2000/XP and I cannot port it over to .NET:

----------------------------------------------------------
Code:
Option Explicit
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

Const WM_SYSCOMMAND = &H112
Const SC_MONITORPOWER = &HF170
   
Private Sub Command1_Click()
    ' Turn Monitor Off
    SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal -1&
End Sub

Private Sub Command2_Click()
    ' Turn monitor on
    SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 0&
End Sub
----------------------------------------------------------

Here's another example:

----------------------------------------------------------
Code:
Private Const SC_MONITORPOWER = &HF170&
Private Const SC_SCREENSAVE = &HF140&
Private Const WM_SYSCOMMAND = &H112
Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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

'Purpose     :  Switches the monitor on or off
'Inputs      :  bPowerOn            If True, turns the monitor on
'                                   else turns to monitor off
'               [lHwnd]             VB: The form handle OR
'               [sFormCaption]      VBA: The caption of the form calling the
'                                   routine, can be Me.Caption or Application.Caption
'Outputs     :
'Author      :  Andrew Baker
'Date        :  05/11/2000 13:07
'Notes       :
'Revisions   :

Sub MonitorPower(bPowerOn As Boolean, Optional lHwnd As Long, Optional sFormCaption As String)
    Dim lState As Long
    If lHwnd = 0 Then
        lHwnd = FindWindowA(vbNullString, sFormCaption)
    End If
    If bPowerOn Then
        lState = -1
    Else
        lState = 2
    End If
    Call SendMessage(lHwnd, WM_SYSCOMMAND, SC_MONITORPOWER, lState)
End Sub


'Purpose     :  Changes the monitor standby mode
'Inputs      :  bStandBy            If True, sets the monitor to Stand by
'                                   else turns the Stand by off
'               [lHwnd]             VB: The form handle OR
'               [sFormCaption]      VBA: The caption of the form calling the
'                                   routine, can be Me.Caption or Application.Caption
'Outputs     :
'Author      :  Andrew Baker
'Date        :  05/11/2000 13:07
'Notes       :
'Revisions   :

Sub MonitorStandby(bStandBy As Boolean, Optional lHwnd As Long, Optional sFormCaption As String)
    Dim lState As Long
    If lHwnd = 0 Then
        lHwnd = FindWindowA(vbNullString, sFormCaption)
    End If
    If bStandBy Then
        lState = 1
    Else
        lState = -1
    End If
    Call SendMessage(lHwnd, WM_SYSCOMMAND, SC_MONITORPOWER, lState)
End Sub
----------------------------------------------------------

Someone please help! I've been looking for an answer for months, and no one seems to know how to do this.

Thanks!
Kosta
 
this solution workes in XP, using VB6

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

Public Const MONITOR_ON = -1&
Public Const SC_MONITORPOWER = &HF170&
Public Const MONITOR_OFF = 2&
Public Const WM_SYSCOMMAND = &H112

Turn Monitor on:
SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON

Turn Monitor off:
SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF


Hope this helps

Matthew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top