我正在使用一组现有的TControls来设计一个新的VCL组件。控件放置在TPanels上,一些控件与左位置对齐,一个位于右侧位置,最后一个与客户端区域对齐。这些面板中的每一个都有自定义的组件作为它的父版。
我需要在面板之间放置拆分器,以便用户能够调整内部组件的大小。
问题是,当在运行时创建拆分器时,我根本找不出应用于拆分器位置的逻辑。
我访问了许多论坛,每个论坛都指向不同的解决方案,但都没有奏效。
试图:
1-在创建拆分器时,使用组件的属性"lef"t和“宽度”作为其“左”属性。
2-使用左边的属性来分配它应该在的位置。
还有一些我不记得了。
我遗漏了什么?
在下面的代码中,拆分器是在应该在的位置创建的。就在相应的面板之后,他们的左边属性对应于我最后一次尝试的东西(它的位置)。
constructor TDBIDBGridFilterBar.Create(AOwner: TComponent);
begin
inherited;
Self.Width := 490;
Self.Height := 23;
Self.Constraints.MinWidth := 285;
if fComboColunasWidth = 0 then
fComboColunasWidth := 118;
fBackGroundPanel := TPanel.Create(Self);
fBackGroundPanel.Parent := Self;
fBackGroundPanel.ShowCaption := false;
fBackGroundPanel.BevelOuter := bvNone;
fBackGroundPanel.Align := alClient;
fButtonsPanel := TPanel.Create(Self);
fButtonsPanel.Parent := fBackGroundPanel;
fButtonsPanel.ShowCaption := false;
fButtonsPanel.BevelOuter := bvNone;
fButtonsPanel.Width := 73;
fButtonsPanel.Align := alLeft;
fBtnMarcaTodos := TButton.Create(Self);
fBtnMarcaTodos.Parent := fButtonsPanel;
fBtnMarcaTodos.Width := 23;
fBtnMarcaTodos.Height := 23;
fBtnMarcaTodos.Left := 0;
fBtnMarcaTodos.Hint := 'Marcar todos';
fBtnMarcaTodos.ShowHint := true;
fBtnDesmarcaTodos := TButton.Create(Self);
fBtnDesmarcaTodos.Parent := fButtonsPanel;
fBtnDesmarcaTodos.Width := 23;
fBtnDesmarcaTodos.Height := 23;
fBtnDesmarcaTodos.Left := 23;
fBtnDesmarcaTodos.Hint := 'Desarcar todos';
fBtnDesmarcaTodos.ShowHint := true;
fBtnInveteSelecao := TButton.Create(Self);
fBtnInveteSelecao.Parent := fButtonsPanel;
fBtnInveteSelecao.Width := 23;
fBtnInveteSelecao.Height := 23;
fBtnInveteSelecao.Left := 46;
fBtnInveteSelecao.Hint := 'Inverter seleção';
fBtnInveteSelecao.ShowHint := true;
fLabelPanel := TPanel.Create(Self);
fLabelPanel.Parent := fBackGroundPanel;
fLabelPanel.Width := 33;
fLabelPanel.Align := alLeft;
fLabelPanel.ShowCaption := false;
fLabelPanel.BevelOuter := bvNone;
fLabel := TLabel.Create(Self);
fLabel.Parent := fLabelPanel;
fLabel.Top := 4;
fLabel.Left := 4;
fLabel.Caption := 'Filtro:';
fLabel.Width := 28;
fComboColunasPan := TPanel.Create(Self);
fComboColunasPan.Parent := fBackGroundPanel;
fComboColunasPan.ShowCaption := false;
fComboColunasPan.BevelOuter := bvNone;
fComboColunasPan.width := fComboColunasWidth;
fComboColunasPan.Align := alLeft;
fComboColunas := TDBIComboBox.Create(Self);
fComboColunas.Parent := fComboColunasPan;
fComboColunas.Text := '';
fComboColunas.OnEnter := PopulaComboColunas;
fComboColunas.OnChange := ComboChange;
fComboColunas.Style := csOwnerDrawFixed;
fComboColunas.Align := alClient;
fComboColunas.Hint := 'Colunas';
fComboColunas.ShowHint := true;
fSplitterColunas := TSplitter.Create(Self);
fSplitterColunas.Parent := fBackGroundPanel;
fSplitterColunas.Align := alLeft;
fSplitterColunas.Left := 1;
fComboOperadorPan := TPanel.Create(Self);
fComboOperadorPan.Parent := fBackGroundPanel;
fComboOperadorPan.ShowCaption := false;
fComboOperadorPan.BevelOuter := bvNone;
fComboOperadorPan.width := fComboColunasWidth;
fComboOperadorPan.Align := alLeft;
fComboOperador := TDBIComboBox.Create(Self);
fComboOperador.Parent := fComboOperadorPan;
// fComboOperador.Items.Add(cItensComboFiltro);
fComboOperador.Style := csOwnerDrawFixed;
fComboOperador.Align := alClient;
fComboOperador.Hint := 'Comparação';
fComboOperador.ShowHint := true;
fComboOperador.Text := ' = ';
fSplitterOperador := TSplitter.Create(Self);
fSplitterOperador.Parent := fBackGroundPanel;
fSplitterOperador.Align := alLeft;
fSplitterOperador.Left := 2;
fBtnAdcFiltroPan := TPanel.Create(Self);
fBtnAdcFiltroPan.Parent := fBackGroundPanel;
fBtnAdcFiltroPan.ShowCaption := false;
fBtnAdcFiltroPan.BevelOuter := bvNone;
fBtnAdcFiltroPan.width := 23;
fBtnAdcFiltroPan.Align := alRight;
fBtnAdcFiltro := TButton.Create(Self);
fBtnAdcFiltro.Parent := fBtnAdcFiltroPan;
fBtnAdcFiltro.Height := 23;
fBtnAdcFiltro.Width := 23;
fBtnAdcFiltro.Caption := '+';
fBtnAdcFiltro.Hint := 'Adicionar filtro...';
fBtnAdcFiltro.ShowHint := true;
fBtnAdcFiltro.OnClick := btnNewFilterClick;
fClientPanel := TPanel.Create(Self);
fClientPanel.Parent := fBackGroundPanel;
fClientPanel.ShowCaption := false;
fClientPanel.BevelOuter := bvNone;
fClientPanel.Align := AlClient;
fMaskEdit := TDBIMaskEdit.Create(Self);
fMaskEdit.Parent := fClientPanel;
fMaskEdit.Font.Size := 9;
fMaskEdit.Align := alTop;
end;发布于 2017-05-20 07:42:49
当您以左向右顺序将面板添加到背景面板时,在将Left属性设置为足够大的值之前,只需将添加面板的Alignment属性设置为足够大的值即可,因此它超过了已放置在背景面板上的组件的Left属性。您设置的值甚至可以大于背景面板的宽度。
当以从右向左的顺序(Alignment = alRight;)添加面板或其他组件时,可以将Left属性保留为0。
对于您放置在背景面板上的任何组件,以及拆分器,上述内容都是正确的。在使用拆分器时,您可能还希望将相邻组件Constraints.MinWidth属性设置为类似于10的内容,以防止组件宽度意外地降为零,从而变得不可见。一旦发生这种情况,就无法再用拆分器使其可见。
例如,这段代码:
procedure TForm1.Panel1Click(Sender: TObject);
var
i: integer;
begin
i := 1;
repeat
with TPanel.Create(self) do
begin
Parent := Panel1;
Left := 510;
Align := alLeft;
Width := 40;
Constraints.MinWidth := 10;
Caption := 'L'+IntToStr(i);
end;
if (i = 3) or (i = 5) then
with TSplitter.Create(self) do
begin
Parent := Panel1;
Left := 510;
Width := 10;
Align := alLeft;
end;
inc(i);
until i > 5;
with TPanel.Create(self) do
begin
Parent := Panel1;
Width := 40;
Align := alRight;
Caption := 'R1';
end;
with TPanel.Create(self) do
begin
Parent := Panel1;
Width := 40;
Align := alRight;
Caption := 'R2';
end;
with TPanel.Create(self) do
begin
Parent := Panel1;
Left := 510;
Align := alClient;
Caption := 'C';
end;
end;制作此设计:

L1 ..L5是alLeft对齐的,C是alClient对齐的,R1是的。R2是alRight对齐的。
发布于 2017-05-19 20:25:14
我设法弄明白了。由于以前创建的所有组件都是左对齐的,因此它们的左属性被设置为0,无论它们在表单中位于何处。
因此,为了解决这个问题,我不得不填充先前面板的左边属性。
fThisPanel.left := fPreviousPanel.Left + fPreviousPanel.width + 1,等等。
https://stackoverflow.com/questions/44077083
复制相似问题