TPanel.AutoSize 不适用于特殊配置的嵌套面板
TPanel.AutoSize does not work with nested panels in a special configuration
在Delphi10.4.2 win-32 VCL Application Windows10中,我使用了几个嵌套TPanel
:
将这些控件粘贴到空窗体上:
object Panel1: TPanel
Left = 0
Top = 0
Width = 494
Height = 299
Align = alClient
BevelOuter = bvNone
TabOrder = 0
ExplicitLeft = 40
ExplicitTop = 140
ExplicitWidth = 185
ExplicitHeight = 41
object Splitter1: TSplitter
Left = 222
Top = 0
Height = 299
Align = alRight
Color = clSilver
ParentColor = False
ExplicitLeft = 280
ExplicitTop = 112
ExplicitHeight = 100
end
object CardPanel1: TCardPanel
Left = 225
Top = 0
Width = 269
Height = 299
Align = alRight
ActiveCard = Card1
BevelOuter = bvNone
Caption = 'CardPanel1'
Ctl3D = False
ParentCtl3D = False
TabOrder = 0
ExplicitLeft = 272
ExplicitHeight = 282
object Card1: TCard
Left = 0
Top = 0
Width = 269
Height = 299
Caption = 'Card1'
CardIndex = 0
TabOrder = 0
ExplicitLeft = 1
ExplicitTop = 1
ExplicitWidth = 298
ExplicitHeight = 280
object pnlBackSide: TPanel
AlignWithMargins = True
Left = 10
Top = 10
Width = 249
Height = 263
Margins.Left = 10
Margins.Top = 10
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
AutoSize = True
Color = clBlue
ParentBackground = False
TabOrder = 0
object pnlFront: TPanel
AlignWithMargins = True
Left = 2
Top = 2
Width = 245
Height = 259
Margins.Left = 1
Margins.Top = 1
Margins.Right = 1
Margins.Bottom = 1
Align = alClient
AutoSize = True
BevelOuter = bvNone
ParentBackground = False
TabOrder = 0
ExplicitWidth = 274
ExplicitHeight = 243
object lblBlaBla: TLabel
AlignWithMargins = True
Left = 3
Top = 3
Width = 239
Height = 13
Align = alTop
Alignment = taCenter
Caption = 'BLA BLA'
ExplicitWidth = 39
end
object listboxTest: TListBox
AlignWithMargins = True
Left = 10
Top = 19
Width = 225
Height = 55
Margins.Left = 10
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
BevelInner = bvNone
ItemHeight = 13
TabOrder = 0
ExplicitWidth = 254
end
object Button1: TButton
AlignWithMargins = True
Left = 10
Top = 78
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button1'
TabOrder = 1
ExplicitWidth = 254
end
object Button3: TButton
AlignWithMargins = True
Left = 10
Top = 136
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button3'
TabOrder = 2
ExplicitWidth = 254
end
object Button4: TButton
AlignWithMargins = True
Left = 10
Top = 165
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button4'
TabOrder = 3
ExplicitWidth = 254
end
object Button2: TButton
AlignWithMargins = True
Left = 10
Top = 107
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button2'
TabOrder = 4
ExplicitWidth = 254
end
object pnlComboBack: TPanel
AlignWithMargins = True
Left = 10
Top = 194
Width = 225
Height = 21
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 4
Align = alTop
AutoSize = True
BevelOuter = bvNone
TabOrder = 5
ExplicitWidth = 254
object cbbTest: TComboBox
Left = 0
Top = 0
Width = 225
Height = 21
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 4
Align = alClient
Style = csDropDownList
ItemIndex = 0
TabOrder = 0
Text = 'Select an item'
Items.Strings = (
'Select an item')
ExplicitWidth = 254
end
end
end
end
end
end
end
然后 运行 编译的应用程序,你会得到这样的东西:
您可以看到面板 pnlBackSide
没有自动调整大小,尽管其 属性 AutoSize
设置为 True
。为什么?
如何使面板 pnlBackSide
自动调整大小?
正如 Andreas 正确观察到的,属性 pnlFront.Align = alClient
和 pnlFront.AutoSize = True
是一个逻辑困境(因为它们是冲突的)因此不能同时执行。因此,解决方案是通过在设计时将它们设置为 pnlFront.Align = alNone
和 pnlFront.AutoSize = False
来 连续地 执行它们。然后在运行时,连续执行它们:
procedure TForm1.FormCreate(Sender: TObject);
begin
pnlFront.AutoSize := True;
pnlFront.Align := alClient;
end;
此外,为了补偿 pnlComboBack.Margins.Bottom
,还必须在设计时将 pnlBackSide.AutoSize
设置为 False
,然后在 运行 时设置为 True
-时间(按正确顺序):
procedure TForm1.FormCreate(Sender: TObject);
begin
pnlFront.AutoSize := True;
pnlBackSide.AutoSize := True;
pnlFront.Align := alClient;
end;
所以现在的结果是完美的:
在Delphi10.4.2 win-32 VCL Application Windows10中,我使用了几个嵌套TPanel
:
将这些控件粘贴到空窗体上:
object Panel1: TPanel
Left = 0
Top = 0
Width = 494
Height = 299
Align = alClient
BevelOuter = bvNone
TabOrder = 0
ExplicitLeft = 40
ExplicitTop = 140
ExplicitWidth = 185
ExplicitHeight = 41
object Splitter1: TSplitter
Left = 222
Top = 0
Height = 299
Align = alRight
Color = clSilver
ParentColor = False
ExplicitLeft = 280
ExplicitTop = 112
ExplicitHeight = 100
end
object CardPanel1: TCardPanel
Left = 225
Top = 0
Width = 269
Height = 299
Align = alRight
ActiveCard = Card1
BevelOuter = bvNone
Caption = 'CardPanel1'
Ctl3D = False
ParentCtl3D = False
TabOrder = 0
ExplicitLeft = 272
ExplicitHeight = 282
object Card1: TCard
Left = 0
Top = 0
Width = 269
Height = 299
Caption = 'Card1'
CardIndex = 0
TabOrder = 0
ExplicitLeft = 1
ExplicitTop = 1
ExplicitWidth = 298
ExplicitHeight = 280
object pnlBackSide: TPanel
AlignWithMargins = True
Left = 10
Top = 10
Width = 249
Height = 263
Margins.Left = 10
Margins.Top = 10
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
AutoSize = True
Color = clBlue
ParentBackground = False
TabOrder = 0
object pnlFront: TPanel
AlignWithMargins = True
Left = 2
Top = 2
Width = 245
Height = 259
Margins.Left = 1
Margins.Top = 1
Margins.Right = 1
Margins.Bottom = 1
Align = alClient
AutoSize = True
BevelOuter = bvNone
ParentBackground = False
TabOrder = 0
ExplicitWidth = 274
ExplicitHeight = 243
object lblBlaBla: TLabel
AlignWithMargins = True
Left = 3
Top = 3
Width = 239
Height = 13
Align = alTop
Alignment = taCenter
Caption = 'BLA BLA'
ExplicitWidth = 39
end
object listboxTest: TListBox
AlignWithMargins = True
Left = 10
Top = 19
Width = 225
Height = 55
Margins.Left = 10
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
BevelInner = bvNone
ItemHeight = 13
TabOrder = 0
ExplicitWidth = 254
end
object Button1: TButton
AlignWithMargins = True
Left = 10
Top = 78
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button1'
TabOrder = 1
ExplicitWidth = 254
end
object Button3: TButton
AlignWithMargins = True
Left = 10
Top = 136
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button3'
TabOrder = 2
ExplicitWidth = 254
end
object Button4: TButton
AlignWithMargins = True
Left = 10
Top = 165
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button4'
TabOrder = 3
ExplicitWidth = 254
end
object Button2: TButton
AlignWithMargins = True
Left = 10
Top = 107
Width = 225
Height = 25
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 0
Align = alTop
Caption = 'Button2'
TabOrder = 4
ExplicitWidth = 254
end
object pnlComboBack: TPanel
AlignWithMargins = True
Left = 10
Top = 194
Width = 225
Height = 21
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 4
Align = alTop
AutoSize = True
BevelOuter = bvNone
TabOrder = 5
ExplicitWidth = 254
object cbbTest: TComboBox
Left = 0
Top = 0
Width = 225
Height = 21
Margins.Left = 10
Margins.Top = 4
Margins.Right = 10
Margins.Bottom = 4
Align = alClient
Style = csDropDownList
ItemIndex = 0
TabOrder = 0
Text = 'Select an item'
Items.Strings = (
'Select an item')
ExplicitWidth = 254
end
end
end
end
end
end
end
然后 运行 编译的应用程序,你会得到这样的东西:
您可以看到面板 pnlBackSide
没有自动调整大小,尽管其 属性 AutoSize
设置为 True
。为什么?
如何使面板 pnlBackSide
自动调整大小?
正如 Andreas 正确观察到的,属性 pnlFront.Align = alClient
和 pnlFront.AutoSize = True
是一个逻辑困境(因为它们是冲突的)因此不能同时执行。因此,解决方案是通过在设计时将它们设置为 pnlFront.Align = alNone
和 pnlFront.AutoSize = False
来 连续地 执行它们。然后在运行时,连续执行它们:
procedure TForm1.FormCreate(Sender: TObject);
begin
pnlFront.AutoSize := True;
pnlFront.Align := alClient;
end;
此外,为了补偿 pnlComboBack.Margins.Bottom
,还必须在设计时将 pnlBackSide.AutoSize
设置为 False
,然后在 运行 时设置为 True
-时间(按正确顺序):
procedure TForm1.FormCreate(Sender: TObject);
begin
pnlFront.AutoSize := True;
pnlBackSide.AutoSize := True;
pnlFront.Align := alClient;
end;
所以现在的结果是完美的: