实现窗体自动适应屏幕分辨率

需要一个数组事先保存所有控件的原始位置和尺寸。

在窗体因为屏幕分辨率的改变而自动调整时,计算的依据依然是不变的原始窗体位置尺寸数据。

//图片设置

Proportional //如果此属性设置为True,图像的尺寸将按原来长宽的比例自动调整

Stretch //如果此属性设置为True, 图像的尺寸将自动调整并且总是撑满整个TImage 组件
//目的:实现窗体屏幕分辨率的自动调整
var
  OriWidth, OriHeight: Integer; //记录设计时的屏幕分辨率
  ScrWidthRate, ScrHeightRate, LocFontRate: Double; //计算尺度调整的基本参数
begin
  OriWidth := 0;
  OriHeight := 0;
  ScrWidthRate := 1.0;
  ScrHeightRate := 1.0;
  sSql := 'select * from DiyControl where Form=''frmOperationVerifyShow'' AND ControlName=''Panel1'' ';
  if not CommonDB.GetDataEx(sSql, dmCommon.cdsCommon) then Exit;
  if not dmCommon.cdsCommon.IsEmpty then
  begin
    OriWidth := dmCommon.cdsCommon.FieldByName('Posleft').AsInteger + dmCommon.cdsCommon.FieldByName('PosWidth').AsInteger;
    OriHeight := dmCommon.cdsCommon.FieldByName('Posheight').AsInteger;
  end;
  ScrWidthRate := Screen.Width / OriWidth;
  ScrHeightRate := Screen.Height / OriHeight;
  if Abs(ScrHeightRate) < Abs(ScrWidthRate) then
    LocFontRate := ScrHeightRate
  else
    LocFontRate := ScrWidthRate;
end;
{计算新的坐标位置:利用递归法遍历各级容器里的控件,直到最后一层。计算坐标时先计算顶级容器级的,然后逐级递进}
procedure TCommonDB.SetControlProperties(AComponent:TComponent; qryControl:TADOQuery; ConnStr:string);
//设置控件属性
var
  tHint, tControlType:string;
begin
  with qryControl do
  begin
    if (AComponent<>nil)
    and (AnsiPos(CSNoDesign, UpperCase(AComponent.Name))=0) then
    begin
      if Assigned(GetPropInfo(AComponent,'Enabled')) then //判断一个属性是否存在
        SetPropValue(AComponent, 'Enabled', True); //保证后面的设置能成功
      // Left
      if not fieldbyname('PosLeft').IsNull then
        if Assigned(GetPropInfo(AComponent,'Left')) then
          SetPropValue(AComponent,'Left',fieldbyname('PosLeft').AsInteger * ScrWidthRate);
      // Top
      if not fieldbyname('PosTop').IsNull then
        if Assigned(GetPropInfo(AComponent,'Top')) then
          SetPropValue(AComponent,'Top',fieldbyname('PosTop').AsInteger * ScrHeightRate);
      // 高度
      if not fieldbyname('PosHeight').IsNull then
        if Assigned(GetPropInfo(AComponent,'Height')) then
          SetPropValue(AComponent,'Height', fieldbyname('PosHeight').AsInteger * ScrHeightRate);
      // 宽度
      if not fieldbyname('PosWidth').IsNull then
        if Assigned(GetPropInfo(AComponent,'Width')) then
          SetPropValue(AComponent,'Width', fieldbyname('PosWidth').AsInteger * ScrWidthRate);
       // 可见
      if not fieldbyname('Visible').IsNull then
        if Assigned(GetPropInfo(AComponent,'Visible')) then
          SetPropValue(AComponent,'Visible', fieldbyname('Visible').AsBoolean);
      // 字体
      if Assigned(GetPropInfo(AComponent,'Font')) then
      begin
        try
          if AComponent is TLabel then
          begin
            with TLabel(AComponent) do
            begin
              Font.Name:=fieldbyname('FontName').AsString;
              Font.Size:=Round(fieldbyname('FontSize').AsInteger * LocFontRate);
              Font.Color:=fieldbyname('FontColor').AsInteger;
              Font.Style:=GetFontStyle(fieldbyname('FontStyle').AsInteger);
            end;
          end;
          if AComponent is TPanel then
          begin
            with TPanel(AComponent) do
            begin
              Font.Name:=fieldbyname('FontName').AsString;
              Font.Size:=Round(fieldbyname('FontSize').AsInteger * LocFontRate);
              Font.Color:=fieldbyname('FontColor').AsInteger;
              Font.Style:=GetFontStyle(fieldbyname('FontStyle').AsInteger);
            end;
          end;

        except
        end;
      end;

    end;
  end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值