LayoutEngine Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Alaposztályt biztosít az elrendezési motorok implementálására.
public ref class LayoutEngine abstract
public abstract class LayoutEngine
type LayoutEngine = class
Public MustInherit Class LayoutEngine
- Öröklődés
-
LayoutEngine
Példák
Az alábbi példakód bemutatja az osztály egyéni elrendezési viselkedésének LayoutEngine megvalósítására való használatát.
#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Drawing;
using namespace System::Text;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Layout;
// This class demonstrates a simple custom layout engine.
public ref class DemoFlowLayout : public 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;
}
};
// This class demonstrates a simple custom layout panel.
// It overrides the LayoutEngine property of the Panel
// control to provide a custom layout engine.
public ref class DemoFlowPanel : public Panel
{
private:
DemoFlowLayout^ layoutEngine;
public:
DemoFlowPanel()
{
layoutEngine = gcnew DemoFlowLayout();
}
public:
virtual property System::Windows::Forms::Layout::LayoutEngine^ LayoutEngine
{
System::Windows::Forms::Layout::LayoutEngine^ get() override
{
if (layoutEngine == nullptr)
{
layoutEngine = gcnew DemoFlowLayout();
}
return layoutEngine;
}
}
};
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
// This class demonstrates a simple custom layout panel.
// It overrides the LayoutEngine property of the Panel
// control to provide a custom layout engine.
public class DemoFlowPanel : Panel
{
private DemoFlowLayout layoutEngine;
public DemoFlowPanel()
{
}
public override LayoutEngine LayoutEngine
{
get
{
layoutEngine ??= new DemoFlowLayout();
return layoutEngine;
}
}
}
// This class demonstrates a simple custom layout engine.
public class DemoFlowLayout : LayoutEngine
{
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;
}
}
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Layout
' This class demonstrates a simple custom layout panel.
' It overrides the LayoutEngine property of the Panel
' control to provide a custom layout engine.
Public Class DemoFlowPanel
Inherits Panel
Private layoutEng As DemoFlowLayout
Public Sub New()
End Sub
Public Overrides ReadOnly Property LayoutEngine() As LayoutEngine
Get
If layoutEng Is Nothing Then
layoutEng = New DemoFlowLayout()
End If
Return layoutEng
End Get
End Property
End Class
' This class demonstrates a simple custom layout engine.
Public Class DemoFlowLayout
Inherits LayoutEngine
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
End Class
Megjegyzések
Akkor használja az LayoutEngine osztályt, ha egyéni elrendezési viselkedést szeretne létrehozni futásidőben. Saját osztályt hozhat ki az LayoutEngine osztályból, és felülbírálhatja a Layout metódust az egyéni elrendezési viselkedés meghatározásához.
Konstruktorok
| Name | Description |
|---|---|
| LayoutEngine() |
Inicializálja a LayoutEngine osztály új példányát. |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| InitLayout(Object, BoundsSpecified) |
Inicializálja az elrendezési motort. |
| Layout(Object, LayoutEventArgs) |
Azt kéri, hogy az elrendezési motor végezzen el egy elrendezési műveletet. |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |