在Delphi Custom Panel中模拟对齐位置

时间:2017-09-05 18:21:49

标签: delphi alignment panel

我在Delphi XE5中构建自定义面板,我很难模拟新的" Gravity" 属性,我可以在其中组合两个坐标(如Right + Bottom),效果类似于" Align"但是,它没有调整对象,方向的大小。我遇到的主要问题是模拟这种行为。我的初衷是在内存中使用相同的" Parent"在我的自定义面板中,然后调整到" Gravity"中定义的位置。覆盖" SetBounds"方法。它有效,但有点不稳定,特别是在设计时间"。 有人可以建议我如何使用VCL更有效地模拟这种对齐吗?

function TZPanel.GetPosition: TCustomPanel;
    var
      sid: TZSide;
      anch: TAnchors;
      panTest: TPanel;
      function getGravity(al: TAlign): TRect;
      var
        panGravity: TPanel;
        I: Integer;
      begin
        try
          //Self.Visible := False;

          panGravity:= TPanel.Create(Self);
          panGravity.BevelInner  := panTest.BevelInner;
          panGravity.BevelOuter  := panTest.BevelOuter;
          panGravity.BevelWidth  := panTest.BevelWidth;
          panGravity.BorderWidth := panTest.BorderWidth;
          panGravity.ParentBackground := True;
          panGravity.SetBounds(panTest.Left, panTest.Top, panTest.Width, panTest.Height);
          panGravity.Parent:= Self.Parent;
          panGravity.Align := al;

          Result:= panGravity.BoundsRect;
        finally
          panGravity.Destroy;
          Self.Visible := True;
        end;
      end;

    begin
      panTest := TPanel.Create(Self);
      panTest.Align   := Align;
      panTest.Anchors := Anchors;
      panTest.BevelInner  := BevelInner;
      panTest.BevelOuter  := BevelOuter;
      panTest.BevelWidth  := BevelWidth;
      panTest.BorderWidth := BorderWidth;
      panTest.SetBounds(Left, Top, Width, Height);

      if (FGravity = []) then
      begin
        //
      end
      else
      begin
        panTest.Align := alCustom;
        anch := [];

        for sid in FGravity do
        begin
          case sid of
            sTop:
            begin
              panTest.Top := getGravity(alTop).Top;
              anch := anch + [akTop];
            end;

            sRight:
            begin
              panTest.Left := getGravity(alRight).Left;
              anch := anch + [akRight];
            end;

            sBottom:
            begin
              panTest.Top := getGravity(alBottom).Top;
              anch := anch + [akBottom];
            end;

            sLeft:
            begin
              panTest.Left := getGravity(alLeft).Left;
              anch := anch + [akLeft];
            end;
          end;
        end;
        panTest.Anchors := anch;
      end;
      Result := panTest;
    end;

0 个答案:

没有答案