Delphi窗体自适应屏幕分辨率
//注意: AForm界面控件的ParentFont要False
//AForm-传入的窗体 OrgWidth,orgHeight-软件设计时的分辨率宽*高
//如果窗体里有特殊控件,请在下面代码【特殊控件1】、【特殊控件2】处修改函数即可
procedure AutoFixForm(AForm: TForm; OrgWidth,orgHeight: Integer);
var
i,j: integer;
t: TControl;
f1,f2: Double;
begin
f1 := Screen.width / orgWidth;
f2 := Screen.Height / orgHeight;
with AForm do
begin
Width := Trunc(Width * f1);
Height := Trunc(Height * f2);
for i := 0 to ComponentCount - 1 do
begin
//特殊控件1:不可见控件处理
if (Components[i] is TADOQuery)
or (Components[i] is TDataSource)
or (Components[i] is TTimer)
or (Components[i] is TMainMenu)
or (Components[i] is TMenuItem)
or (Components[i] is TOpenDialog)
then
begin
Continue;
end;
try
t := TControl(Components[i]);
t.left := Trunc(t.left * f1);
t.top := Trunc(t.Top * f2);
t.Width := Trunc(t.Width * f1);
t.Height := Trunc(t.Height * f2);
//特殊控件2:Font不直接调用的控件处理
if Components[i] is TChart then
begin
(Components[i] as TChart).Title.Font.Size := Trunc((Components[i] as TChart).Title.Font.Size * f1);
end else
if Components[i] is TChartSeries then
begin
(Components[i] as TChartSeries).Marks.Font.Size := Trunc((Components[i] as TChartSeries).Marks.Font.Size * f1);
end else
if Components[i] is TDBGridEh then
begin
(Components[i] as TDBGridEh).Font.Size := Trunc((Components[i] as TDBGridEh).Font.Size * f1);
for j := 0 to (Components[i] as TDBGridEh).Columns.Count - 1 do
(Components[i] as TDBGridEh).Columns[j].Width := Trunc((Components[i] as TDBGridEh).Columns[j].Width * f1);
end else
begin
TFontedControl(t).Font.Size := Trunc(TFontedControl(t).Font.Size * f1);
end;
except
end;
end;
end;
end;
调用例子:
procedure TForm1.FormCreate(Sender: TObject);
begin
AutoFixForm(Self, 800, 600); //Self表示为本窗体、800和600为页面设计时的初始分辨率
end;
//注意: AForm界面控件的ParentFont要False
//AForm-传入的窗体 OrgWidth,orgHeight-软件设计时的分辨率宽*高
//如果窗体里有特殊控件,请在下面代码【特殊控件1】、【特殊控件2】处修改函数即可
procedure AutoFixForm(AForm: TForm; OrgWidth,orgHeight: Integer);
var
i,j: integer;
t: TControl;
f1,f2: Double;
begin
f1 := Screen.width / orgWidth;
f2 := Screen.Height / orgHeight;
with AForm do
begin
Width := Trunc(Width * f1);
Height := Trunc(Height * f2);
for i := 0 to ComponentCount - 1 do
begin
//特殊控件1:不可见控件处理
if (Components[i] is TADOQuery)
or (Components[i] is TDataSource)
or (Components[i] is TTimer)
or (Components[i] is TMainMenu)
or (Components[i] is TMenuItem)
or (Components[i] is TOpenDialog)
then
begin
Continue;
end;
try
t := TControl(Components[i]);
t.left := Trunc(t.left * f1);
t.top := Trunc(t.Top * f2);
t.Width := Trunc(t.Width * f1);
t.Height := Trunc(t.Height * f2);
//特殊控件2:Font不直接调用的控件处理
if Components[i] is TChart then
begin
(Components[i] as TChart).Title.Font.Size := Trunc((Components[i] as TChart).Title.Font.Size * f1);
end else
if Components[i] is TChartSeries then
begin
(Components[i] as TChartSeries).Marks.Font.Size := Trunc((Components[i] as TChartSeries).Marks.Font.Size * f1);
end else
if Components[i] is TDBGridEh then
begin
(Components[i] as TDBGridEh).Font.Size := Trunc((Components[i] as TDBGridEh).Font.Size * f1);
for j := 0 to (Components[i] as TDBGridEh).Columns.Count - 1 do
(Components[i] as TDBGridEh).Columns[j].Width := Trunc((Components[i] as TDBGridEh).Columns[j].Width * f1);
end else
begin
TFontedControl(t).Font.Size := Trunc(TFontedControl(t).Font.Size * f1);
end;
except
end;
end;
end;
end;
调用例子:
procedure TForm1.FormCreate(Sender: TObject);
begin
AutoFixForm(Self, 800, 600); //Self表示为本窗体、800和600为页面设计时的初始分辨率
end;