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 Use SetDisplayConfig in Windows to Manage Multiple Displays

In the Windows environment, managing multiple display configurations is crucial for enhancing productivity and ensuring an optimal user experience. The SetDisplayConfig function is a powerful tool provided by the Windows API that allows users to programmatically configure display settings such as resolution, orientation, and the arrangement of multiple monitors. This article will guide you through the process of using SetDisplayConfig in Windows, explaining its importance and providing practical examples to help you get started.

Examples:

Example 1: Basic Use of SetDisplayConfig in C++

To use SetDisplayConfig, you need to include the necessary headers and link against the correct libraries. Below is a basic example in C++ that demonstrates how to change the display configuration.

#include <Windows.h>
#include <iostream>

int main() {
    // Define the desired display mode
    DEVMODE devMode;
    memset(&devMode, 0, sizeof(devMode));
    devMode.dmSize = sizeof(devMode);
    devMode.dmPelsWidth = 1920;
    devMode.dmPelsHeight = 1080;
    devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;

    // Apply the display mode
    LONG result = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
    if (result == DISP_CHANGE_SUCCESSFUL) {
        std::cout << "Display settings changed successfully." << std::endl;
    } else {
        std::cout << "Failed to change display settings." << std::endl;
    }

    return 0;
}

Example 2: Using PowerShell to Change Display Settings

While SetDisplayConfig is typically used in C++ applications, you can also manage display settings using PowerShell, which provides a more accessible scripting environment.

# Load the necessary assembly
Add-Type -AssemblyName System.Windows.Forms

# Create a screen object
$screen = [System.Windows.Forms.Screen]::AllScreens[0]

# Change the primary screen resolution
$width = 1920
$height = 1080

# Create a DEVMODE structure
$devMode = New-Object PSObject -Property @{
    dmSize = 0
    dmPelsWidth = $width
    dmPelsHeight = $height
    dmFields = 0x00080000 -bor 0x00040000
}

# Apply the new display settings
$result = [System.Runtime.InteropServices.Marshal]::StructureToPtr($devMode, [System.IntPtr]::Zero, $false)
if ($result -eq 0) {
    Write-Output "Display settings changed successfully."
} else {
    Write-Output "Failed to change display settings."
}

Example 3: Using Command Prompt to Change Display Orientation

Although SetDisplayConfig is not directly accessible via the Command Prompt, you can use the DisplaySwitch.exe utility to change display orientations and configurations.

@echo off
:: Change display orientation to landscape
DisplaySwitch.exe /internal

This command sets the primary display to internal, which is useful for laptops and tablets.

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.