|
-
Dec 21st, 2008, 10:53 AM
#1
[RESOLVED] trying to turn off monitor using sendmessage
this code should work, but i am not 100% sure i hav the sendmessage declared right. Can someone help me debug this?
It's a console program and if you pass it the parameter "off" it is supposed to turn the monitor off. It doesn't.
vb Code:
Module Module1
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Const HWND_BROADCAST As Integer = &HFFFF
Const SC_MONITORPOWER As Integer = &HF170
Const WM_SYSCOMMAND As Short = &H112S
Sub Main()
Dim instr As String = Command()
Select Case Command().ToLower
'Make sure it matches - and look at the commandline switch
Case "off"
TurnOff()
Case "on"
TurnOn()
End Select
End Sub
Sub TurnOff()
MsgBox("turning off")
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub
Sub TurnOn()
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1)
End Sub
End Module
If someone has a better solution for what i need it for, i am all ears. I am writing a custom program that launches with the remote power button on my media center remote. It is to turn the monitor off and on.
Advanced planned functionality includes muting the sound at the same time.
Last edited by Lord Orwell; Dec 21st, 2008 at 10:56 AM.
-
Dec 21st, 2008, 12:20 PM
#2
Re: trying to turn off monitor using sendmessage
If your sure its just the SendMessage API that is the problem then you should try posting this in the API section instead..
-
Dec 21st, 2008, 12:47 PM
#3
Re: trying to turn off monitor using sendmessage
I believe this should be it.
Code:
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
For VB6.0 it was Long but Long in VB6.0 is Integer in .Net.
-
Dec 21st, 2008, 12:55 PM
#4
Re: trying to turn off monitor using sendmessage
I just had a quick go and got this code working in a winform app:
vb Code:
Const SC_MONITORPOWER As Integer = 61808
Const WM_SYSCOMMAND As Integer = 274
Const MONITOR_OFF As Integer = 2
Partial Public Class API
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SendMessage")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
End Class
Private Sub Offbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Offbtn.Click
API.SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY)
End Sub
Turns the monitor off as soon as Offbtn is clicked
-
Dec 21st, 2008, 02:12 PM
#5
Re: trying to turn off monitor using sendmessage
 Originally Posted by chris128
I just had a quick go and got this code working in a winform app:
vb Code:
Const SC_MONITORPOWER As Integer = 61808
Const WM_SYSCOMMAND As Integer = 274
Const MONITOR_OFF As Integer = 2
Partial Public Class API
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SendMessage")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
End Class
Private Sub Offbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Offbtn.Click
API.SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY)
End Sub
Turns the monitor off as soon as Offbtn is clicked 
you didn't declare the correct constant, but for whatever reason this isn't working on my system. It is working... My screen goes off, but it immediately turns on again. I was however able to get it to work by changing the handle to the broadcast one. This is against convention but it actually functions, whereas the "make a window and send the message to it" doesn't. Rep++ for you.
Here's my finished code:
Code:
Module Module1
Const SC_MONITORPOWER As Integer = 61808
Const WM_SYSCOMMAND As Integer = 274
Const MONITOR_OFF As Integer = 2
Partial Public Class API
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SendMessage")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
End Class
Sub main()
API.SendMessage(&HFFFF, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF)
End Sub
End Module
i was going to make it a toggle thing, but simply pressing any other remote button already wakes the monitor up, and that's good enough for me.
-
Dec 21st, 2008, 02:20 PM
#6
Re: trying to turn off monitor using sendmessage
Your current code should work if you will implement it the suggested modification I specified in post #3. I tried the following and it worked.
Code:
Public Class Form1
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
Const HWND_BROADCAST As Integer = &HFFFF
Const SC_MONITORPOWER As Integer = &HF170
Const WM_SYSCOMMAND As Short = &H112S
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub
End Class
-
Oct 2nd, 2016, 05:00 PM
#7
New Member
Re: trying to turn off monitor using sendmessage
Hi, if you did get this working, do you have an app or exe file. It would be great if you could share with me. Thanks.
 Originally Posted by chris128
I just had a quick go and got this code working in a winform app:
Turns the monitor off as soon as Offbtn is clicked 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|