首页 > 解决方案 > Electronjs如何检测监视器/屏幕是否关闭

问题描述

我们如何检测监视器是否关闭。Electron 提供了electron.screen API,我们可以在其中检索所有显示器electron.screen.getAllDisplays(),但此处提供的信息不包含有关显示器电源状态的信息:

[{
    id: 2528732444,
    bounds: { x: 0, y: 0, width: 1920, height: 1080 },
    workArea: { x: 0, y: 0, width: 1920, height: 1040 },
    accelerometerSupport: 'unknown',
    monochrome: false,
    colorDepth: 24,
    colorSpace: '{primaries:BT709, transfer:IEC61966_2_1, matrix:RGB, range:FULL}',
    depthPerComponent: 8,
    size: { width: 1920, height: 1080 },
    workAreaSize: { width: 1920, height: 1040 },
    scaleFactor: 1,
    rotation: 0,
    internal: false,
    touchSupport: 'unknown'
  },
...
]

如果显示器未插入 PC,则不会列出它。但是,如果显示器已插入 PC 并关闭,它会将其列出,electron.screen.getAllDisplays()但不提供其状态。

标签: electron

解决方案


这是不可能的,即使显示器关闭(这就是它显示在 getAllDisplays() 中的原因),DVI 或 HDMI 之类的东西仍会报告屏幕已连接。

我能想到的唯一选择是运行一个访问 Win32 API 的 shell 脚本,但这将不再基于 Electron。

您将使用Windows 的电源设置 GUID

GUID_CONSOLE_DISPLAY_STATE

6FE69556-704A-47A0-8F24-C28D936FDA47

The current monitor's display state has changed.

Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This notification is available starting with Windows 8 and Windows Server 2012.

The Data member is a DWORD with one of the following values.

0x0 - The display is off.

0x1 - The display is on.

0x2 - The display is dimmed.

推荐阅读