Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

How To Turn Off Your Monitor When You Lock Your Machine

Rate me:
Please Sign up or sign in to vote.
4.82/5 (5 votes)
9 Oct 2012MIT1 min read 12.1K   7   3
How to turn off your monitor when you lock your machine

Usually we forget to turn off the monitor, when we lock our computer. It is a good thing, to save energy. You can configure your operating system to turn off the monitor after sometime. But sometimes, you may change/ disable this setting and forget to re-enable it. Here is a code snippet, which will turn off your monitor, while you lock your system. To get the System Lock event, you need to subscribe SessionSwitch event in SystemEvents class. SystemEvents class provides access to system event notifications. And the sessionswitch event occurs when the currently logged-in user has changed. You can find more details about SystemEvents class in MSDN. To turn off monitor combination off, SendMessage and HWND_BROADCAST API calls are used. Here is a post about How to turn off monitor programmatically using C#.

And here is the code snippet.

C#
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;

public partial class Form1 : Form
{
    private const int HWND_BROADCAST = 0xFFFF;
    private const int SC_MONITORPOWER = 0xF170;
    private const int WM_SYSCOMMAND = 0x112;

    private const int MONITOR_ON = -1;
    private const int MONITOR_OFF = 2;
    private const int MONITOR_STANBY = 1;

    [DllImport("user32.dll")]
    private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

    public Form1()
    {
        InitializeComponent();
        //Subscribe for SessionSwith event
        SystemEvents.SessionSwitch += new SessionSwitchEventHandler((sender, e) =>
        {
            switch (e.Reason)
            {
                //If Reason is Lock, Turn off the monitor.
                case SessionSwitchReason.SessionLock:
                    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
                    break;
            }
        });
    }
}

This can be used in a Windows Forms or WPF application.

Happy programming. :)

Update: Find this application on codeplex: http://powersave.codeplex.com/

Related Content

  1. How to turn off monitor programmatically using C#
  2. How to make a form stay always on top
  3. How to delete a file or folder to Recyclebin
  4. Setting an application on top other windows using C#
  5. WebCam in your applications using C#

Share This

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ryan Scott White31-Dec-20 11:25
professionalRyan Scott White31-Dec-20 11:25 
Thanks. This worked great for me.
GeneralMy vote of 4 Pin
Jason Christian15-Oct-12 6:02
Jason Christian15-Oct-12 6:02 
GeneralRe: My vote of 4 Pin
Anuraj Parameswaran15-Oct-12 7:38
Anuraj Parameswaran15-Oct-12 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.