自适应分辨率

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值