Skip to content

Instantly share code, notes, and snippets.

@mooware
Last active October 23, 2023 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooware/9388ff7b6f2595697a38 to your computer and use it in GitHub Desktop.
Save mooware/9388ff7b6f2595697a38 to your computer and use it in GitHub Desktop.
Simple program to change the display resolution on Windows
// prebuilt for VC14/VS2015 here:
// https://dl.dropboxusercontent.com/u/267889/changeres.exe
#include <iostream>
#include <sstream>
#include <Windows.h>
int main(int argc, char **argv)
{
bool reset = false;
bool keep = false;
bool error = true;
DWORD width = 0, height = 0, refresh = 0;
if (argc > 1 && strcmp(argv[1], "reset") == 0)
reset = true;
else if (argc >= 3)
{
int base = 1;
if (argc > 3 && strcmp(argv[1], "keep") == 0)
{
keep = true;
++base;
}
// std::stoul throws exceptions, and streams have better error indications,
// so put everything in a stream
std::stringstream ss;
for (int i = base; i < argc; ++i)
ss << argv[i] << ' ';
ss >> width >> height;
if (argc > base + 2)
ss >> refresh;
error = ss.fail();
}
if (error && !reset)
{
std::cout << "usage: " << argv[0] << " (reset | [keep] <width> <height> [<hz>])" << std::endl;
return 1;
}
DEVMODE mode;
if (!reset)
{
// search for a matching setting
DEVMODE iter;
iter.dmSize = sizeof(iter);
iter.dmDriverExtra = 0;
DWORD maxRefresh = 0;
DWORD maxBpp = 0;
for (DWORD i = 0; EnumDisplaySettings(NULL, i, &iter); ++i)
{
if (iter.dmPelsWidth == width && iter.dmPelsHeight == height)
{
// check fixed refresh rate
if (refresh != 0 && iter.dmDisplayFrequency != refresh)
continue;
// try to maximize color depth (and refresh rate)
if (iter.dmBitsPerPel >= maxBpp && iter.dmDisplayFrequency >= maxRefresh)
{
maxRefresh = iter.dmDisplayFrequency;
maxBpp = iter.dmBitsPerPel;
mode = iter;
}
}
}
if (maxRefresh == 0 || maxBpp == 0)
{
std::cerr << "no matching display mode found" << std::endl;
return 1;
}
else
{
std::cout << "found display mode: " << mode.dmPelsWidth << "x" << mode.dmPelsHeight
<< " at " << mode.dmDisplayFrequency << " hz " << mode.dmBitsPerPel << " bpp"
<< std::endl;
}
}
// maybe these were overwritten
mode.dmSize = sizeof(mode);
mode.dmDriverExtra = 0;
auto res = ChangeDisplaySettings((reset ? nullptr : &mode), (keep ? CDS_UPDATEREGISTRY : 0));
if (res == DISP_CHANGE_SUCCESSFUL)
return 0;
// text from MSDN
switch (res)
{
case DISP_CHANGE_BADDUALVIEW: std::cerr << "The settings change was unsuccessful because the system is DualView capable." << std::endl; break;
case DISP_CHANGE_BADFLAGS: std::cerr << "An invalid set of flags was passed in." << std::endl; break;
case DISP_CHANGE_BADMODE: std::cerr << "The graphics mode is not supported." << std::endl; break;
case DISP_CHANGE_BADPARAM: std::cerr << "An invalid parameter was passed in.This can include an invalid flag or combination of flags." << std::endl; break;
case DISP_CHANGE_FAILED: std::cerr << "The display driver failed the specified graphics mode." << std::endl; break;
case DISP_CHANGE_NOTUPDATED: std::cerr << "Unable to write settings to the registry." << std::endl; break;
case DISP_CHANGE_RESTART: std::cerr << "The computer must be restarted for the graphics mode to work." << std::endl; break;
default: std::cerr << "Unknown error code " << res << std::endl; break;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment