[英]Is there a way to detect if a monitor is plugged in?
I have a custom application written in C++ that controls the resolution and other settings on a monitor connected to an embedded system.我有一个用 C++ 编写的自定义应用程序,它控制连接到嵌入式系统的显示器上的分辨率和其他设置。 Sometimes the system is booted headless and run via VNC, but can have a monitor plugged in later (post boot).
有时系统是无头启动并通过 VNC 运行,但可以稍后(启动后)插入监视器。 If that happens he monitor is fed no video until the monitor is enabled.
如果发生这种情况,在启用监视器之前,他的监视器不会收到任何视频。 I have found calling "displayswitch /clone" brings the monitor up, but I need to know when the monitor is connected.
我发现调用“displayswitch /clone”可以启动显示器,但我需要知道显示器何时连接。 I have a timer that runs every 5 seconds and looks for the monitor, but I need some API call that can tell me if the monitor is connected.
我有一个每 5 秒运行一次并查找监视器的计时器,但我需要一些 API 调用来告诉我监视器是否已连接。
Here is a bit of psudocode to describe what I'm after (what is executed when the timer expires every 5 seconds).这里有一些伪代码来描述我所追求的(当计时器每 5 秒到期时执行什么)。
if(If monitor connected)
{
ShellExecute("displayswitch.exe /clone);
}else
{
//Do Nothing
}
I have tried GetSystemMetrics(SM_CMONITORS)
to return the number of monitors, but it returns 1 if the monitor is connected or not.我曾尝试
GetSystemMetrics(SM_CMONITORS)
返回监视器的数量,但如果监视器已连接,则返回 1。 Any other ideas?还有其他想法吗?
Thanks!谢谢!
Try the following code试试下面的代码
BOOL IsDisplayConnected(int displayIndex = 0)
{
DISPLAY_DEVICE device;
device.cb = sizeof(DISPLAY_DEVICE);
return EnumDisplayDevices(NULL, displayIndex, &device, 0);
}
This will return true
if Windows identifies a display device with index (AKA identity) 0
(this is what the display control panel uses internally).如果 Windows 识别索引(AKA 标识)为
0
(这是显示控制面板内部使用的)的显示设备,这将返回true
。 Otherwise, it will return false false
.否则,它将返回 false
false
。 So by checking the first possible index (which I marked as the default argument), you can find out whether any display device is connected (or at least identified by Windows, which is essentially what you're looking for).因此,通过检查第一个可能的索引(我将其标记为默认参数),您可以找出是否连接了任何显示设备(或至少由 Windows 识别,这实际上是您要查找的)。
Seems that there is some kind of "default monitor" even if no real monitor is connected.即使没有连接真正的监视器,似乎也有某种“默认监视器”。 The function below works for me (tested on a Intel NUC and a Surface 5 tablet).
下面的功能对我有用(在英特尔 NUC 和 Surface 5 平板电脑上测试)。
The idea is to get the device id and check if it contains the string "default_monitor".这个想法是获取设备 id 并检查它是否包含字符串“default_monitor”。
bool hasMonitor()
{
// Check if we have a monitor
bool has = false;
// Iterate over all displays and check if we have a valid one.
// If the device ID contains the string default_monitor no monitor is attached.
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
int deviceIndex = 0;
while (EnumDisplayDevices(0, deviceIndex, &dd, 0))
{
std::wstring deviceName = dd.DeviceName;
int monitorIndex = 0;
while (EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
{
size_t len = _tcslen(dd.DeviceID);
for (size_t i = 0; i < len; ++i)
dd.DeviceID[i] = _totlower(dd.DeviceID[i]);
has = has || (len > 10 && _tcsstr(dd.DeviceID, L"default_monitor") == nullptr);
++monitorIndex;
}
++deviceIndex;
}
return has;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.