Auto-resizing and centring forms on screen #67
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.
Note – form should be set to autoresize and
AutoScroll := False
at least while being re-calculated. Also
the font size for the form should be (probably) 12 for the font to be resized
downwards for 800×600 – all controls should be set to
ParentFont := True
so they will resize.
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: centreleft and centretop. 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;
Author: | Alan Bailey |
---|---|
Contributor: | Alan Bailey |
Added: | 2008/10/17 |
Last updated: | 2008/10/17 |