根据布局尺寸调整控件大小?
Resize controls based on Layout dimensions?
好的,我需要根据布局尺寸调整控件的大小。我还需要它在布局尺寸发生变化时调整大小(例如,将设备从配置文件变为横向)
我假设您只需检查设备的尺寸并进行相应调整
例如
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
但是,把它放在哪里最好?
我在布局调整大小之前和布局调整大小之后都在调整大小方法中尝试过。好像不行!
好吧,它在表单的初始激活时起作用,但在调整表单大小时却不起作用。在 IDE 中,我什至必须按下重新加载按钮才能使其正常工作。
下面是一个在表单上包含两个组件的示例。 TW3HeaderControl 与表单顶部对齐,TW3ListBox 与客户端对齐。
我想根据 TW3HeaderControl 是在 Profile 还是 Landscape 中调整它的高度
例如
unit Form1;
interface
uses
SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Layout,
SmartCL.Controls;
type
TForm1 = class(TW3Form)
private
{$I 'Form1:intf'}
fLayout: TLayout;
fHeader: TW3HeaderControl;
fList: TW3ListBox;
protected
procedure InitializeForm; override;
procedure InitializeObject; override;
procedure Resize; override;
end;
implementation
{ TForm1 }
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader), Layout.Client(fList)]);
end;
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fList:= TW3ListBox.Create(self);
end;
procedure TForm1.Resize;
begin
inherited;
if assigned(FLayout) then
begin
fLayout.Resize(self);
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
end;
end;
initialization
Forms.RegisterForm({$I %FILE%}, TForm1);
end.
我什至尝试覆盖 "FormActivated" 并将其放置在那里并调用调整大小。
什么给了?
更新!!!!!
而不是在 InitializeForm 中分配 Layout
例如
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)]);
end;
改为在 Resize 方法中分配它??
例如
procedure TForm1.Resize;
begin
inherited;
if ClientWidth > ClientHeight then
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)])
else
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 8),fHeader), Layout.Client(fList)]);
fLayout.Resize(self);
end;
更新#2
而且,如果我在表单上添加另一个控件,则会出现另一个问题:(
如果我将大小设置为静态值,效果很好
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fHeader.Height:= 50; //******************************
fHeader.BackButton.Visible:= False;
fHeader.Title.Caption:= 'Menu';
fHeader.Title.AlignText:= taCenter;
fFooter:= TW3HeaderControl.Create(self);
fFooter.Height:= 50; //******************************
fFooter.BackButton.Visible:= False;
fFooter.Title.Caption:= 'Copyright (C) 2016';
fFooter.Title.AlignText:= taCenter;
fList:= TW3ListBox.Create(self);
end;
这有效
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader),
Layout.Bottom(fFooter),
Layout.Client(fList)]);
end;
但是,如果我在运行时根据尺寸动态设置高度,则不会
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6), fHeader),
Layout.Bottom(Layout.Height(ClientHeight Div 6),fFooter),
Layout.Client(fList)]);
end;
这个有效
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader),
Layout.Bottom(fFooter),
Layout.Client(fList)]);
end;
浏览器强加给我们的一些规则,从 object pascal 的角度来看似乎很奇怪。 InitializeForm 与 InitializeObject 之间的区别就是其中之一。与 "forms".
的怪异之处一样
正如 John 所指出的,InitializeForm 是个好地方。该方法在构造 JS 对象并且 DOM 元素已注入对象模型后调用。这两个不是一回事,这可能有点令人困惑。此外,如果元素是在我们调用 createElement() 时或构造函数完成后创建的,则由浏览器决定。因此我们必须引入几个新过程来处理这个问题。
您可能想更深入地研究一下 TApplication。
您会发现以下内容:TApplication -> Display -> View。表单在 "view" 容器内创建。每当方向或表单大小发生变化时,最快的通知方式是通过 Application.Display.View.OnReSize() 事件。
好的,我需要根据布局尺寸调整控件的大小。我还需要它在布局尺寸发生变化时调整大小(例如,将设备从配置文件变为横向)
我假设您只需检查设备的尺寸并进行相应调整
例如
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
但是,把它放在哪里最好?
我在布局调整大小之前和布局调整大小之后都在调整大小方法中尝试过。好像不行!
好吧,它在表单的初始激活时起作用,但在调整表单大小时却不起作用。在 IDE 中,我什至必须按下重新加载按钮才能使其正常工作。
下面是一个在表单上包含两个组件的示例。 TW3HeaderControl 与表单顶部对齐,TW3ListBox 与客户端对齐。 我想根据 TW3HeaderControl 是在 Profile 还是 Landscape 中调整它的高度
例如
unit Form1;
interface
uses
SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Layout,
SmartCL.Controls;
type
TForm1 = class(TW3Form)
private
{$I 'Form1:intf'}
fLayout: TLayout;
fHeader: TW3HeaderControl;
fList: TW3ListBox;
protected
procedure InitializeForm; override;
procedure InitializeObject; override;
procedure Resize; override;
end;
implementation
{ TForm1 }
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader), Layout.Client(fList)]);
end;
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fList:= TW3ListBox.Create(self);
end;
procedure TForm1.Resize;
begin
inherited;
if assigned(FLayout) then
begin
fLayout.Resize(self);
if ClientHeight > ClientWidth then
fHeader.Height:= ClientHeight Div 6
else
fHeader.Height:= ClientHeight Div 8;
end;
end;
initialization
Forms.RegisterForm({$I %FILE%}, TForm1);
end.
我什至尝试覆盖 "FormActivated" 并将其放置在那里并调用调整大小。
什么给了?
更新!!!!!
而不是在 InitializeForm 中分配 Layout
例如
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)]);
end;
改为在 Resize 方法中分配它??
例如
procedure TForm1.Resize;
begin
inherited;
if ClientWidth > ClientHeight then
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)])
else
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 8),fHeader), Layout.Client(fList)]);
fLayout.Resize(self);
end;
更新#2
而且,如果我在表单上添加另一个控件,则会出现另一个问题:(
如果我将大小设置为静态值,效果很好
procedure TForm1.InitializeObject;
begin
inherited;
{$I 'Form1:impl'}
fHeader:= TW3HeaderControl.Create(self);
fHeader.Height:= 50; //******************************
fHeader.BackButton.Visible:= False;
fHeader.Title.Caption:= 'Menu';
fHeader.Title.AlignText:= taCenter;
fFooter:= TW3HeaderControl.Create(self);
fFooter.Height:= 50; //******************************
fFooter.BackButton.Visible:= False;
fFooter.Title.Caption:= 'Copyright (C) 2016';
fFooter.Title.AlignText:= taCenter;
fList:= TW3ListBox.Create(self);
end;
这有效
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader),
Layout.Bottom(fFooter),
Layout.Client(fList)]);
end;
但是,如果我在运行时根据尺寸动态设置高度,则不会
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6), fHeader),
Layout.Bottom(Layout.Height(ClientHeight Div 6),fFooter),
Layout.Client(fList)]);
end;
这个有效
procedure TForm1.InitializeForm;
begin
inherited;
// this is a good place to initialize components
fLayout:= Layout.Client([Layout.Top(fHeader),
Layout.Bottom(fFooter),
Layout.Client(fList)]);
end;
浏览器强加给我们的一些规则,从 object pascal 的角度来看似乎很奇怪。 InitializeForm 与 InitializeObject 之间的区别就是其中之一。与 "forms".
的怪异之处一样正如 John 所指出的,InitializeForm 是个好地方。该方法在构造 JS 对象并且 DOM 元素已注入对象模型后调用。这两个不是一回事,这可能有点令人困惑。此外,如果元素是在我们调用 createElement() 时或构造函数完成后创建的,则由浏览器决定。因此我们必须引入几个新过程来处理这个问题。
您可能想更深入地研究一下 TApplication。 您会发现以下内容:TApplication -> Display -> View。表单在 "view" 容器内创建。每当方向或表单大小发生变化时,最快的通知方式是通过 Application.Display.View.OnReSize() 事件。