如何通过SetDisplayConfig与DISPLAYCONFIG_PATH_TARGET_INFO立即应用显示缩放模式
问题:SetDisplayConfig无法设置「保持纵横比」显示缩放模式
问题描述
尝试通过winuser.h中的SetDisplayConfig函数,为当前显示模式立即应用「保持纵横比」的显示缩放模式,但设置始终无法生效。已尝试多种方法均未成功,求指明实现方向。
已尝试方案
曾结合QueryDisplayConfig与SetDisplayConfig,修改DISPLAYCONFIG_PATH_INFO -> DISPLAYCONFIG_PATH_TARGET_INFO.scaling字段值,但修改后调用SetDisplayConfig仍不生效。
更新:当前代码及错误
以下代码中,QueryDisplayConfig和SetDisplayConfig均返回ERROR_INVALID_PARAMETER错误:
void setDisplayScalingMode(DISPLAYCONFIG_SCALING scalingMode) { // Initialize paths and modes. UINT32 numPathArrayElements = 0; UINT32 numModeInfoArrayElements = 0; DISPLAYCONFIG_PATH_INFO* pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements]; DISPLAYCONFIG_MODE_INFO* modeInfoArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements]; // Query the active display configuration. LONG queryDisplayConfigResult = QueryDisplayConfig(QDC_DATABASE_CURRENT, &numPathArrayElements, pathArray, &numModeInfoArrayElements, modeInfoArray, NULL); // Check if the active display configuration was successfully queried, and output an error message if it wasn't. if (queryDisplayConfigResult != ERROR_SUCCESS) { std::cerr << "Failed to query the display configuration! Error Code: " << queryDisplayConfigResult << std::endl; } // Set the new display scaling mode for all paths. for (UINT32 i = 0; i < numPathArrayElements; ++i) { pathArray->targetInfo.scaling = scalingMode; } // Apply the new display configuration. LONG setDisplayConfigResult = SetDisplayConfig(numPathArrayElements, pathArray, numModeInfoArrayElements, modeInfoArray, SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG); // Check if the new display configuration was successfully applied, and output an error message if it wasn't. if (setDisplayConfigResult != ERROR_SUCCESS) { std::cerr << "Failed to set the display configuration! Error Code: " << setDisplayConfigResult << std::endl; } // Clean up allocated memory delete[] pathArray; delete[] modeInfoArray; }
核心问题与解决方向
- QueryDisplayConfig调用流程错误:第一次调用时需传入NULL数组,先获取所需的路径和模式数组大小,再分配对应内存后二次调用获取实际配置。初始代码直接用大小为0的数组调用,必然返回参数错误。
- 数组遍历错误:循环中始终修改数组第一个元素的scaling值,需改为
pathArray[i].targetInfo.scaling = scalingMode来遍历所有路径。 - 参数标志与配置范围问题:
- 改用
QDC_ALL_PATHS获取完整显示路径配置,而非QDC_DATABASE_CURRENT; SetDisplayConfig可先添加SDC_VALIDATE标志验证配置有效性,再执行应用;- 确保
SDC_USE_SUPPLIED_DISPLAY_CONFIG与其他标志的组合符合要求,比如可搭配SDC_ALLOW_CHANGES。
- 改用
- 缩放模式有效性验证:先通过
DisplayConfigGetDeviceInfo获取目标显示器支持的缩放模式列表,确认传入的scalingMode(如对应保持纵横比的枚举值)在支持范围内。
内容的提问来源于stack exchange,提问作者Jonathan Miller
相关产品推荐
相关产品推荐

