LayoutEngine.Layout(Object, LayoutEventArgs) Método

Definición

Solicita que el motor de diseño realice una operación de diseño.

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

Parámetros

container
Object

El contenedor en el que funcionará el motor de diseño.

layoutEventArgs
LayoutEventArgs

Un argumento de evento de un evento Layout.

Devoluciones

Es true si el elemento primario de container debe realizar de nuevo el diseño; de lo contrario, es false.

Excepciones

container no es un tipo en el que LayoutEngine pueda realizar el diseño.

Ejemplos

En el ejemplo de código siguiente se muestra el uso del Layout método para implementar el comportamiento de diseño personalizado. Este ejemplo de código es parte de un ejemplo más grande proporcionado para la clase LayoutEngine.

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

Comentarios

Se llama a este método cuando el motor de diseño va a realizar una operación de diseño en el container parámetro . Puede comprobar el valor de las AffectedPropertypropiedades , AffectedComponenty AffectedControl en layoutEventArgs para decidir si es necesaria una operación de diseño.

Notas a los desarrolladores de herederos

Invalide el método para proporcionar el Layout(Object, LayoutEventArgs) comportamiento de diseño personalizado.

Al diseñar el contenido del container parámetro, asegúrese de comprobar la Visible propiedad de cada control secundario.

Devuelve true si la lógica del motor de diseño determina que el elemento primario del contenedor debe volver a realizar el diseño. Esto puede ocurrir, por ejemplo, cuando el motor de diseño cambia el tamaño de los controles secundarios y determina que el contenedor debe aumentarse de tamaño para dar cabida al nuevo diseño.

Se aplica a

Consulte también