简体   繁体   English

有没有办法检测 Windows 中的监视器状态(打开或关闭)?

[英]Is there any way to detect the monitor state in Windows (on or off)?

Does anyone know if there is an API to get the current monitor state (on or off) in Windows (XP/Vista/2000/2003)?有谁知道在 Windows (XP/Vista/2000/2003) 中是否有一个 API 来获取当前的监视器状态(打开或关闭)?

All of my searches seem to indicate there is no real way of doing this.我所有的搜索似乎都表明没有真正的方法可以做到这一点。

This thread tries to use GetDevicePowerState which according to Microsoft's docs does not work for display devices.该线程尝试使用GetDevicePowerState ,根据 Microsoft 的文档,它不适用于显示设备。

In Vista I can listen to GUID_MONITOR_POWER_ON but I do not seem to get events when the monitor is turned off manually.在 Vista 中,我可以收听GUID_MONITOR_POWER_ON但在手动关闭监视器时我似乎没有收到事件。

In XP I can hook into WM_SYSCOMMAND SC_MONITORPOWER , looking for status 2. This only works for situations where the system triggers the power off.在 XP 中,我可以连接到WM_SYSCOMMAND SC_MONITORPOWER ,寻找状态 2。这仅适用于系统触发电源关闭的情况。

The WMI Win32_DesktopMonitor class does not seem to help out as well. WMI Win32_DesktopMonitor类似乎也没有帮助。

Edit : Here is a discussion on comp.os.ms-windows.programmer.win32 indicating there is no reliable way of doing this.编辑:这是关于 comp.os.ms-windows.programmer.win32 的讨论,表明没有可靠的方法可以做到这一点。

Anyone else have any other ideas?其他人有任何其他想法吗?

GetDevicePowerState sometimes works for monitors. GetDevicePowerState有时适用于监视器。 If it's present, you can open the \\\\.\\LCD device.如果存在,您可以打开\\\\.\\LCD设备。 Close it immediately after you've finished with it.完成后立即关闭它。

Essentially, you're out of luck—there is no reliable way to detect the monitor power state, short of writing a device driver and filtering all of the power IRPs up and down the display driver chain.从本质上讲,您很不走运——没有可靠的方法来检测显示器电源状态,除非编写设备驱动程序并过滤显示驱动程序链上下的所有电源 IRP。 And that's not very reliable either.而且这也不是很可靠。

您可以连接网络摄像头,将其对准屏幕并对收到的图像进行一些分析;)

在根据监视器状态做任何事情之前,请记住,用户可以使用具有其他系统远程桌面的机器,不需要将监视器连接到机器 - 所以不要关闭任何基于监视器状态的可视化。

You can't.你不能。

Look like all monitor power capabilities connected to the "power safe mode"看起来所有显示器电源功能都连接到“电源安全模式”
After searching i found here code that connecting between SC_MONITORPOWER message and system values (post number 2)搜索后,我在 这里找到 连接SC_MONITORPOWER消息和系统值的代码(帖子编号 2)
I use the code to testing if the system values is changing when i am manually switch off the monitor.我使用代码来测试当我手动关闭显示器时系统值是否发生变化。

int main()
{
    for(;monitorOff()!=1;)
        Sleep(500);
    return 0;
}//main

And the code is never stopped, no matter how long i am switch off my monitor.无论我关闭显示器多久,代码都不会停止。
There the code of monitorOff function:有monitorOff函数的代码:

