Results 1 to 7 of 7

Thread: [RESOLVED] trying to turn off monitor using sendmessage

  1. #1

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Resolved [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:
    1. Module Module1
    2.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    3.     (ByVal hwnd As Long, ByVal wMsg As Long, _
    4.      ByVal wParam As Long, ByVal lParam As Long) As Long
    5.     Const HWND_BROADCAST As Integer = &HFFFF
    6.     Const SC_MONITORPOWER As Integer = &HF170
    7.     Const WM_SYSCOMMAND As Short = &H112S
    8.  
    9.     Sub Main()
    10.         Dim instr As String = Command()
    11.         Select Case Command().ToLower
    12.             'Make sure it matches - and look at the commandline switch
    13.             Case "off"
    14.                 TurnOff()
    15.             Case "on"
    16.                 TurnOn()
    17.         End Select
    18.     End Sub
    19.  
    20.     Sub TurnOff()
    21.         MsgBox("turning off")
    22.  
    23.         SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2)
    24.     End Sub
    25.  
    26.     Sub TurnOn()
    27.         SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1)
    28.     End Sub
    29.  
    30.  
    31. 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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Const SC_MONITORPOWER As Integer = 61808
    2.     Const WM_SYSCOMMAND As Integer = 274
    3.     Const MONITOR_OFF As Integer = 2
    4.  
    5.     Partial Public Class API
    6.         <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SendMessage")> _
    7.         Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    8.         End Function
    9.     End Class
    10.  
    11.  
    12.     Private Sub Offbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Offbtn.Click
    13.         API.SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY)
    14.     End Sub

    Turns the monitor off as soon as Offbtn is clicked
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: trying to turn off monitor using sendmessage

    Quote Originally Posted by chris128
    I just had a quick go and got this code working in a winform app:

    vb Code:
    1. Const SC_MONITORPOWER As Integer = 61808
    2.     Const WM_SYSCOMMAND As Integer = 274
    3.     Const MONITOR_OFF As Integer = 2
    4.  
    5.     Partial Public Class API
    6.         <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SendMessage")> _
    7.         Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    8.         End Function
    9.     End Class
    10.  
    11.  
    12.     Private Sub Offbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Offbtn.Click
    13.         API.SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY)
    14.     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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    New Member
    Join Date
    Oct 2016
    Posts
    1

    Red face 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.
    Quote Originally Posted by chris128 View Post
    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
  •  



Click Here to Expand Forum to Full Width