Display Duplicate optionally with three monitors connected in the OS

Ye, Chengyang 1 Reputation point
2020-08-04T02:30:51.523+00:00
I am trying to display with 3 monitors, ordered in 1,2,3.  I need to config the 3 monitors by programming. How to clone or duplicate 2 of the 3 monitors optionally? For example 1|2 or 2|3 or 1|3 duplicate?
I do not know which API I can use to do this function. Do you have idea or sample code for reference?
Thanks!
Windows development Windows API - Win32
{count} votes

2 answers

Sort by: Most helpful
  1. Rita Han - MSFT 2,171 Reputation points
    2020-08-07T10:35:36.777+00:00

    Hello,

    Take 1|2 duplicate as an example.

    1. Find active path (associate with monitor 1) via DISPLAYCONFIG_PATH_ACTIVE which indicates that the path is active and part of the desktop.
    2. Find available target (associate with monitor 2) via targetAvailable. It has a different targetInfo.id (compared to active path's targetInfo.id).
    3. 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!

    0 comments No comments

  2. Nouman 1 Reputation point
    2021-10-11T00:39:44.607+00:00

    Hi @Rita Han - MSFT , I can't get this to work and I'm desperate need to get this working, could you please help me with it. Thanks is advance !

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.