窗体自适应分辨率

unit untFixForm;
interface
uses
  Classes, SysUtils, Controls, Forms;
type
  TFontedControl = class(TControl)
  public
    property Font;
  end;
  TFontMapping = record
    SWidth: Integer;
    SHeight: Integer;
    FName: string;
    FSize: Integer;
  end;

procedure FixForm(AForm: TForm);
procedure SetFontMapping;
var
  FontMapping: array of TFontMapping;
implementation

procedure SetFontMapping;
begin
  SetLength(FontMapping, 3);
// 800 x 600
  FontMapping[0].SWidth := 800;
  FontMapping[0].SHeight := 600;
  FontMapping[0].FName := '宋体';
  FontMapping[0].FSize := 11;

// 1024 x 768
  FontMapping[1].SWidth := 1024;
  FontMapping[1].SHeight := 768;
  FontMapping[1].FName := '宋体';
  FontMapping[1].FSize := 14;

// 1280 x 1024
  FontMapping[2].SWidth := 1280;
  FontMapping[2].SHeight := 1024;
  FontMapping[2].FName := '宋体';
  FontMapping[2].FSize := 16;
end;

procedure FixForm(AForm: TForm);
var
  i, j: integer;
  t: TControl;
begin
  with AForm do
  begin
    for i := 0 to ComponentCount - 1 do
    begin
      try
        t := TControl(Components[i]);
        t.left := Trunc(t.left * (Screen.width / 1024));
        t.top := Trunc(t.Top * (Screen.Height / 768));
        t.Width := Trunc(t.Width * (Screen.Width / 1024));
        t.Height := Trunc(t.Height * (Screen.Height / 768));
      except
      end; { try }
    end; { for i }
    for i := 0 to Length(FontMapping) - 1 do
    begin
      if (Screen.Width = FontMapping[i].SWidth) and (Screen.Height = FontMapping[i].SHeight) then
      begin
        for j := 0 to ComponentCount - 1 do
        begin
          try
            TFontedControl(Components[j]).Font.Name := FontMapping[i].FName;
            TFontedControl(Components[j]).FONT.Size := FontMapping[i].FSize;
          except
          end; { try }
        end; { for j }
      end; { if }
    end; { for i }
  end; { with }
end;
initialization
  SetFontMapping;
end.
 { 引用
procedure TForm1.FormShow(Sender: TObject);
begin
untFixForm.FixForm(Self);
end;
 }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值