Hi, Does anybody know how to enable/disable a secondary monitor using ChangeDisplaySettingsEx or any other function?

Dani AI

Generated

did the right thing by checking the driver control panel; that’s usually the easiest route and what meant by “do it manually.” For a one‑click, programmatic toggle there are two practical, supported approaches: on Windows 7 and later use the QueryDisplayConfig / SetDisplayConfig APIs to change the display topology; on older systems you can use EnumDisplayDevices + ChangeDisplaySettingsEx and detach the device via a DEVMODE tweak. (learn.microsoft.com)

A common SetDisplayConfig workflow (Windows 7+) is: call GetDisplayConfigBufferSizes, QueryDisplayConfig to fetch current paths/modes, mark the path(s) you want to disable as inactive, then call SetDisplayConfig with SDC_APPLY (and SDC_ALLOW_CHANGES / SDC_USE_SUPPLIED_DISPLAY_CONFIG as needed). The snippet below shows the idea — always check return codes and limit changes to the specific paths you mean to touch. (learn.microsoft.com)

#define _WIN32_WINNT 0x0601
#include <windows.h>
#include <vector>

void ToggleDisableSecondaries()
{
    UINT32 pathCount=0, modeCount=0;
    GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount);
    std::vector<DISPLAYCONFIG_PATH_INFO> paths(pathCount);
    std::vector<DISPLAYCONFIG_MODE_INFO> modes(modeCount);
    QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, paths.data(), &modeCount, modes.data(), nullptr);

    for (UINT32 i=0;i<pathCount;++i) {
        UINT32 src = paths[i].sourceInfo.modeInfoIdx;
        if (src < modeCount) {
            POINTL p = modes[src].sourceMode.position;
            if (p.x != 0 || p.y != 0) paths[i].flags = 0; // deactivate non-primary
        }
    }

    SetDisplayConfig(pathCount, paths.data(), modeCount, modes.data(),
                     SDC_APPLY | SDC_ALLOW_CHANGES | SDC_USE_SUPPLIED_DISPLAY_CONFIG);
}

For legacy code (pre‑Windows 7) you can enumerate devices with EnumDisplayDevices, prepare a DEVMODE and “detach” the monitor by setting width/height to zero (the MSDN docs describe this behavior), then call ChangeDisplaySettingsEx for each device (use CDS_UPDATEREGISTRY | CDS_NORESET) and finally call ChangeDisplaySettingsEx(NULL, NULL, 0, 0, NULL) to apply. Test on the target OS and GPU — some vendor drivers keep their own settings and may override or require their APIs/tools. (learn.microsoft.com)

Quick tips: always log and check API return values, test on a multi‑GPU/multi‑monitor setup like your users have, and prefer SetDisplayConfig for modern Windows builds. If driver control‑panel hotkeys already work on the target machines, invoking those vendor tools (or using their command‑line APIs) is often the most reliable option. (learn.microsoft.com)

Recommended Answers

All 3 Replies

Why not just do it manually? It'l take less time than that way.

I need it completely disabled for my game to work. My game runs slow with both monitors enabled, so I wanted to make a one click program to disable one of the monitors rather than go through the display properties every time.

I need something to change the display settings from "extend these displays" to "show desktop only on 2"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.