inpostor
inpostor

Reputation: 1

Using Windows API to "turn on/off" a selected monitor

My main goal is to imitate the "Show only on 1/2" setting from Windows. I'm trying to use ChangeDisplaySettingsEx to turn a specific monitor on and off. Setting dmPelsWidth and dmPelsHeight to 0 effectively turns off the monitor, but when I try to revert to the original settings, nothing changes. I also attempted to use SendMessage but ended up affecting all monitors.

`
#include <iostream>
#include <windows.h>
using namespace std;

int main() {
    DISPLAY_DEVICEA device;
    DEVMODE settings;
    DEVMODE old_settings;
    int iDevNum = 0;

    ZeroMemory(&device, sizeof(device));
    ZeroMemory(&settings, sizeof(settings));
    ZeroMemory(&old_settings, sizeof(old_settings));

    device.cb = sizeof(DISPLAY_DEVICEA);
    settings.dmSize = sizeof(DEVMODE);
    old_settings.dmSize = sizeof(DEVMODE);

    while (EnumDisplayDevices(NULL, iDevNum, &device, EDD_GET_DEVICE_INTERFACE_NAME)) {
        if (strcmp(device.DeviceName, "\\\\.\\DISPLAY1") == 0) {
            EnumDisplaySettings(device.DeviceName, ENUM_CURRENT_SETTINGS, &settings);
            old_settings = settings;

            // Turn off the monitor
            settings.dmPelsWidth = 0;
            settings.dmPelsHeight = 0;
            ChangeDisplaySettingsEx(device.DeviceName, &settings, NULL, CDS_UPDATEREGISTRY | CDS_GLOBAL, NULL);

            // Wait for user input
            cin.get();

            // Restore the original settings
            ChangeDisplaySettingsEx(device.DeviceName, &old_settings, NULL, CDS_UPDATEREGISTRY | CDS_GLOBAL, NULL);

            break;
        }
        iDevNum++;
    }

    return 0;
}
`

Upvotes: 0

Views: 110

Answers (0)

Related Questions