Delphi Coding - Delphi Automatic Resizing and Centering Forms
While this works exactly for the old screen resolutions which were all more or less in proportion there is nothing which will resize screens properly for the newer stretched screens, eg. for 1280×960 and 1280×1024. The min ratio calculated is scrx / screenx = 1 so the form stays with original measurements.
Calling this procedure from the form's OnActivate event will resize the form in proportion to the screen resolution – I do my programming on resolution 1280×768 hence the constants – once the form height and width has been calculated it can then be centred.
While this works exactly for the old screen resolutions which were all more or less in proportion there is nothing which will resize screens properly for the newer stretched screens, eg. for 1280×960 and 1280×1024. The min ratio calculated is scrx / screenx = 1 so the form stays with original measurements.
uses math,windows; const screenx = 1280 ; screeny = 768 ; procedure tform1.autosizeall ; var scrx,scry, k:integer ; ratio:double; begin scrx:= GetSystemMetrics(SM_CXSCREEN); {finds screen resolution x value} scry:= (GetSystemMetrics(SM_CYSCREEN)); {finds screen resolution y value} ratio:=min(scrx/screenx,scry/screeny); {takes the smaller ratio and makes sure you dont make the window too big for the screen} scaleby(trunc(ratio* 100 ), 100 ); {scales all controls and attempts to place them in the correct position} {to centre the form on the screen} form1.Left:=centreleft(form1.width); form1.Top:=centretop(form1.Height) ; end ;
This code refers to two functions: scaling and centering. These are defined as follows:
function centreleft(fw:integer):integer; {calculates the form.left} var smcx:integer; begin smcx:=GetSystemMetrics(SM_CXSCREEN); centreleft:=(smcx-fw) div 2 ; end ; function centretop(fh:integer):integer; {calculates the form.top} var smcy:integer; begin smcy:=GetSystemMetrics(SM_CYSCREEN); centtop:=(smcy-fh) div 2 ; end ;
The program is resized according to the user's screen resolution.
You can access Delphi sample projects and source codes here.
You can comment on the topics you are curious about and what you want to ask. Thanks.