PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2005 [RESOLVED] Turn monitor on/off?-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Turn monitor on/off?

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Resolved [RESOLVED] Turn monitor on/off?

    I built a power saver program that makes the screen turn black when you're not standing over the keyboard, but it only makes the screen turn black and that probably won't save much (or any) power! Is there a way to turn the screen off with code?

  2. #2
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Turn monitor on/off?

    There is a system message if your monitor supports suspend mode. WM_SYSCOMMAND and the SC_MONITORPOWER flag.

    http://msdn.microsoft.com/en-us/libr...60(VS.85).aspx
    Last edited by ZenDisaster; Oct 17th, 2009 at 09:34 AM.

  3. #3

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Turn monitor on/off?

    So what is WM_SYSCOMMAND? Is it an API function? I can't find any declaration information or anything.

  4. #4

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Turn monitor on/off?

    Ok, I googled about "turn screen off" and I found the SendMessage thing, and now I have this:
    vb.net Code:
    1. Public Class Form1
    2. #Region "API"
    3.     Private Const HWND_BRODCAST As Int32 = &HFFFF
    4.     Private Const SC_MONITORPOWER As Int32 = &HF170
    5.     Private Const WM_SYSCOMMAND As Int16 = &H122
    6.     Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Integer
    7. #End Region
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStandBy.Click
    10.         Debug.WriteLine(SendMessage(HWND_BRODCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2))
    11.         Debug.WriteLine("Sleeping...")
    12.         Threading.Thread.Sleep(3 * 1000)
    13.         Debug.WriteLine(SendMessage(HWND_BRODCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1))
    14.         Debug.WriteLine("Awakening...")
    15.     End Sub
    16. End Class
    and the calls return 1 both times. Nothing happens. What's wrong? I'm certain that my monitor is sleep-compatible.

  5. #5
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Turn monitor on/off?

    Although my monitor didn't shut off, I got a 0 responses with this;
    Code:
    #Region "API"
        Private Const HWND_BRODCAST As Integer = &HFFFF
        Private Const SC_MONITORPOWER As Integer = &HF170
        Private Const WM_SYSCOMMAND As Integer = &H122
        Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Debug.WriteLine(SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2))
            Debug.WriteLine("Sleeping...")
            Threading.Thread.Sleep(3 * 1000)
            Debug.WriteLine(SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1))
            Debug.WriteLine("Awakening...")
        End Sub
    Let me know if it shut down your display.

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

    Re: Turn monitor on/off?

    Have a look at this thread and see if it helps: http://www.vbforums.com/showthread.php?t=551140
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  7. #7

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Turn monitor on/off?

    @ZenDisaster: No, my display doesn't shut off whether I use 1 or 2 as a lParam.
    @chris128: Thanks, it works!

  8. #8
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: [RESOLVED] Turn monitor on/off?

    Right on, changing the constants worked great, thx Chris.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width