博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
让窗体自适应屏幕
阅读量:5165 次
发布时间:2019-06-13

本文共 3231 字,大约阅读时间需要 10 分钟。

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.

转载于:https://www.cnblogs.com/onionhacker/p/3717616.html

你可能感兴趣的文章
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
JavaScript---Promise
查看>>
暖暖的感动
查看>>
Java中的日期和时间
查看>>