int monitorOff()
{
    const GUID MonitorClassGuid =
        {0x4d36e96e, 0xe325, 0x11ce, 
            {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};

    list<DevData> monitors;
    ListDeviceClassData(&MonitorClassGuid, monitors);

    list<DevData>::iterator it = monitors.begin(),
                            it_end = monitors.end();
    for (; it != it_end; ++it)
    {
        const char *off_msg = "";

        //it->PowerData.PD_PowerStateMapping
        if (it->PowerData.PD_MostRecentPowerState != PowerDeviceD0)
        {
            return 1;
        }
    }//for

    return 0;
}//monitorOff

Conclusion : when you manually switch of the the monitor, you cant catch it by windows (if there is no unusual driver interface for this), because all windows capabilities is connected to "power safe mode" .结论:当您手动切换显示器时,您无法通过 windows 捕捉到它(如果没有异常的驱动程序接口),因为所有 windows 功能都连接到“电源安全模式”

In Windows XP or later you can use the IMSVidDevice Interface.在 Windows XP 或更高版本中,您可以使用 IMSVidDevice 接口。

See http://msdn.microsoft.com/en-us/library/dd376775(VS.85).aspx请参阅http://msdn.microsoft.com/en-us/library/dd376775(VS.85).aspx

(not sure if this works in Sever 2003) (不确定这是否适用于 Sever 2003)

If your monitor has some sort of built-in USB hub, you could try and use that to detect if the monitor is off/on.如果您的显示器有某种内置的 USB 集线器,您可以尝试使用它来检测显示器是否关闭/打开。
This will of course only work if the USB hub doesn't stay connected when the monitor is consider "off".这当然只有在显示器被认为“关闭”时 USB 集线器没有保持连接的情况下才有效。

With Delphi code, you can detect invalid monitor geomerty while standby in progress:使用 Delphi 代码,您可以在待机期间检测无效的监视器几何体:

i := 0
('Monitor'+IntToStr(i)+': '+IntToStr(Screen.Monitors[i].BoundsRect.Left)+', '+
IntToStr(Screen.Monitors[i].BoundsRect.Top)+', '+
IntToStr(Screen.Monitors[i].BoundsRect.Right)+', '+
IntToStr(Screen.Monitors[i].BoundsRect.Bottom))

Results:结果:

Monitor geometry before standby:待机前监控几何:

Monitor0: 0, 0, 1600, 900

Monitor geometry while standby in Deplhi7:在 Deplhi7 中待机时监控几何:

Monitor0: 1637792, 4210405, 31266576, 1637696

Monitor geometry while standby in DeplhiXE:在 DeplhiXE 中待机时监控几何:

Monitor0: 4211194, 40, 1637668, 1637693

This is a really old post but if it can help someone, I have found a solution to detect a screen being available or not : the Connecting and Configuring Displays (CCD) API of Windows.这是一篇很老的帖子,但如果它可以帮助某人,我找到了一种检测屏幕可用与否的解决方案:Windows 的连接和配置显示器 (CCD) API。

It's part of User32.ddl and the interesting functions are GetDisplayConfigBufferSizes and QueryDisplayConfig .它是User32.ddl的一部分,有趣的函数是GetDisplayConfigBufferSizesQueryDisplayConfig It give us all informations that can be viewed in the Configuration Panel of windows.它为我们提供了可以在窗口的配置面板中查看的所有信息。

In particular the PathInfo contains a TargetInfo property that have a targetAvailable flag.特别是PathInfo包含具有targetAvailable标志的TargetInfo属性。 This flag seems to be correctly updated on all the configurations I have tried so far.到目前为止,我尝试过的所有配置似乎都正确更新了此标志。

This allow you to know the state of every screens connected to the PC and set their configurations.这使您可以了解连接到 PC 的每个屏幕的状态并设置其配置。

Here a CCD wrapper for .Net 这是 .Net 的 CCD 包装器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3:在Windows中检测显示器电源状态 - Python 3: Detect monitor power state in Windows 有什么方法可以检查哪个显示器是主要显示器,然后在Windows命令行中基于该显示器执行命令? - Is there any way to check which monitor is the primary display, then execute a command based off of that in windows command line? 有没有办法检测显示器是否插入? - Is there a way to detect if a monitor is plugged in? 有什么方法不能从Windows PC中检测USB? - Is there any way Not to detect USB from windows PC? electron 中是否有任何方法可以检测 Windows 上的关机? - Is there any way in electron to detect a shutdown on Windows? 如何在Windows 7中检查PC监视器是否已打开或关闭任何工具或事件查看器 - How to check PC monitor is turned on or off any tool or event viewer in windows 7 有什么方法可以检测 Windows 服务器是在有头还是无头中运行? - Is there any way to detect whether Windows server is running in headed or headless? 在IE中使用JavaScript的Windows中可以使用任何检测URI模式的方法 - Any way to detect URI schema is available in windows using JavaScript in IE 如何检测Windows中的显示器是否为宽屏 - How to detect whether a monitor is widescreen in Windows Windows 10 S用户代理字符串中的边缘? 以编程方式检测Windows 10 S的任何其他方法? - Edge in Windows 10 S user agent string? Any other way to programmatically detect Windows 10 S?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM