Monitor Power Off

This is a very simple application that will allow you to turn off your monitor(s) in Windows, rather than waiting for the set amount of time.

How to use: When it starts, it will be minimised to the Task Bar (the icons next to the time). Double clicking on that icon will set the monitors to power off. Single clicking on the icon will bring up the small form, where you can click on the Turn Off button to do the same. Pressing CTRL+ALT+SHIFT+D will also power down your monitor. Closing the form will exit the application.

Rather than posting the source code as a full project, below is the source code for MonitorPowerOff. You just need to add a Button to the main form, rename the form file to frmMonitorOff and paste this into the code behind the form.

Tested in Windows 10 x64.
I am currently using Visual Studio 2017 Community Edition.

Download MonitorPowerOff [FREE]

btn_donateCC_LGPlease support this app, and my time, by donating whatever $ you can spare.


using LAWC.Common;
using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace MonitorPowerOff
{
    public partial class frmMonitorOff : Form
    {

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


        
        public enum MonitorState
        {
            MonitorStateOn = -1,
            MonitorStateOff = 2,
            MonitorStateStandBy = 1
        }

        private KeyboardHook keyboardHook;


        public frmMonitorOff()
        {
            InitializeComponent();
            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;

            Keys key1 = Keys.Control;
            Keys key2 = Keys.Alt;
            Keys key3 = Keys.Shift;
            Keys key4 = Keys.D;

            // HOTKEY STUFF:
            // register the event that is fired after the key press.
            keyboardHook = new KeyboardHook();
            keyboardHook.RegisterHotKey(1, KeyboardHook.KeyToModifierKey(key1) | KeyboardHook.KeyToModifierKey(key2) | KeyboardHook.KeyToModifierKey(key3), key4);
            keyboardHook.KeyPressed += new EventHandler<LAWC.Common.KeyPressedEventArgs>(Hook_KeyPressed);

            this.notifyIcon1.MouseClick += NotifyIcon1_MouseClick;

            minimise();
        }

        private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            // show context menu
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                toggleForm();
            }
        }


        private void toggleForm()
        {
            // toggle
            if (this.WindowState == FormWindowState.Minimized)
            {
                restore();
            }
            else if (this.WindowState == FormWindowState.Normal)
            {
                minimise();
            }
        }

        private void restore()
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            notifyIcon1.Visible = true;
            this.Show();
            this.BringToFront();
            this.Focus();
        }

        private void minimise()
        {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            notifyIcon1.Visible = true;
            if (this.Visible == true) this.Hide();
        }


        void Hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }


        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }


        private void SetMonitorInState(MonitorState state)
        {
            SendMessage(0xFFFF, 0x112, 0xF170, (int)state);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }


    }
}