ChangeDisplaySettings succeeds but does nothing. How do I make it work?

I am trying to use ChangeDisplaySettings to change the default display device desktop resolution. However, when I execute my function ( set_resolution), ChangeDisplaySettings always succeeds despite the fact that I do not see any obvious changes in the desktop resolution (the return code is always DISP_CHANGE_SUCCESSFUL).

I tried every value for dwFlags, but for each value I get the same result. I tried several resolutions that my display should support, but I get the same result. My display is 16: 9, native 1920x1080. I tried, for example, 1280x720.

I tried to execute set_resolutionat the same time as creating the window, and also tried to execute a function in every event WM_ACTIVATE.

LONG set_resolution(uint32_t width, uint32_t height)
{
    DEVMODE dm;

    dm.dmPelsWidth = width;
    dm.dmPelsHeight = height;
    dm.dmBitsPerPel = 32;
    dm.dmDisplayFrequency = 60;
    dm.dmFields =
        DM_PELSWIDTH |
        DM_PELSHEIGHT |
        DM_BITSPERPEL |
        DM_DISPLAYFREQUENCY;

    DWORD flags =
        0;
        //CDS_FULLSCREEN;
        //CDS_GLOBAL;
        //CDS_NORESET;
        //CDS_RESET;
        //CDS_SET_PRIMARY;
        //CDS_TEST;
        //CDS_UPDATEREGISTRY;

    LONG code = ChangeDisplaySettings(&dm, flags);

    if (code == DISP_CHANGE_SUCCESSFUL)
    {
        printf("Display change successful [%dx%d]: %d", width, height, flags);
    }
    else
    {
        printf("Display change failed [%dx%d]: %d", width, height, code);
    }

    return code;
}
+4
source share
1 answer

First remove the memory, then install dmSize. Call EnumDisplaySettingsto initialize other members.

It would be nice to have a routine to automatically undo changes if the settings were incompatible and led to a black screen ...

DEVMODE dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);

if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
    int savew = dm.dmPelsWidth;
    int saveh = dm.dmPelsHeight;
    dm.dmPelsWidth = width;
    dm.dmPelsHeight = height;

    LONG result = ChangeDisplaySettings(&dm, 0);
    if (result == DISP_CHANGE_SUCCESSFUL)
    {
        printf("okay\n");

        //Add a dialog to ask the user to confirm.
        //The dialog should close automatically if user is unable to confirm
        //if (confirm()) return;
        Sleep(5000);

        dm.dmPelsWidth = savew;
        dm.dmPelsHeight = saveh;
        ChangeDisplaySettings(&dm, 0);
    }
    else
    {
        printf("error\n");
    }
}

Change, typo fixed. I wanted to say call EnumDisplaySettingsto initialize the DEVMODEparticipants

+2
source

Source: https://habr.com/ru/post/1651828/


All Articles