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
Public Overridable Function Layout (container As Object, layoutEventArgs As LayoutEventArgs) As Boolean

參數

container
Object

配置引擎將在其中作業的容器。The container on which the layout engine will operate.

layoutEventArgs
LayoutEventArgs

Layout 事件的事件引數。An event argument from a Layout event.

傳回

如果 true 的父代 (Parent) 應該再次執行配置,則為 container,否則為 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. 您可以檢查 layoutEventArgs 上的 [AffectedProperty]、[AffectedComponent] 和 [AffectedControl] 屬性的值,以決定是否需要進行版面配置操作。You 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.

如果您的配置引擎邏輯判斷應該由容器的父系再次執行版面配置,則傳回 trueReturn 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.

適用於

另請參閱