Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Configure Display Settings Using SetDisplayConfig in Windows

The SetDisplayConfig function is a powerful tool in the Windows environment for configuring display settings programmatically. It is part of the Windows API and allows developers and system administrators to change display configurations such as orientation, resolution, and multiple display setups.

Understanding SetDisplayConfig

SetDisplayConfig is used primarily in applications that need to control display settings without user intervention. It is a part of the Windows Graphics Device Interface (GDI) and is typically used in C++ applications. This function is particularly useful in environments where displays need to be managed dynamically, such as in digital signage or multi-monitor setups.

Practical Examples

Below are examples demonstrating how to use SetDisplayConfig in a C++ application to change display settings.

Example 1: Changing Display Orientation

This example changes the orientation of a display to landscape mode.

#include <windows.h>

int main() {
    DISPLAYCONFIG_PATH_INFO pathInfoArray[1];
    DISPLAYCONFIG_MODE_INFO modeInfoArray[1];
    UINT32 numPathArrayElements = 1;
    UINT32 numModeInfoArrayElements = 1;

    // Initialize the path and mode info arrays
    ZeroMemory(pathInfoArray, sizeof(pathInfoArray));
    ZeroMemory(modeInfoArray, sizeof(modeInfoArray));

    // Query the current display configuration
    LONG result = GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPathArrayElements, &numModeInfoArrayElements);
    if (result != ERROR_SUCCESS) {
        return -1;
    }

    // Set the desired orientation
    modeInfoArray[0].infoType = DISPLAYCONFIG_MODE_INFO_TYPE_TARGET;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.videoStandard = DISPLAYCONFIG_VIDEO_STANDARD_NTSC;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.vSyncFreq.Numerator = 60;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.vSyncFreq.Denominator = 1;

    // Apply the new display configuration
    result = SetDisplayConfig(numPathArrayElements, pathInfoArray, numModeInfoArrayElements, modeInfoArray, SDC_APPLY | SDC_SAVE_TO_DATABASE);
    if (result != ERROR_SUCCESS) {
        return -1;
    }

    return 0;
}

Example 2: Setting Up a Dual Monitor Configuration

This example configures a dual monitor setup with extended display.

#include <windows.h>

int main() {
    DISPLAYCONFIG_PATH_INFO pathInfoArray[2];
    DISPLAYCONFIG_MODE_INFO modeInfoArray[2];
    UINT32 numPathArrayElements = 2;
    UINT32 numModeInfoArrayElements = 2;

    // Initialize the path and mode info arrays
    ZeroMemory(pathInfoArray, sizeof(pathInfoArray));
    ZeroMemory(modeInfoArray, sizeof(modeInfoArray));

    // Query the current display configuration
    LONG result = GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPathArrayElements, &numModeInfoArrayElements);
    if (result != ERROR_SUCCESS) {
        return -1;
    }

    // Configure the first display
    modeInfoArray[0].infoType = DISPLAYCONFIG_MODE_INFO_TYPE_TARGET;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.videoStandard = DISPLAYCONFIG_VIDEO_STANDARD_NTSC;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.vSyncFreq.Numerator = 60;
    modeInfoArray[0].targetMode.targetVideoSignalInfo.vSyncFreq.Denominator = 1;

    // Configure the second display
    modeInfoArray[1].infoType = DISPLAYCONFIG_MODE_INFO_TYPE_TARGET;
    modeInfoArray[1].targetMode.targetVideoSignalInfo.videoStandard = DISPLAYCONFIG_VIDEO_STANDARD_NTSC;
    modeInfoArray[1].targetMode.targetVideoSignalInfo.vSyncFreq.Numerator = 60;
    modeInfoArray[1].targetMode.targetVideoSignalInfo.vSyncFreq.Denominator = 1;

    // Apply the new display configuration
    result = SetDisplayConfig(numPathArrayElements, pathInfoArray, numModeInfoArrayElements, modeInfoArray, SDC_APPLY | SDC_SAVE_TO_DATABASE);
    if (result != ERROR_SUCCESS) {
        return -1;
    }

    return 0;
}

Important Considerations

  • Permissions: Modifying display settings may require administrative privileges.
  • Error Handling: Always check the return values of the functions for error handling.
  • Testing: Test configurations in a controlled environment to ensure that changes do not disrupt the user experience.

Alternatives

For users not familiar with C++ or those looking for simpler solutions, Windows provides built-in tools like the Display Settings in the Control Panel or the DisplaySwitch command-line utility for basic display configuration tasks.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.