Author: Jorge Abel Ayala Marentes
Have you ever wondered how to display your forms and components always the same
size no matter what the screen resolution is?
Answer:
You have to modify your projectīs DPR file to acomplish that:
1
2 // Modify your projects code to achieve resolution independent
3 // applications. I have found that this trick only works for
4 // resolutions greater than the resolution you design the forms, for
5 // example if you design at 800x600 you can use your application at
6 // a resolution of 1024 x 768 or greater.
7 // However it wonīt run fine at 640x480 (altough, nowadays I donīt
8 // kwonw many people with thatv resolution, so I think 800X600 is
9 // fine)
10
11 program TestResolution;
12
13 uses
14 Forms,
15 Dialogs,
16 Classes,
17 TypInfo,
18 Windows,
19 Unit1 in 'Unit1.pas' {Form1},
20 Unit2 in 'Unit2.pas' {Form2};
21
22 {$R *.RES}
23
24 const
25 //If form is designed in 800 x 600 mode
26 ScreenWidth: LongInt = 800;
27 ScreenHeight: LongInt = 600;
28
29 var
30 vi_Counter1, vi_Counter2: Integer;
31 NewFormWidth: Integer;
32
33 begin
34 Application.Initialize;
35 Application.CreateForm(TForm1, Form1);
36 Application.CreateForm(TForm2, Form2);
37 NewFormWidth := GetSystemMetrics(0);
38
39 with Application do
40 for vi_Counter1 := 0 to ComponentCount - 1 do
41 begin
42 //Find all the Auto-create forms
43 if Components[vi_Counter1] is TForm then
44 with (Components[vi_Counter1] as TForm) do
45 begin
46 Scaled := True;
47 if screen.Width <> ScreenWidth then
48 begin
49 Height := longint(Height) * longint(screen.Height) div
50 ScreenHeight;
51 Width := longint(Width) * longint(screen.Width) div
52 ScreenWidth;
53 ScaleBy(screen.Width, ScreenWidth);
54
55 //Now Scale the Formīs componentīs Font
56 for vi_Counter2 := 0 to ControlCount - 1 do
57 with Components[vi_Counter2] do
58 //Use RTTI information to find for a Font property
59 if GetPropInfo(ClassInfo, 'font') <> nil then
60 Font.Size := (NewFormWidth div ScreenWidth)
61 * font.Size;
62 end;
63 end;
64 end;
65 Application.Run;
66 end.
finally some aditional considerations:
You will have to scale every form create on the fly.
Use only TrueType fonts, not Bitmapped fonts to avoid problems.
Donīt set the Formīs Position property to poDesigned, when scalling it could be off
the screen.
Don't change the PixelsPerInch property of the form.
Don't crowd controls on the form - leave at least 4 pixels between controls, so that a one pixel change in border locations (due to scaling) won't show up as ugly overlapping controls.
|