Delphi自适应分辨率

上源码。

unit uMyClassHelpers;

interface

Uses

SysUtils,Windows,Classes,Graphics, Controls,Forms,Dialogs,

uMySysUtils;

Const //记录设计时的屏幕分辨率

OriWidth=1366;

OriHeight=768;

Type

TfmForm=Class(TForm) //实现窗体屏幕分辨率的自动调整

Private

fScrResolutionRateW: Double;

fScrResolutionRateH: Double;

fIsFitDeviceDone: Boolean;

fPosition:Array of TRect;

procedure FitDeviceResolution;

Protected

Property IsFitDeviceDone:Boolean Read fIsFitDeviceDone;

Property ScrResolutionRateH:Double Read fScrResolutionRateH;

Property ScrResolutionRateW:Double Read fScrResolutionRateW;

Public

Constructor Create(AOwner: TComponent); Override;

End;

TfdForm=Class(TfmForm) //增加对话框窗体的修改确认

Protected

fIsDlgChange:Boolean;

Public

Constructor Create(AOwner: TComponent); Override;

Property IsDlgChange:Boolean Read fIsDlgChange default false;

End;

implementation

Constructor TfmForm.Create(AOwner: TComponent);

begin

Inherited Create(AOwner);

fScrResolutionRateH:=1;

fScrResolutionRateW:=1;

Try

if Not fIsFitDeviceDone then

Begin

FitDeviceResolution;

fIsFitDeviceDone:=True;

End;

Except

fIsFitDeviceDone:=False;

End;

end;

procedure TfmForm.FitDeviceResolution;

Var

i:Integer;

LocList:TList;

LocFontSize:Integer;

LocFont:TFont;

LocCmp:TComponent;

LocFontRate:Double;

LocRect:TRect;

LocCtl:TControl;

begin

LocList:=TList.Create;

Try

Try

if (Screen.width<>OriWidth)OR(Screen.Height<>OriHeight) then

begin

Self.Scaled:=False;

fScrResolutionRateH:=screen.height/OriHeight;

fScrResolutionRateW:=screen.Width/OriWidth;

Try

if fScrResolutionRateH<fScrResolutionRateW then

LocFontRate:=fScrResolutionRateH

Else

LocFontRate:=fScrResolutionRateW;

Finally

ReleaseDC(0, GetDc(0));

End;

For i:=Self.ComponentCount-1 Downto 0 Do

Begin

LocCmp:=Self.Components[i];

If LocCmp Is TControl Then

LocList.Add(LocCmp);

If PropertyExists(LocCmp,'FONT') Then

Begin

LocFont:=TFont(GetObjectProperty(LocCmp,'FONT'));

LocFontSize := Round(LocFontRate*LocFont.Size);

LocFont.Size:=LocFontSize;

End;

End;

SetLength(fPosition,LocList.Count+1);

For i:=0 to LocList.Count-1 Do

With TControl(LocList.Items[i])Do

fPosition[i+1]:=BoundsRect;

fPosition[0]:=Self.BoundsRect;

With LocRect Do

begin

Left:=Round(fPosition[0].Left*fScrResolutionRateW);

Right:=Round(fPosition[0].Right*fScrResolutionRateW);

Top:=Round(fPosition[0].Top*fScrResolutionRateH);

Bottom:=Round(fPosition[0].Bottom*fScrResolutionRateH);

Self.SetBounds(Left,Top,Right-Left,Bottom-Top);

end;

i:= LocList.Count-1;

While (i>=0) Do

Begin

LocCtl:=TControl(LocList.Items[i]);

If LocCtl.Align=alClient Then

begin

Dec(i);

Continue;

end;

With LocRect Do

begin

Left:=Round(fPosition[i+1].Left*fScrResolutionRateW);

Right:=Round(fPosition[i+1].Right*fScrResolutionRateW);

Top:=Round(fPosition[i+1].Top*fScrResolutionRateH);

Bottom:=Round(fPosition[i+1].Bottom*fScrResolutionRateH);

LocCtl.SetBounds(Left,Top,Right-Left,Bottom-Top);

end;

Dec(i);

End;

End;

Except on E:Exception Do

Raise Exception.Create('进行屏幕分辨率自适应调整时出现错误'+E.Message);

End;

Finally

LocList.Free;

End;

end;

{ TfdForm }

constructor TfdForm.Create(AOwner: TComponent);

begin

inherited;

fIsDlgChange:=False;

end;

end.

上面包括两个类,一个是普通窗体类,一个是其子类对话框型窗体类。在实际应用过程中只要自己创建的窗体类继承自以上两个类中的一个,例如 TForm1 = class(TfdForm),则不需添加任何源码,设计出窗体会自动调整其上控件的尺寸,以适应不同的屏幕分辨率。

转载自:https://www.cnblogs.com/FuYan/p/4972894.html 感谢“ 倾天

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi 的自适应分辨率指的是在开发 Delphi 应用程序时,能够适应不同分辨率的显示设备。这个功能非常有用,因为用户使用的显示器分辨率可能各不相同,为了让应用程序在不同的屏幕上都有良好的显示效果,我们需要进行适配。 在 Delphi 中实现自适应分辨率的方式有很多种。一种常用的方式是使用控件的 Anchors 属性和 Align 属性。Anchors 属性可以定义控件在窗口中的位置和大小的相对固定或相对变化,使其能够根据窗口尺寸的变化而自动调整位置和大小。Align 属性可以根据父控件或窗口的大小,自动调整控件的位置和大小。 另一种方式是使用 TGridPanel 控件。TGridPanel 是一个类似于表格的布局控件,可以将控件划分为多个单元格,并根据父控件或窗口的大小自动调整单元格中控件的位置和大小。 此外,Delphi 还提供了一些辅助功能,如 Screen 属性和 OnResize 事件,可以帮助我们实现自适应分辨率。通过 Screen 属性可以获取屏幕的分辨率信息,根据这些信息进行适配。OnResize 事件会在窗口大小发生改变时触发,我们可以在这个事件中调整控件的位置和大小。 总的来说,Delphi 提供了多种方式来实现应用程序的自适应分辨率,我们可以根据具体的需求选择合适的方式进行实现,以确保应用程序在不同分辨率的屏幕上都能有良好的显示效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值