CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    62

    Monitor Power Off, Import DLL?

    I have 2 pieces of freeware (one dedicated, one a suite of utilities) that will turn my PC's monitor off. I like to be able to hit my monitor off with a double click on a desktop icon.

    Both these pieces of freeware don't do the job properly (sometimes), so I thought I'd look in to writing such a utility myself and do it properly.

    So I know it's possible to do, probably through importing a DLL, but I can't seem to find out which one.

    Can someone point me in the right direction please?

    Thanks.

  2. #2
    Join Date
    Mar 2007
    Posts
    90

    Re: Monitor Power Off, Import DLL?

    The right direction will be this thread.

    And in C# it will look like this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using HWND = System.IntPtr;
    using BOOL = System.Int32;
    using UINT = System.UInt32;
    using WPARAM = System.IntPtr;
    using LPARAM = System.IntPtr;
    
    namespace ShutdownMonitor
    {
        class Program
        {
            private enum SC_MONITORPOWEROptions : int
            {
                /// <summary>
                /// the display is powering on
                /// </summary>
                PowerOn = -1,
                /// <summary>
                /// the display is going to low power
                /// </summary>
                LowPower = 1,
                /// <summary>
                /// the display is being shut off
                /// </summary>
                ShutDown = 2
            }
    
            public static UINT WM_SYSCOMMAND = 0x112;
            public static WPARAM SC_MONITORPOWER = (System.IntPtr)0xF170;
    
            static void Main(string[] args)
            {
                
                HWND desktopWindow = GetDesktopWindow();
                BOOL result = PostMessage(desktopWindow, WM_SYSCOMMAND, SC_MONITORPOWER, (WPARAM)SC_MONITORPOWEROptions.LowPower);
                Console.WriteLine(result != 0);
            }
    
            [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
            private static extern BOOL PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
            [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", SetLastError = true)]
            private static extern HWND GetDesktopWindow();
        }
    }
    If you are using Windows Forms than you can get the desktopWindow using the Form's Handle property.

  3. #3
    Join Date
    Sep 2009
    Posts
    62

    Re: Monitor Power Off, Import DLL?

    What a comphrensive reply.

    Thanks someuser. Cheers.

  4. #4
    Join Date
    Sep 2009
    Posts
    62

    Re: Monitor Power Off, Import DLL?

    Thanks again someuser77.

    I just got around to writing the software today, I want an icon on the notifications bar that I can use to turn the monitor off, and it's working a treat so far.

    Cheers.

    PS. If anyone finds this at a later date with a search and wants to know how to use a form's handle to get the desktop window then simply replace the first line below with the second. (Form applications only!!).

    HWND desktopWindow = GetDesktopWindow();

    HWND desktopWindow = this.Handle;

    HTH.

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

Featured