CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2008
    Posts
    48

    How to set Display Settings to EXTEND mode in Windows 7 using C++?

    In my C++ Win32 program I want to set the current Display Settings to "Extend" mode. I Googled and found out that SetDisplayConfig() is the way to go forward in Windows 7 (I'm on Windows 7) but now I am kind of stuck how to proceed as the MSDN explanation (link http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx) is pretty confusing. I am very much new to C++ and API programming so I am finding it difficult to understand this.

    I would highly appreciate some code sample and an explanation. Thanks in advance!
    Last edited by sachintha81; July 5th, 2011 at 11:02 PM.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    Try
    Code:
    SetDisplayConfig(0,NULL,0,NULL,SDC_TOPOLOGY_EXTEND|SDC_APPLY);
    or
    Code:
    SetDisplayConfig(0,NULL,0,NULL,SDC_TOPOLOGY_CLONE|SDC_APPLY);

  3. #3
    Join Date
    Jul 2008
    Posts
    48

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    olivthill2, thanks for the response.

    Is that all I have to do? Do I not need to check already existing configuration and so on?

    By the way, I encounter another problem trying to use this code. As stated in the MSDN page, I included "Windows.h" header, but still when compiled it gives me a bunch of errors saing SetDisplayConfig() etc is not defined. What could be the problem?

  4. #4
    Join Date
    Jul 2008
    Posts
    48

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    Okay so I found the solution to the second part of my problem (not defined problem). I had set the wrong SDK version, so first corrected it in Start Menu > All Programs > Microsoft Windows SDK 7.1 > Visual Studio Registration and by choosing the correct version (in my case 7.1) and then also in targetver.h header file I had to change WINVER and _WIN32_WINNT define statements to correct Windows version which is 0x0601 for Windows 7.

    After than I could use your code to change display settings and it worked just fine. Thanks a lot.

    Also, please let me know how can I find the currently set display setting using C++ code?

  5. #5
    Join Date
    Apr 2009
    Posts
    598

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    Get the current configuration with QueryDisplayConfig().
    See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    The mode (topology) is returned in the 6th argument.
    The 2nd, 3rd, 4th, and 5th arguments may be NULL, because they are about paths, which is something not very interesting unless you want to install or uninstall monitors.

    So, try:
    Code:
    DISPLAYCONFIG_TOPOLOGY_ID curent_mode;
    if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, NULL, NULL, NULL, NULL, &curent_mode) != ERROR_SUCCESS)
      // failed
      return(-1);
    if (curent_mode == DISPLAYCONFIG_TOPOLOGY_EXTEND)
      // View spans over several monitors
      ...
    if (curent_mode == DISPLAYCONFIG_TOPOLOGY_CLONE)
      // View is duplicated on several monitors
      ...

  6. #6
    Join Date
    Jul 2008
    Posts
    48

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    Thank you olivthill2, that was most helpful!

  7. #7
    Join Date
    Jul 2008
    Posts
    48

    Re: How to set Display Settings to EXTEND mode in Windows 7 using C++?

    Quote Originally Posted by olivthill2 View Post
    Get the current configuration with QueryDisplayConfig().
    See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    The mode (topology) is returned in the 6th argument.
    The 2nd, 3rd, 4th, and 5th arguments may be NULL, because they are about paths, which is something not very interesting unless you want to install or uninstall monitors.

    So, try:
    Code:
    DISPLAYCONFIG_TOPOLOGY_ID curent_mode;
    if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, NULL, NULL, NULL, NULL, &curent_mode) != ERROR_SUCCESS)
      // failed
      return(-1);
    if (curent_mode == DISPLAYCONFIG_TOPOLOGY_EXTEND)
      // View spans over several monitors
      ...
    if (curent_mode == DISPLAYCONFIG_TOPOLOGY_CLONE)
      // View is duplicated on several monitors
      ...
    Mate, a correction:

    I think you can not set the middle 4 parameters to be NULL, as it returns an empty current_mode variable. Looks like it is stated in MSDN too that they cannot be NULL. I had to do something like this, and it worked like a charm; gave me the correct current display setting.

    Code:
    UINT32 PathArraySize = 0;
    UINT32 ModeArraySize = 0;
    DISPLAYCONFIG_PATH_INFO* PathArray;
    DISPLAYCONFIG_MODE_INFO* ModeArray;
    DISPLAYCONFIG_TOPOLOGY_ID CurrentTopology;
    
    GetDisplayConfigBufferSizes(QDC_ALL_PATHS, &PathArraySize, &ModeArraySize);
    	
    PathArray =   (DISPLAYCONFIG_PATH_INFO*)malloc(PathArraySize * sizeof(DISPLAYCONFIG_PATH_INFO));
    memset(PathArray, 0, PathArraySize * sizeof(DISPLAYCONFIG_PATH_INFO));
    
    ModeArray =   (DISPLAYCONFIG_MODE_INFO*)malloc(ModeArraySize * sizeof(DISPLAYCONFIG_MODE_INFO));
    memset(ModeArray, 0, ModeArraySize * sizeof(DISPLAYCONFIG_MODE_INFO));
    
    LONG ret = QueryDisplayConfig(QDC_DATABASE_CURRENT,&PathArraySize, PathArray, &ModeArraySize, ModeArray, &CurrentTopology);
    
    free(PathArray);
    free(ModeArray);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured