// ChangeScreenResolution.cpp : Defines the entry point for the console application. // // This is a command line tool for changing the screen setting to another resolution. // /* Copyright (c) 2005 Harald Vogt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "stdafx.h" #include char* progname; void usage() { printf("Usage: %s 1024x768|1280x1024\n", progname); printf("Usage: %s \n", progname); } int _tmain(int argc, _TCHAR* argv[]) { DEVMODE mode; DISPLAY_DEVICE ddevice; BOOL r; LONG lr; DWORD flags; int ux, uy; progname = argv[0]; if (argc > 2) { ux = atoi(argv[1]); uy = atoi(argv[2]); if (ux == 0 || uy == 0) { usage(); exit(0); } } else if (argc > 1) { if (strcmp(argv[1], "1024x768") == 0) { ux = 1024; uy = 768; } else if (strcmp(argv[1], "1280x1024") == 0) { ux = 1280; uy = 1024; } else { usage(); exit(0); } } else { usage(); exit(0); } // First, get the name of the main display // We assume that the index of the main display is always zero. ddevice.cb = sizeof(DISPLAY_DEVICE); r = EnumDisplayDevices(NULL, 0, &ddevice, 0); if (r) { //printf("%s\n", ddevice.DeviceName); //printf("%s\n", ddevice.DeviceID); if ((ddevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) > 0) { //printf("attached to desktop\n"); } else { printf("Error: Display not attached to desktop - failing\n"); exit(1); } } else { printf("Error: EnumDisplayDevices failed\n"); exit(1); } mode.dmSize = sizeof(DEVMODE); mode.dmDriverExtra = 0; strncpy((char*)mode.dmDeviceName, ddevice.DeviceName, CCHDEVICENAME); mode.dmPelsWidth = ux; mode.dmPelsHeight = uy; mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; flags = CDS_UPDATEREGISTRY | CDS_GLOBAL; //printf("device name: %s (%d)\n", mode.dmDeviceName, CCHDEVICENAME); lr = ChangeDisplaySettings(&mode, flags); if (lr != DISP_CHANGE_SUCCESSFUL) { if (lr == DISP_CHANGE_BADMODE) { printf("Error: Mode not supported\n"); } else if (lr == DISP_CHANGE_RESTART) { printf("Warning: The computer needs to restart before the change takes effect\n"); } else { printf("Error, code = %ld\n", lr); } } return 0; }