PDA

View Full Version : C#/WPF - Monitor Switcher



kirupa
February 6th, 2007, 03:15 AM
There are many situations where I want to turn my monitor off without putting my system into standby. For example, at night, I keep my system running to record shows on my Media Center, so I would want my monitors to turn off immediately. Manually turning them on/off by pushing their buttons isn't a very efficient solution.

So, instead, I created an application that turns the monitor off at the push of a virtual button and turns them back on when the mouse moves. Here is how my application looks:

http://www.kirupa.com/net/images/monitorswitcher.png

You can install and run the application (XP/Vista) by clicking here: http://www.kirupafx.com/MonitorSwitcher/MonitorSwitcher.application (http://www.kirupafx.com/MonitorSwitcher/MonitorSwitcher.application)

Because accessing the monitor requires lower-level Win32 calls, I used some resources on http://www.pinvoke.net to create a SendMessage call to access the monitor's On/Off state. The code is:

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Runtime.InteropServices;
using System.Windows.Input;
namespace MonitorSwitcher
{
public partial class Window1
{
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
const int SC_MONITORPOWER = 0xF170;
const int WM_SYSCOMMAND = 0x0112;
const int MONITOR_ON = -1;
const int MONITOR_OFF = 2;
const int MONITOR_STANBY = 1;
int onFlag = 0;
public Window1()
{
this.InitializeComponent();
}
private void MonitorOff(object sender, RoutedEventArgs e)
{
SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
onFlag = 1;
}
private void MonitorOn(object sender, MouseEventArgs e)
{
if (onFlag == 1)
{
SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
onFlag = 0;
}
}
}
}
I have attached the full source file for this below :)

Cheers!
Kirupa :bandit:

dreeft
February 6th, 2007, 03:43 AM
Super impressive. Nice idea :)

Esherido
March 27th, 2007, 06:31 AM
Sweet I like it. :thumb:

hybrid101
March 27th, 2007, 07:00 AM
would this work with a dual monitor setup, turning off only one of the monitor?

kirupa
March 27th, 2007, 01:16 PM
Currently this turns off all monitors, but I guess in a future version, I can create something that allows you to specify which monitor to turn off.

:sc:

ChaseVoid
March 28th, 2008, 03:17 AM
Well, I have a problem. Whenever I try executing this it shows error. What I mean is, If I run this aplication once it's all dandy, it switches off the monitor and when moving mouse it turns it back on. However, if I try to use it again, the running application windows goes all black and then shows the 'Not Responding' message. Then, when I try to close it, the dialog box appears to ask what to do (aka. the check for solution and all that stuffs)

-----------------
More Information about the error :
Description:
A problem caused this program to stop interacting with Windows.
Problem signature:
Problem Event Name: AppHangXProcB1
Application Name: MonitorSwitcher.exe
Application Version: 0.0.0.0
Application Timestamp: 47ec9780
Hang Signature: ff7d
Hang Type: 288
Waiting on Application Name: bcont_nm.exe
Waiting on Application Version: 7.0.499.0
OS Version: 6.0.6001.2.1.0.256.1
Locale ID: 1033
Additional Hang Signature 1: b3dbe7e2e9ddf5456b5f6a35b78b013d
Additional Hang Signature 2: a38e
Additional Hang Signature 3: 17b60673b375ca7658a24d17fb2125dc
Additional Hang Signature 4: ff7d
Additional Hang Signature 5: b3dbe7e2e9ddf5456b5f6a35b78b013d
Additional Hang Signature 6: a38e
Additional Hang Signature 7: 17b60673b375ca7658a24d17fb2125dc
Read our privacy statement:
http://go.microsoft.com/fwlink/?linkid=50163&clcid=0x0409
-----------------

So, sadly I have to switch back to my old Windows Form Application, which works perfectly. I even tried to make this application from the scratch and even tried your given installer, but the results were the same.

I want to use a WPF version cause you can make it look pretty that way xD

branded
April 6th, 2008, 03:17 AM
Thank you Sir. It unfortunately took me two hours to find your code, I was having trouble working with the CodeProject article (http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx), especially since tonight is the first time I've ever written c# or used API calls (for real). :)

Appreciate it.

dirtylarry
May 15th, 2011, 11:08 AM
Currently this turns off all monitors, but I guess in a future version, I can create something that allows you to specify which monitor to turn off.

:sc:

can you update your code so that this is possible? if not, can you post something on the kirupa.com website as a tutorial?

thanks

Razarulz
May 24th, 2011, 05:30 AM
very nice idea ...i like it..