Author: Jonas Bilinkevicius
How to change the screen resolution programmatically
Answer:
Solve 1:
To change the screen resolution on a user's PC is bad programming style, but anyway:
1 { ... }
2 type
3 TDisplaySetting = record
4 dsWidth, dsHeight, dsBPP: Cardinal {DWORD};
5 end;
6 { ... }
7
8 function ChangeDisplayMode(var Settings: TDisplaySetting): Boolean;
9 var
10 I: Integer;
11 DC: HDC;
12 DevMode: TDevMode;
13 Current: TDisplaySetting;
14 begin
15 Result := FALSE;
16 { Enumerate available display modes }
17 I := 0;
18 while EnumDisplaySettings(nil, I, DevMode) do
19 begin
20 { Check if this is desired settings }
21 if (DevMode.dmPelsWidth = Settings.dsWidth) and (DevMode.dmPelsHeight =
22 Settings.dsHeight)
23 and (DevMode.dmBitsPerPel = Settings.dsBPP) then
24 begin
25 { Get Current Display Settings }
26 DC := GetDC(0);
27 with Current do
28 begin
29 dsWidth := GetDeviceCaps(DC, HORZRES);
30 dsHeight := GetDeviceCaps(DC, VERTRES);
31 dsBPP := GetDeviceCaps(DC, BITSPIXEL);
32 end;
33 ReleaseDC(0, DC);
34 { Indicate what settings to change }
35 DevMode.dmFields := DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT;
36 { Try to change settings }
37 if ChangeDisplaySettings(DevMode, 0) = DISP_CHANGE_SUCCESSFUL then
38 begin
39 Result := TRUE;
40 { Return previous settings if successful }
41 Settings := Current;
42 end;
43 Break;
44 end;
45 Inc(I);
46 end;
47 end;
48
49 //Used like this:
50
51 var
52 DS: TDisplaySettings;
53 begin
54 DS.dsWidth := 800;
55 DS.dsHeight := 600;
56 DS.dsBPP := 24;
57 { Change display mode }
58 if ChangeDisplayMode(DS) then
59 begin
60 { DS now stores previous settings }
61 { ... }
62 { Restore original settings }
63 ChangeDisplayMode(DS);
64 end;
65 end;
Solve 2:
Here's a method I built to force 1024x768:
66 function SetDisplay1024x768: Boolean;
67 var
68 DevMode: TDeviceMode;
69 begin
70 EnumDisplaySettings(nil, 0, DevMode);
71 DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
72 DevMode.dmPelsWidth := 1024;
73 DevMode.dmPelsHeight := 768;
74 Result := ChangeDisplaySettings(DevMode, CDS_UPDATEREGISTRY)
75 = DISP_CHANGE_SUCCESSFUL;
76 end;
Answer 3:
77
78 function ChangeRes(X, Y, Frequency, ColorDepth: Cardinal): boolean;
79 var
80 RetCode: Integer;
81 MyDevMode: TDeviceMode;
82 begin
83 EnumDisplaySettings(nil, 0, MyDevMode);
84 MyDevMode.dmDisplayFrequency := Frequency;
85 MyDevMode.dmPelsWidth := X;
86 MyDevMode.dmPelsHeight := Y;
87 MyDevMode.dmBitsPerPel := ColorDepth;
88 MyDevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL
89 or DM_DISPLAYFREQUENCY;
90 RetCode := ChangeDisplaySettings(MyDevMode, CDS_UPDATEREGISTRY);
91 case RetCode of
92 DISP_CHANGE_SUCCESSFUL, DISP_CHANGE_RESTART: Result := true;
93 else
94 Result := false;
95 end;
96 case RetCode of
97 DISP_CHANGE_SUCCESSFUL:
98 Writeln('DISP_CHANGE_SUCCESSFUL');
99 DISP_CHANGE_RESTART:
100 Writeln('DISP_CHANGE_RESTART');
101 DISP_CHANGE_FAILED:
102 Writeln('DISP_CHANGE_FAILED');
103 DISP_CHANGE_BADMODE:
104 Writeln('DISP_CHANGE_BADMODE');
105 DISP_CHANGE_NOTUPDATED:
106 Writeln('DISP_CHANGE_NOTUPDATED');
107 DISP_CHANGE_BADFLAGS:
108 Writeln('DISP_CHANGE_BADFLAGS');
109 DISP_CHANGE_BADPARAM:
110 Writeln('DISP_CHANGE_BADPARAM');
111 else
112 Writeln('DISP_CHANGE_OTHER_ERROR');
113 end;
114 end;
|