2,782 questions
Hello,
Take 1|2 duplicate as an example.
- Find active path (associate with monitor 1) via DISPLAYCONFIG_PATH_ACTIVE which indicates that the path is active and part of the desktop.
- Find available target (associate with monitor 2) via targetAvailable. It has a different
targetInfo.id
(compared to active path'stargetInfo.id
). - Assign the same source id and modeInfoIdx to the path you target. Set
DISPLAYCONFIG_PATH_ACTIVE
flat to enable this path. Call SetDisplayConfig to duplicate. (clone)
The following is an example you can refer to:
#include <iostream>
#include <windows.h>
#include <vector>
int main()
{
UINT32 cPath = 0; //path count
UINT32 cMode = 0; //mode count
HRESULT hr;
UINT32 activePathIndex = 0;
UINT32 activeTargetId = 0;
hr = GetDisplayConfigBufferSizes(QDC_ALL_PATHS, &cPath, &cMode);
std::vector<DISPLAYCONFIG_PATH_INFO> pathArray(cPath);
std::vector<DISPLAYCONFIG_MODE_INFO> modeArray(cMode);
hr = QueryDisplayConfig(QDC_ALL_PATHS, &cPath, &pathArray[0], &cMode, &modeArray[0], NULL);
for (UINT32 i = 0; i < cPath; i++)
{
if (pathArray[i].flags & DISPLAYCONFIG_PATH_ACTIVE)
{
activePathIndex = i;
activeTargetId = pathArray[i].targetInfo.id;
printf("activePathIndex: %d, activeTargetId: %d\n", i, activeTargetId);
continue;
}
if (activeTargetId == pathArray[i].targetInfo.id)
continue;
if (!pathArray[i].targetInfo.targetAvailable)
continue;
pathArray[i].flags |= DISPLAYCONFIG_PATH_ACTIVE;
pathArray[i].sourceInfo.modeInfoIdx = pathArray[0].sourceInfo.modeInfoIdx; //same source
pathArray[i].sourceInfo.id = pathArray[0].sourceInfo.id; //same source
hr = SetDisplayConfig(cPath, &pathArray[0], cMode, &modeArray[0], SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_ALLOW_CHANGES | SDC_SAVE_TO_DATABASE);
if (hr == ERROR_SUCCESS) {
printf("Duplicate succeed !");
break;
}
}
}
Thank you!