LayoutEngine.Layout(Object, LayoutEventArgs) Metoda
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Żąda, aby aparat układu wykonał operację układu.
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
Parametry
- container
- Object
Kontener, na którym będzie działać aparat układu.
- layoutEventArgs
- LayoutEventArgs
Argument zdarzenia ze Layout zdarzenia.
Zwraca
truejeśli układ powinien zostać wykonany ponownie przez element nadrzędny ; containerw przeciwnym razie . false
Wyjątki
container nie jest typem, na którym LayoutEngine można wykonać układ.
Przykłady
W poniższym przykładzie kodu pokazano użycie metody w celu zaimplementowania niestandardowego Layout zachowania układu. Ten przykład kodu jest częścią większego przykładu udostępnionego LayoutEngine dla klasy .
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
Uwagi
Ta metoda jest wywoływana, gdy aparat układu ma wykonać operację układu na parametrze container . Możesz sprawdzić wartość AffectedPropertywłaściwości , i AffectedControllayoutEventArgs , AffectedComponentaby zdecydować, czy wymagana jest operacja układu.
Notatki dla spadkobierców
Zastąpij metodę Layout(Object, LayoutEventArgs) , aby zapewnić zachowanie układu niestandardowego.
Podczas układania zawartości parametru container należy sprawdzić Visible właściwość każdej kontrolki podrzędnej.
Wróć true , jeśli logika aparatu układu określa, że układ powinien być wykonywany ponownie przez element nadrzędny kontenera. Taka sytuacja może wystąpić na przykład wtedy, gdy aparat układu zmienia rozmiar kontrolek podrzędnych i określa, że kontener musi zostać zwiększony w celu dostosowania go do nowego układu.