Monitor Turn On/Off/StandBy in C# 5

You might want to change your display settings in a running program. The code behind requires the Import of a Dll & execution of a function SendMessage(,,,) by supplying 4 parameters. The value of the fourth parameter having values 0, 1 & 2 has the monitor in the states ON, STAND BY & OFF respectively. The first parameter has the value of the valid window which has received the handle to change the monitor state. This must be an active window. You can see the simple code below:


using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace Turn_Off_Monitor

{

    public partial class Form1 : Form

    {

        public Form1()  {  InitializeComponent();  }

        [DllImport("user32.dll")]

        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,

                                 IntPtr wParam, IntPtr lParam);

        private void btnStandBy_Click(object sender, EventArgs e)

        {

            IntPtr a = new IntPtr(1);

            IntPtr b = new IntPtr(0xF170);

            const int WM_SYSCOMMAND = 0x0112;

            SendMessage(this.Handle, WM_SYSCOMMAND, b, a);

        }

        private void btnMonitorOff_Click(object sender, EventArgs e)

        {

            IntPtr a = new IntPtr(2);

            IntPtr b = new IntPtr(0xF170);

            const int WM_SYSCOMMAND = 0x0112;

            SendMessage(this.Handle, WM_SYSCOMMAND, b,a);

        }

        private void btnMonitorOn_Click(object sender, EventArgs e)

        {

            IntPtr a = new IntPtr(-1);

            IntPtr b = new IntPtr(0xF170);

            const int WM_SYSCOMMAND = 0x0112;

            SendMessage(this.Handle, WM_SYSCOMMAND, b, a);

        }

       }

} 
Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes written here are 100%  tested & running.
Custom Search