为何断开所有物理显示器后DISPLAY_DEVICE仍返回有效显示信息?
无物理显示器时Windows仍报告活跃显示的原因分析
我正在开发一款Win32桌面C++应用,测试断开所有物理显示器后使用DISPLAY_DEVICE获取显示信息的场景。发现即使没有物理显示器连接,该API仍报告存在活跃显示。想了解:Windows是否会在无物理显示器时保留活跃的虚拟显示器?
示例代码
void GetMonitorInfoUsingDisplayDevice() { DISPLAY_DEVICE displayDevice; displayDevice.cb = sizeof(DISPLAY_DEVICE); int deviceIndex = 0; while (EnumDisplayDevices(NULL, deviceIndex, &displayDevice, 0)) { if (!(displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) { deviceIndex++; continue; // Skip displays that are not part of the desktop } std::wstringstream debugMessage; debugMessage << L"Display Device " << deviceIndex << L": " << displayDevice.DeviceName << L" - " << displayDevice.DeviceString << L"\n"; // Check if the display is active if (displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE) { debugMessage << L" Status: Active\n"; } else { debugMessage << L" Status: Inactive\n"; } // Check if it's the primary display if (displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) { debugMessage << L" Primary Display: Yes\n"; } else { debugMessage << L" Primary Display: No\n"; } // Now, enumerate the monitor attached to this display device DISPLAY_DEVICE monitorDevice; monitorDevice.cb = sizeof(DISPLAY_DEVICE); int monitorIndex = 0; while (EnumDisplayDevices(displayDevice.DeviceName, monitorIndex, &monitorDevice, 0)) { debugMessage << L" Monitor " << monitorIndex << L": " << monitorDevice.DeviceName << L" - " << monitorDevice.DeviceString << L"\n"; monitorIndex++; } // Retrieve screen resolution using DEVMODE DEVMODE devMode; devMode.dmSize = sizeof(DEVMODE); if (EnumDisplaySettings(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &devMode)) { debugMessage << L" Screen Resolution: " << devMode.dmPelsWidth << L" x " << devMode.dmPelsHeight << L" pixels\n"; debugMessage << L" Refresh Rate: " << devMode.dmDisplayFrequency << L" Hz\n"; } else { debugMessage << L" Could not retrieve display settings.\n"; } debugMessage << L"---------------------------------\n"; // Send output to Visual Studio Debug window LogToDebugWindow(debugMessage.str()); deviceIndex++; } }
测试输出
连接1台物理显示器时的输出
Display Device 1: \.\DISPLAY2 - Intel(R) UHD Graphics 770 Status: Active Primary Display: Yes Monitor 0: \.\DISPLAY2\Monitor0 - Generic PnP Monitor Screen Resolution: 1920 x 1080 pixels Refresh Rate: 60 Hz
断开所有物理显示器后的输出
Display Device 0: \.\DISPLAY1 - Intel(R) UHD Graphics 770 Status: Active Primary Display: Yes Monitor 0: \.\DISPLAY1\Monitor0 - Digital Flat Panel (640x480 60Hz) Screen Resolution: 1920 x 1080 pixels Refresh Rate: 60 Hz
解答
- 是的,Windows确实会在无物理显示器连接时保留一个虚拟显示设备,这是系统的默认行为。
- 这种虚拟显示由显卡驱动提供的“无显示器模式”实现,目的是保证系统桌面环境正常运行——很多后台服务、远程桌面工具依赖活跃的显示上下文,断开所有物理显示器后系统不会销毁显示设备,而是用虚拟显示器替代。
- 从输出可以看到,断开显示器后系统切换到了
\.\DISPLAY1,对应的监视器是“Digital Flat Panel (640x480 60Hz)”,这就是显卡驱动模拟的虚拟显示设备,它会沿用之前的分辨率设置维持桌面环境。 - 另外,
DISPLAY_DEVICE_ACTIVE标记表示设备处于活跃状态,即使没有物理显示器,只要系统需要维持显示上下文,这个标记就会被设置。
内容的提问来源于stack exchange,提问作者Harshith
相关产品推荐
相关产品推荐

