It's known how to turn the monitor screen on/off in C#. But how do I get it's current state?
Asked
Active
Viewed 3,052 times
1
-
What do you mean by "current state"? Also: have you tried anything? There are lots examples out there if you used your favourite search engine.... – DerApe Jul 23 '15 at 06:30
-
I mean: if the display ON or OFF. And no, I didn't find anything useful. The posts I found gives broken links and I only find applications that gives display's informations like name and resolutions, but never it's state. – tkpb Jul 23 '15 at 06:33
-
It would be really hard to find out whether a monitor is on or off since there isn't any standard that all manufacturers follow. I also can't see how you would turn it on/off, you should show your research that proves this as it at least shows you did some research – Sayse Jul 23 '15 at 06:36
-
@tkpb I just did a look on SO and found already [some](http://stackoverflow.com/questions/16668075/check-if-the-monitor-is-connected) [posts](http://stackoverflow.com/questions/5020559/screen-allscreen-is-not-giving-the-correct-monitor-count) with my first query which points you at least in some direction – DerApe Jul 23 '15 at 06:41
-
Well, Windows controls the display, then it might know if it's ON or OFF. I only know how to **send** commands to turn ON/OFF, but I'd like to check it's current state. – tkpb Jul 23 '15 at 06:41
-
why dont you start with ON state as you ll be doing it on your system only, take the STATE as On and try, also put code, I would like to know how to do that. – DeshDeep Singh Jul 23 '15 at 06:46
-
@derape yeah, I found posts like yours too. Not really what I'm asking. I'm not talking about any kind of connections. Otherwise, it would be just a matter of monitoring the connected devices. – tkpb Jul 23 '15 at 06:50
2 Answers
1
Simply put, there is no way of telling the monitor state. At least not reliably.
You can try to use GetDevicePowerState, but officialy it's not working and some people report it does. On one of my computers it really is, on all others - it doesn't.
Also, listening to system power messages is unreliable, as user may switch it off by him/herself and you will not detect it.
You can use this code to turn it on or off, but that's pretty much it. I also left GetDevicePowerState references, maybe it will work in your case, but probably won't(I get error 6, which is invalid handle error code from WinAPI)
class Program
{
const int SYSCOM = 0x0112;
const int MONPOW = 0xF170;
const int MON_ON = -1;
const int MON_OFF = 2;
const int MON_STANBY = 1;
const int MONITOR_DEFAULTTOPRIMARY = 1;
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
[DllImport("kernel32.dll", SetLastError =true)]
static extern bool GetDevicePowerState(IntPtr handle, out bool state);
[DllImport("kernel32.dll")]
static extern uint GetLastError();
static void Main(string[] args)
{
IntPtr handle = FindWindow(null, null);
IntPtr monitorHandle = MonitorFromWindow(handle, MONITOR_DEFAULTTOPRIMARY);
SendMessage(handle, SYSCOM, MONPOW, MON_OFF);
bool isOn, result;
result=GetDevicePowerState(monitorHandle, out isOn);
uint err = GetLastError();
if (result)
{
Console.WriteLine("Monitor power state: "+isOn);
}
Console.WriteLine(err);
Console.ReadKey();
}
}

ilmash
- 174
- 2
- 12
-1
CapabilitiesRequestAndCapabilitiesReply
Return Zero if monitor is turn off.

user3400760
- 25
- 3
-
not work on win10? `ctypes.windll.dxva2.CapabilitiesRequestAndCapabilitiesReply()` always return 0 – CS QGB Apr 03 '21 at 15:24