System Delphi

Title: Resolution
Question: Changing Display Settings in code
Answer:
*****
1. Changing Display Settings in code
*****
*****
1.2 Get
*****
We can obtain information for all of display device's graphics modes by making a series of calls to EnumDisplaySettings function.
EnumDisplaySettings finds the number of possible modes by enumerating them until the function result is no longer True.
This function requires the TDevMode type variable in which to place the settings. The TDevMode type has a number of variables that refer to display devices (among others). These include the display device resolution in pixels (dmPelsWidth, dmPelsHeight), the color depth (in bits per pixel) supported at the resolution (dmBitsPerPel), refresh rate (dmDisplayFrequency) and other.
*****
procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
DevMode : TDevMode;
begin
i:=0;
while EnumDisplaySettings(nil,i,DevMode) do begin
with Devmode do
ListBox1.Items.Add
(Format('%dx%d %d Colors',
[dmPelsWidth,dmPelsHeight,1 shl dmBitsperPel]));
Inc(i);
end;
end;
*****
1.3 Set
*****
Once we have all the possible display modes, setting the desired-appropriate display mode is very simple. The ChangeDisplaySettings function is then used to change the current display mode and update the Windows Registry as necessary.
*****
procedure TForm1.Button1Click(Sender: TObject);
var
DevMode : TDeviceMode;
liRetValue : Longint;
begin
if EnumDisplaySettings
(nil,Listbox1.ItemIndex,Devmode) then
liRetValue := ChangeDisplaySettings(DevMode, 0);
end;
*****
The ChangeDisplaySettings function returns the long integer value. This value can be compared to a list of constants to determine whether the function was successful or not.
Note 1: It is not recommended that we set the display modes individually, rather than choosing from the Enumerated list. This will prevent us from bringing the blank screen in front of the user. For example: setting the dmBitsPerPel variable to a color depth unsupported by the resolution could blank the screen.
Note 2: Many drivers (especially older ones) will not perform the change without rebooting the computer, even though they are capable of doing so.
*****
1.4 Detecting changes in display
*****
To detect changes in the display, we'll create a message handler to trap the WM_DISPLAYCHANGE message. If your application makes use of graphics, you may want to restart the application, as there may be many changes to the system that may be difficult to overcome as the screen resolution, color depth and default fonts may change.
*****
...
type
TForm1 = class(TForm)
ListBox1: TListBox;
...
private
procedure WMDisplayChange(var Message:TMessage);
message WM_DISPLAYCHANGE;
...
procedure
TForm1.WMDisplayChange(var Message: TMessage);
begin
ShowMessage('Changes in display detected!');
inherited;
end;