Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
引数に2つの解像度を指定して切り替えるC++コード
#include <iostream>
#include <string>
#include <Windows.h>
int main(int argc, char* argv[]) {
if (argc != 6)
{
printf("Please arguments: monitorNo width1 height1 width2 height2\n");
getchar();
return 0;
}
DWORD monitorNo = std::stoi(argv[1]);
DWORD width = std::stoi(argv[2]);
DWORD height = std::stoi(argv[3]);
DWORD width2 = std::stoi(argv[4]);
DWORD height2 = std::stoi(argv[5]);
DISPLAY_DEVICE device;
device.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(NULL, monitorNo, &device, NULL);
DEVMODE checkMode;
EnumDisplaySettings(device.DeviceName, ENUM_CURRENT_SETTINGS, &checkMode);
if (checkMode.dmPelsWidth == width && checkMode.dmPelsHeight == height)
{
width = width2;
height = height2;
}
DEVMODE submitMode = { 0 };
submitMode.dmSize = sizeof(submitMode);
submitMode.dmPelsWidth = width;
submitMode.dmPelsHeight = height;
submitMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
DWORD ret = ChangeDisplaySettingsEx(device.DeviceName, &submitMode, NULL, 0, NULL);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment