如何使用 SetDisplayConfig 和 DISPLAYCONFIG_PATH_TARGET_INFO 立即应用 DISPLAYCONFIG_SCALING 显示缩放模式

问题描述 投票:0回答:1

我目前正在尝试使用 winuser.h 中的 SetDisplayConfig 立即为活动显示模式应用“保留宽高比”显示缩放模式,但我似乎无法应用设置。我尝试了很多方法,但没有任何效果。有人能指出我实施这个的正确方向吗?任何帮助将不胜感激。

相关API:

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setdisplayconfig

https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-displayconfig_path_info

https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-displayconfig_path_target_info

让我开始的文章:

https://learn.microsoft.com/en-us/windows-hardware/drivers/display/scaling-the-desktop-image

我尝试使用 SetDisplayConfig 和 QueryDisplayConfig 来更改 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 scaling mode.
    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;
}
c++ winapi windows-10 implementation windows-11
1个回答
0
投票

我能够正确获取显示缩放模式值,并在进行以下更改后立即应用:

 // Variable to hold the current display topology after querying the display configuration.
DISPLAYCONFIG_TOPOLOGY_ID* currentTopology = new DISPLAYCONFIG_TOPOLOGY_ID;

// Retrieve the size of the buffers that are required to call the QueryDisplayConfig function.
GetDisplayConfigBufferSizes(QDC_DATABASE_CURRENT, &numPathArrayElements, &numModeInfoArrayElements);

// Initialize the path array and mode info array with the correct buffers to hold the active paths as defined in the
// persistence database for the currently connected monitors.
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, currentTopology);

我的 SetDisplay 程序中提供了完整的实现。谢谢大家为我指明了正确的方向!

© www.soinside.com 2019 - 2024. All rights reserved.