LayoutEngine.Layout(Object, LayoutEventArgs) 方法

定义

请求布局引擎执行布局操作。Requests that the layout engine perform a layout operation.

public:
 virtual bool Layout(System::Object ^ container, System::Windows::Forms::LayoutEventArgs ^ layoutEventArgs);
public virtual bool Layout (object container, System.Windows.Forms.LayoutEventArgs layoutEventArgs);
abstract member Layout : obj * System.Windows.Forms.LayoutEventArgs -> bool
override this.Layout : obj * System.Windows.Forms.LayoutEventArgs -> bool

参数

container
Object

布局引擎将在其上执行操作的容器。The container on which the layout engine will operate.

layoutEventArgs
LayoutEventArgs

来自 Layout 事件的事件参数。An event argument from a Layout event.

返回

如果 container 的父容器应重新执行布局,则为 true;否则为 falsetrue if layout should be performed again by the parent of container; otherwise, false.

异常

container 不是一个 LayoutEngine 可在其上执行布局的类型。container is not a type on which LayoutEngine can perform layout.

示例

下面的代码示例演示如何使用Layout方法来实现自定义布局行为。The following code example demonstrates the use of the Layout method to implement custom layout behavior. 此代码示例是为LayoutEngine类提供的更大示例的一部分。This code example is part of a larger example provided for the LayoutEngine class.

public:
    virtual bool Layout(Object^ container,
        LayoutEventArgs^ layoutEventArgs) override
    {
        Control^ parent = nullptr;
        try
        {
            parent = (Control ^) container;
        }
        catch (InvalidCastException^ ex)
        {
            throw gcnew ArgumentException(
                "The parameter 'container' must be a control", "container", ex);
        }
        // Use DisplayRectangle so that parent.Padding is honored.
        Rectangle parentDisplayRectangle = parent->DisplayRectangle;
        Point nextControlLocation = parentDisplayRectangle.Location;

        for each (Control^ currentControl in parent->Controls)
        {
            // Only apply layout to visible controls.
            if (!currentControl->Visible)
            {
                continue;
            }

            // Respect the margin of the control:
            // shift over the left and the top.
            nextControlLocation.Offset(currentControl->Margin.Left,
                currentControl->Margin.Top);

            // Set the location of the control.
            currentControl->Location = nextControlLocation;

            // Set the autosized controls to their
            // autosized heights.
            if (currentControl->AutoSize)
            {
                currentControl->Size = currentControl->GetPreferredSize(
                    parentDisplayRectangle.Size);
            }

            // Move X back to the display rectangle origin.
            nextControlLocation.X = parentDisplayRectangle.X;

            // Increment Y by the height of the control
            // and the bottom margin.
            nextControlLocation.Y += currentControl->Height +
                currentControl->Margin.Bottom;
        }

        // Optional: Return whether or not the container's
        // parent should perform layout as a result of this
        // layout. Some layout engines return the value of
        // the container's AutoSize property.

        return false;
    }
public override bool Layout(
    object container,
    LayoutEventArgs layoutEventArgs)
{
    Control parent = container as Control;

    // Use DisplayRectangle so that parent.Padding is honored.
    Rectangle parentDisplayRectangle = parent.DisplayRectangle;
    Point nextControlLocation = parentDisplayRectangle.Location;

    foreach (Control c in parent.Controls)
    {
        // Only apply layout to visible controls.
        if (!c.Visible)
        {
            continue;
        }

        // Respect the margin of the control:
        // shift over the left and the top.
        nextControlLocation.Offset(c.Margin.Left, c.Margin.Top);

        // Set the location of the control.
        c.Location = nextControlLocation;

        // Set the autosized controls to their 
        // autosized heights.
        if (c.AutoSize)
        {
            c.Size = c.GetPreferredSize(parentDisplayRectangle.Size);
        }

        // Move X back to the display rectangle origin.
        nextControlLocation.X = parentDisplayRectangle.X;

        // Increment Y by the height of the control 
        // and the bottom margin.
        nextControlLocation.Y += c.Height + c.Margin.Bottom;
    }

    // Optional: Return whether or not the container's 
    // parent should perform layout as a result of this 
    // layout. Some layout engines return the value of 
    // the container's AutoSize property.

    return false;
}
Public Overrides Function Layout( _
ByVal container As Object, _
ByVal layoutEventArgs As LayoutEventArgs) As Boolean

    Dim parent As Control = container

    ' Use DisplayRectangle so that parent.Padding is honored.
    Dim parentDisplayRectangle As Rectangle = parent.DisplayRectangle
    Dim nextControlLocation As Point = parentDisplayRectangle.Location

    Dim c As Control
    For Each c In parent.Controls

        ' Only apply layout to visible controls.
        If c.Visible <> True Then
            Continue For
        End If

        ' Respect the margin of the control:
        ' shift over the left and the top.
        nextControlLocation.Offset(c.Margin.Left, c.Margin.Top)

        ' Set the location of the control.
        c.Location = nextControlLocation

        ' Set the autosized controls to their 
        ' autosized heights.
        If c.AutoSize Then
            c.Size = c.GetPreferredSize(parentDisplayRectangle.Size)
        End If

        ' Move X back to the display rectangle origin.
        nextControlLocation.X = parentDisplayRectangle.X

        ' Increment Y by the height of the control 
        ' and the bottom margin.
        nextControlLocation.Y += c.Height + c.Margin.Bottom
    Next c

    ' Optional: Return whether or not the container's 
    ' parent should perform layout as a result of this 
    ' layout. Some layout engines return the value of 
    ' the container's AutoSize property.
    Return False

End Function

注解

当布局引擎要对container参数执行布局操作时, 将调用此方法。This method is called when the layout engine is to perform a layout operation on the container parameter. 您可以AffectedProperty检查上AffectedControl AffectedComponent的、和属性的值,以确定是否需要布局操作。layoutEventArgsYou can check the value of the AffectedProperty, AffectedComponent, and AffectedControl properties on layoutEventArgs to decide if a layout operation is necessary.

继承者说明

重写Layout(Object, LayoutEventArgs)方法以提供自定义布局行为。Override the Layout(Object, LayoutEventArgs) method to provide your custom layout behavior.

在布局container参数的内容时, 请务必Visible检查每个子控件的属性。When laying out the contents of the container parameter, be sure to check the Visible property of each child control.

如果true布局引擎逻辑确定应由容器的父级再次执行布局, 则返回。Return true if your layout engine logic determines that layout should be performed again by the parent of the container. 例如, 当布局引擎调整子控件的大小时, 并且确定容器必须增加大小以容纳新布局时, 可能会发生这种情况。This might occur, for example, when the layout engine resizes child controls and determines that the container must be increased in size to accommodate the new layout.

适用于

另请参阅