









I wrote this article as a response to answer questions about how to change screen resolution posted on Delphindo mailing list. Actually, this writing is my post in that mailing list.
Personally, changing screen resolution to the faint of heart, is not good practice because screen is shared resource. Changing screen resolution may cause other applications currently running, look ugly. That means our application is not cooperative.
TScreen has properties Width and Height which can be use to query current screen resolution. Width and Height properties is made read-only to enforce concept that in multi-tasking operating system, application should be cooperative to use shared resource.
What if two applications use their favourite screen resolution? Screen will be full with flickers due to screen resolution switching.
It is better if application adapts with screen resolution. So if screen resolution is changed, our forms will changed their size to match screen resolution if their size exceed screen resolution.
To get notified when screen resolution changed, you can handle WM_DISPLAYCHANGE message. For example
type TForm1=class(TForm)
private
...
procedure WMDisplayChange(var msg:TMessage);
message WM_DISPLAYCHANGE;
...
end;
...
procedure TForm1.WMDisplayChange(var msg:TMessage);
begin
if Width>Screen.Width then
Width:=Screen.Width;
if Height>Screen.Height then
Height:=Screen.Height;
end;
To prevent content of form looks bad, use containers such as TPanel and set its Align property to alClient, which automatically will be resized to match its parent control. For fixed size dialogs, set form size in such a way that it is small enough and will not exceed screen resolution.
But if you still want to change screen resolution, you can use ChangeDisplaySettings() declared in windows.pas unit.
To change screen resolution only,
procedure SetScreenResolution(const width,
height:integer);overload;
var mode:TDevMode;
begin
zeroMemory(@mode,sizeof(TDevMode));
mode.dmSize:=sizeof(TDevMode);
mode.dmPelsWidth:=width;
mode.dmPelsHeight:=height;
mode.dmFields:=DM_PELSWIDTH or DM_PELSHEIGHT;
ChangeDisplaySettings(mode,0);
end;
To change screen resolution and color depth,
procedure SetScreenResolution(const width,
height,colorDepth:integer);overload;
var mode:TDevMode;
begin
zeroMemory(@mode,sizeof(TDevMode));
mode.dmSize:=sizeof(TDevMode);
mode.dmPelsWidth:=width;
mode.dmPelsHeight:=height;
mode.dmBitsPerPel:=colorDepth;
mode.dmFields:=DM_PELSWIDTH or DM_PELSHEIGHT or
DM_BITSPERPEL;
ChangeDisplaySettings(mode,0);
end;
To change screen resolution to 800x600
SetScreenResolution(800,600);
to change to 1024x768 16 bits (65536 colors) screen resolution.
SetScreenResolution(1024,768,16);
Ok, hopefully this article helps you.
Do you like this article? Help this website improve by donating. Any amounts is appreciated.
Or you can help by bookmarking this page.
Bookmark this on Delicious