This example shows how to dynamically modify the resolution of the screen in a program, and it takes effect without restarting the computer.
Add two button controls to the form, and the finished main interface is shown in Figure 1.
Figure 1 Main interface
Add a custom function dynamicresolution to the program, which has two parameters, corresponding to the horizontal and vertical resolution, and the function has a bool-type return value that can be judged by the return value to determine whether the Dynamicresolution function is properly executed. The Dynamicresolution function code is as follows:
function TfrmMain.DynamicResolution(X, Y: word): BOOL;
var
lpDevMode: TDeviceMode;
begin
Result := EnumDisplaySettings(nil, 0, lpDevMode);
if Result then
begin
lpDevMode.dmFields := DM_PELSWIDTH Or DM_PELSHEIGHT;
lpDevMode.dmPelsWidth := X;
lpDevMode.dmPelsHeight := Y;
Result:=ChangeDisplaySettings(lpDevMode,0)= DISP_CHANGE_SUCCESSFUL;
end;
end;
While the program is running, call Dynamicresolution (640,480) or dynamicresolution (800,600) to modify the resolution by clicking the two buttons "640*480" and "800*600" on the form.
The program first obtains the current screen resolution by enumdisplaysettings, stores it in the variable lpdevmode, and then sets the Lpdevmode to the new resolution. Finally, through the ChangeDisplaySettings function to complete the dynamic modification of the display resolution of the specific operation.