SetScreenResolution
To change the screen resolution you can use the following function which is a wrapper for the Windows API ChangeDisplaySettings. Below is the source code of a function that takes the desired width and height as parameters and returns the return value of ChangeDisplaySettings (see the documentation for more details).
uses Windows;
function SetScreenResolution(Width, Height: integer): Longint;
var
DeviceMode: TDeviceMode;
begin
with DeviceMode do begin
dmSize := SizeOf(TDeviceMode);
dmPelsWidth := Width;
dmPelsHeight := Height;
dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
end;
Result := ChangeDisplaySettings(DeviceMode, CDS_UPDATEREGISTRY);
end;You can use ChangeDisplaySettings to change other properties of the display like the color depth and the display frequency.
Sample call
In the following example first we get the current screen resolution before setting it to 800x600, and then we restore it calling SetScreenResolution again.
var OldWidth, OldHeight: integer; procedure TForm1.Button1Click(Sender: TObject); begin OldWidth := GetSystemMetrics(SM_CXSCREEN); OldHeight := GetSystemMetrics(SM_CYSCREEN); SetScreenResolution(800, 600); end; procedure TForm1.Button2Click(Sender: TObject); begin SetScreenResolution(OldWidth, OldHeight); end;