LayoutEngine.Layout(Object, LayoutEventArgs) Methode

Definition

Fordert an, dass die Layout-Engine eine Layoutoperation ausführt.

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

Parameter

container
Object

Der Container, für den die Layout-Engine ausgeführt wird.

layoutEventArgs
LayoutEventArgs

Ein Ereignisargument von einem Layout-Ereignis.

Gibt zurück

true, wenn das Layout erneut vom übergeordneten Element von container erstellt werden soll, andernfalls false.

Ausnahmen

container ist kein Typ, für den LayoutEngine eine Layoutoperation ausführen kann.

Beispiele

Im folgenden Codebeispiel wird die Verwendung der -Methode zum Implementieren des Layout benutzerdefinierten Layoutverhaltens veranschaulicht. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die LayoutEngine-Klasse bereitgestellt wird.

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

Hinweise

Diese Methode wird aufgerufen, wenn die Layout-Engine einen Layoutvorgang für den container Parameter ausführen soll. Sie können den Wert der AffectedPropertyEigenschaften , AffectedComponentund AffectedControl überprüfen layoutEventArgs , um zu entscheiden, ob ein Layoutvorgang erforderlich ist.

Hinweise für Vererber

Überschreiben Sie die Layout(Object, LayoutEventArgs) -Methode, um Ihr benutzerdefiniertes Layoutverhalten bereitzustellen.

Achten Sie beim Festlegen des Inhalts des container Parameters darauf, die Visible -Eigenschaft jedes untergeordneten Steuerelements zu überprüfen.

Gibt zurück true , wenn ihre Layout-Engine-Logik bestimmt, dass das Layout erneut vom übergeordneten Container ausgeführt werden soll. Dies kann beispielsweise der Fall sein, wenn die Layout-Engine die Größe untergeordneter Steuerelemente ändert und feststellt, dass der Container vergrößert werden muss, um das neue Layout zu berücksichtigen.

Gilt für:

Weitere Informationen