Mega Code Archive

 
Categories / Delphi / Examples
 

Delphi command line tool to change screen resolution

The following little commandline tool, written in Delphi, demonstrates how to retrieve all available screen resolutions (width, height, number of colors and display frequency), and how to modify the current setting. If you run the executable in a DOS box, it will write all possible settings to standard output. If you provide a set of settings e.g. 800 600 256 85, then it will switch to this (provided, that your drivers allow the setting). {$APPTYPE CONSOLE} // commandline use: DisplayMode 800 600 256 85 // no arguments: it will list all modi. program DisplayMode; uses SysUtils, Messages, Windows; {$R *.RES} var i: Integer; DevMode: TDevMode; begin i := 0; if ParamCount=0 then begin while EnumDisplaySettings(nil, i, DevMode) do begin with DevMode do Writeln(Format('%dx%d %d Colors, %d Hz', [dmPelsWidth, dmPelsHeight, Int64(1) shl dmBitsperPel, dmDisplayFrequency])); inc(i); end; Writeln('-----'); Writeln('Usage: DisplayMode '); Writeln('Example: DisplayMode 800 600 65536 85'); end else begin while EnumDisplaySettings(nil, i, DevMode) do begin with DevMode do begin if (paramstr(1)=IntToStr(dmPelsWidth)) and (paramstr(2)=IntToStr(dmPelsHeight)) and (paramstr(3)=IntToStr(Int64(1) shl dmBitsperPel)) and (paramstr(4)=IntToStr(dmDisplayFrequency)) then begin ChangeDisplaySettings(DevMode, CDS_UPDATEREGISTRY); SendMessage(HWND_BROADCAST, WM_DISPLAYCHANGE, SPI_SETNONCLIENTMETRICS, 0); break; end; end; inc(i); end end; end.