How can I programmatically check in Windows 7 and XP if 'windows power management' has turned off the display? (If I can receive an event, that would be even better.)
-
1why do you want to know? So you can turn it back on? There's a way to say to Windows "when this app is running it doesn't get much keyboard/mouse action but that doesn't mean we're idle so don't blank the screen" (eg for a video player). If that's you, preventing the blanking is better than being notified of it, right? – Kate Gregory Jul 02 '11 at 15:59
-
similar: http://stackoverflow.com/questions/328490/monitoring-a-displays-state-in-python – Jul 06 '11 at 13:08
-
You could check out IMSVidDevice: msdn.microsoft.com/en-us/library/dd694519(VS.85).aspx – Jul 06 '11 at 13:13
-
Possibly a duplicate of [this](http://stackoverflow.com/questions/203355/is-there-any-way-to-detect-the-monitor-state-in-windows-on-or-off)? Bottom line is that it's not really possible. – Peter Jun 30 '11 at 06:55
-
that question seemed to be focused on humans turning the monitor off with a power switch, not Windows doing it because of idle time – Kate Gregory Jul 02 '11 at 16:01
2 Answers
I don't think it can be done for XP. In Windows 7 there are all kinds of goodies related to power management. The Windows API Code Pack is a set of managed wrappers that are simple to call from C# or VB and that map Windows paradigms (like event sinks, Windows messages and function pointers) into .NET ones (like delegates and events.) From the Power Management Demo that comes with the code pack, here is some code you might like:
using Microsoft.WindowsAPICodePack.ApplicationServices;
// . . .
PowerManager.IsMonitorOnChanged += new EventHandler(MonitorOnChanged);
// . . .
void MonitorOnChanged(object sender, EventArgs e)
{
settings.MonitorOn = PowerManager.IsMonitorOn;
AddEventMessage(string.Format("Monitor status changed (new status: {0})", PowerManager.IsMonitorOn ? "On" : "Off"));
}
Edit:
Links to Windows API Code Pack: Windows API Code Pack: Where is it?
If you want use it just like is mentioned in this post check this: https://stackoverflow.com/a/27709672/846232

- 1
- 1

- 18,808
- 8
- 56
- 85
-
-
Works like a charm, combined with the screensaver check from http://stackoverflow.com/a/9858981/1025177 (change "(isRunning)" to "(isRunning || !PowerManager.IsMonitorOn)") can both kinds of screensaving be covered :D – BloodyRain2k Jul 11 '16 at 17:12
Your application will get a WM_SYSCOMMAND message with SC_MONITORPOWER in wParam (make sure to and wParam with 0xfff0 first). It will send a similar message when the screen saver kicks in (SC_SCREENSAVE). If your goal is preventing the screen to turn off you can return 0 on these, although that doesn't work when the user has a password set.

- 46
- 1
-
It also doesn't work if your application's window isn't the active window. – Dunk May 29 '14 at 21:34