如何获取Windows系统中已连接显示器的最高刷新率?
使用CCD API获取显示器可选刷新率及最大值
核心实现步骤(基于CCD API)
- 枚举显示路径和模式
先调用DisplayConfigGetDisplayBufferSizes获取缓冲区大小,再调用DisplayConfigQueryDisplayConfig(传入QDC_ALL_PATHS参数)枚举所有显示路径与模式信息,得到DISPLAYCONFIG_PATH_INFO和DISPLAYCONFIG_MODE_INFO数组。 - 匹配目标显示器的模式
遍历DISPLAYCONFIG_PATH_INFO,通过DisplayConfigGetDeviceInfo获取每个路径对应的显示器名称,匹配到目标显示器后,记录其targetInfo.targetId;再遍历DISPLAYCONFIG_MODE_INFO数组,筛选类型为DISPLAYCONFIG_MODE_TYPE_TARGET且targetMode.targetId匹配的条目,这些就是该显示器的所有可选模式。 - 计算最高刷新率
每个DISPLAYCONFIG_TARGET_MODE的refreshRate字段包含刷新率的分子(Numerator)和分母(Denominator),刷新率数值为Numerator / Denominator。遍历所有匹配模式,计算并记录最大值。
关键代码示例(C++)
#include <Windows.h> #include <vector> #include <algorithm> #include <cwchar> double GetMaxRefreshRate(LPCWSTR targetDisplayName) { UINT32 pathCount = 0; UINT32 modeCount = 0; LONG result = DisplayConfigGetDisplayBufferSizes(QDC_ALL_PATHS, &pathCount, &modeCount); if (result != ERROR_SUCCESS) return 0.0; std::vector<DISPLAYCONFIG_PATH_INFO> paths(pathCount); std::vector<DISPLAYCONFIG_MODE_INFO> modes(modeCount); result = DisplayConfigQueryDisplayConfig(QDC_ALL_PATHS, &pathCount, paths.data(), &modeCount, modes.data(), nullptr); if (result != ERROR_SUCCESS) return 0.0; double maxRefresh = 0.0; for (const auto& path : paths) { DISPLAYCONFIG_TARGET_DEVICE_NAME targetName = {}; targetName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; targetName.header.size = sizeof(targetName); targetName.header.adapterId = path.targetInfo.adapterId; targetName.header.id = path.targetInfo.id; result = DisplayConfigGetDeviceInfo(&targetName.header); if (result != ERROR_SUCCESS) continue; if (_wcsicmp(targetName.monitorFriendlyDeviceName, targetDisplayName) != 0) continue; for (const auto& mode : modes) { if (mode.type != DISPLAYCONFIG_MODE_TYPE_TARGET) continue; if (!IsEqualGUID(mode.targetMode.targetId, path.targetInfo.targetId)) continue; double currentRefresh = static_cast<double>(mode.targetMode.refreshRate.Numerator) / mode.targetMode.refreshRate.Denominator; if (currentRefresh > maxRefresh) { maxRefresh = currentRefresh; } } break; } return maxRefresh; }
fallback方案(EnumDisplaySettings API)
如果CCD API存在兼容问题,可使用传统的EnumDisplaySettings:
- 先通过
EnumDisplayDevices获取显示器的设备名; - 循环调用
EnumDisplaySettings,每次传入递增的模式索引,直到返回失败,收集DEVMODE结构体中的dmDisplayFrequency字段; - 对比所有收集到的刷新率,取最大值;
- 可通过
DISPLAYCONFIG_TARGET_DEVICE_NAME中的devicePath字段,关联CCD API与EnumDisplayDevices获取的设备信息,保证显示器匹配一致。
内容的提问来源于stack exchange,提问作者Julien BERNARD
相关产品推荐
相关产品推荐

