Delphi 让窗体自适应屏幕显示


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Typinfo, Vcl.StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private


{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function PropertyExists(const AObject: TObject; const APropName: string): Boolean;
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AObject.ClassInfo, APropName);
Result := Assigned(PropInfo);
end;

function GetObjectProperty(
const AObject: TObject;
const APropName: string
): TObject;
var
PropInfo: PPropInfo;
begin
Result := nil;
PropInfo := GetPropInfo(AObject.ClassInfo, APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkClass) then
Result := GetObjectProp(AObject, PropInfo);
end;

procedure FitDeviceResolution(Sender: TForm);
const
OriWidth = 1440;
OriHeight = 900;
var
i: Integer;
j: Integer;
LocAnchors: array of TAnchors;
LocAlign: array of TAlign;
LocList: TList;
LocFontSize: Integer;
LocFont: TFont;
LocCmp: TComponent;
ScrResolutionRateH, ScrResolutionRateW,LocFontRate: Double;
begin
ScrResolutionRateH := Screen.Height / OriHeight;
ScrResolutionRateW := Screen.Width / OriWidth;
if Abs(ScrResolutionRateH - 1) < Abs(ScrResolutionRateW - 1) then
LocFontRate := ScrResolutionRateH
else
LocFontRate := ScrResolutionRateW;
LocList := TList.Create;
try
try
if (screen.width <> OriWidth) or (screen.Height <> OriHeight) then
begin
Sender.Scaled := False;
for i := Sender.ComponentCount - 1 downto 0 do
begin
LocCmp := Sender.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(LocAnchors, LocList.Count);
SetLength(LocAlign, LocList.Count);
for i := 0 to LocList.Count - 1 do
with TControl(LocList.Items[i]) do
begin
LocAnchors[i] := Anchors;
LocAlign[i] := Align;
Align := alNone;
Anchors := [akLeft, akTop];
end;
Sender.Top := Round(Sender.Top * ScrResolutionRateH);
Sender.Left := Round(Sender.Left * ScrResolutionRateW);
Sender.Height := Round(Sender.Height * ScrResolutionRateH);
Sender.Width := Round(Sender.Width * ScrResolutionRateW);
Sender.Font.size := Round(LocFontRate * Sender.Font.size);
for i := 0 to LocList.Count - 1 do
begin
with TControl(LocList.Items[i]) do
begin
Top := Round(Top * ScrResolutionRateH);
Left := Round(Left * ScrResolutionRateW);
Height := Round(height * ScrResolutionRateH);
Width := Round(width * ScrResolutionRateW);
end;
end;
for i := 0 to LocList.Count - 1 do
TControl(LocList.Items[i]).Align := LocAlign[i];
for i := 0 to LocList.Count - 1 do
TControl(LocList.Items[i]).Anchors := LocAnchors[i];
end;
except
MessageDlg(LocCMP.Name, mtInformation, [mbOK], 0);
end;
finally
LocList.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FitDeviceResolution(Self);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FitDeviceResolution(Self);
end;

end.

相关阅读 >>

windows 编程 窗体生成的过程

Delphi 数据类型cardinal 怎么转换成 string?

Delphi源码一个对字符加密解密的实例

Delphi 制作一个内网传播的程序

Delphi 压缩与修复access数据库

Delphi xe6通过wifiapi得到wifi信息

Delphi for 循环输出 a-z

Delphi dcc32.exe 参数说明

Delphi xe berlin readprocessmemory writeprocessmemory

Delphi通过窗口标题结束指定进程函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...