Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on/off monitor

Tags:

c#

winapi

Is it programmatically possible to turn a monitor on/off through code (C#)?

like image 275
RV. Avatar asked Apr 03 '09 11:04

RV.


People also ask

What button turn the monitor on and off?

All computer monitors have a power button located somewhere near the front of the monitor. The power button is often indicated by a power icon, like the icon pictured to the right.

How do I turn my monitor screen off?

Hit the Ctrl + Alt + B key at once to turn your monitor off instantly.

Can a monitor turn on or off with a PC?

In short, no, you can't. Not on a desktop. The power of the monitor is independent of any kind of communication from the CPU.


2 Answers

Did you even try googling it?

First hit: http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx

I am not surprised you need to use some DLL's supplied by Windows.

(I guessed you needed a C# solution, because that's the only tag you applied).

EDIT February 8th 2013:

It was mentioned that the solution no longer worked under Windows 7 en 8. Well here is one that works nicely under Windows 7, haven't tried Windows 8 yet.

http://cocoa.ninja/posts/Turn-off-your-monitor-in-Csharp.html

namespace MonitorOff {      public enum MonitorState {         MonitorStateOn = -1,         MonitorStateOff = 2,         MonitorStateStandBy = 1     }      public partial class Form1 : Form {         [DllImport("user32.dll")]         private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);          public Form1() {             InitializeComponent();             SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;         }          void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) {             SetMonitorInState(MonitorState.MonitorStateOff);         }          private void button1_Click(object sender, EventArgs e) {             SetMonitorInState(MonitorState.MonitorStateOff);         }          private void SetMonitorInState(MonitorState state) {             SendMessage(0xFFFF, 0x112, 0xF170, (int)state);         }     } } 
like image 70
Wim Haanstra Avatar answered Oct 09 '22 00:10

Wim Haanstra


Press the on/off button


If you want to do it in code, apparently this is possible in the Win32 API:

SendMessage hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, param

where WM_SYSCOMMAND = 0x112 and SC_MONITORPOWER = 0xF170 and param indicates the mode to put the monitor in: -1 : on 2 : off 1 : energy saving mode

hWnd can be a handle for any window - so if you have a Form, something like this should work

int WM_SYSCOMMAND = 0x112; int SC_MONITORPOWER = 0xF170;  [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);  public static void Main(string[] args) {     Form f = new Form();     bool turnOff = true;   //set true if you want to turn off, false if on     SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ? 2 : -1)); } 

Note I haven't actually tried this...

like image 22
Lee Avatar answered Oct 09 '22 00:10

Lee