Poznámka
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
Toto téma obsahuje přehled použití DrawingVisual objektů ve vizuální vrstvě WPF.
DrawingVisual – objekt
DrawingVisual je jednoduchá třída výkresu, která slouží k vykreslení obrazců, obrázků nebo textu. Tato třída je považována za lehkou, protože neposkytuje rozložení ani zpracování událostí, což zlepšuje její výkon. Z tohoto důvodu jsou kresby ideální pro pozadí a klipart.
Hostitelský kontejner DrawingVisual
Pokud chcete použít DrawingVisual objekty, musíte pro objekty vytvořit kontejner hostitele. cs-CZ: Objekt kontejneru hostitele musí být odvozen od třídy FrameworkElement, která poskytuje podporu pro rozložení a zpracování událostí, jež třída DrawingVisual postrádá. Objekt kontejneru hostitele nezobrazuje žádné viditelné vlastnosti, protože jeho hlavním účelem je obsahovat podřízené objekty. Visibility Vlastnost kontejneru hostitele však musí být nastavena na Visible; jinak nebude viditelná žádná z jejích podřízených prvků.
Když vytvoříte objekt kontejneru hostitele pro objekty vizuálů, je nutné uložit odkazy na objekty vizuálu do objektu VisualCollection. Použijte metodu Add k přidání objektu vizuálu do kontejneru hostitele. V následujícím příkladu se vytvoří objekt kontejneru hostitele a do jeho VisualCollectionobjektu se přidají tři vizuální objekty .
// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyVisualHost : FrameworkElement
{
// Create a collection of child visual objects.
private VisualCollection _children;
public MyVisualHost()
{
_children = new VisualCollection(this);
_children.Add(CreateDrawingVisualRectangle());
_children.Add(CreateDrawingVisualText());
_children.Add(CreateDrawingVisualEllipses());
// Add the event handler for MouseLeftButtonUp.
this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
}
' Create a host visual derived from the FrameworkElement class.
' This class provides layout, event handling, and container support for
' the child visual objects.
Public Class MyVisualHost
Inherits FrameworkElement
' Create a collection of child visual objects.
Private _children As VisualCollection
Public Sub New()
_children = New VisualCollection(Me)
_children.Add(CreateDrawingVisualRectangle())
_children.Add(CreateDrawingVisualText())
_children.Add(CreateDrawingVisualEllipses())
' Add the event handler for MouseLeftButtonUp.
AddHandler MouseLeftButtonUp, AddressOf MyVisualHost_MouseLeftButtonUp
End Sub
Poznámka:
Kompletní vzorový kód, ze kterého byl extrahován předchozí příklad kódu, najdete v části Hit Test Using DrawingVisuals Sample.
Vytváření objektů DrawingVisual
Když vytvoříte objekt DrawingVisual, nemá žádný obsah kreslení. Text, grafiku nebo obsah obrázku můžete přidat načtením a nakreslením objektu DrawingContext . A DrawingContext je vrácen voláním RenderOpen metody objektu DrawingVisual .
Chcete-li nakreslit obdélník do objektu DrawingContext, použijte DrawRectangle metodu objektu DrawingContext . Podobné metody existují pro kreslení jiných typů obsahu. Až dokončíte kreslení obsahu do DrawingContext, volejte metodu Close k zavření DrawingContext a uložení obsahu.
V následujícím příkladu je vytvořen objekt DrawingVisual a obdélník je nakreslen do jeho DrawingContext.
// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
DrawingVisual drawingVisual = new DrawingVisual();
// Retrieve the DrawingContext in order to create new drawing content.
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Create a rectangle and draw it in the DrawingContext.
Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
// Persist the drawing content.
drawingContext.Close();
return drawingVisual;
}
' Create a DrawingVisual that contains a rectangle.
Private Function CreateDrawingVisualRectangle() As DrawingVisual
Dim drawingVisual As New DrawingVisual()
' Retrieve the DrawingContext in order to create new drawing content.
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
' Create a rectangle and draw it in the DrawingContext.
Dim rect As New Rect(New Point(160, 100), New Size(320, 80))
drawingContext.DrawRectangle(Brushes.LightBlue, CType(Nothing, Pen), rect)
' Persist the drawing content.
drawingContext.Close()
Return drawingVisual
End Function
Vytváření přepisování pro členy FrameworkElement
Objekt kontejneru hostitele zodpovídá za správu kolekce vizuálních objektů. To vyžaduje, aby kontejner hostitele přepsal členy pro odvozenou FrameworkElement třídu.
Následující seznam popisuje dva členy, které musíte přepsat:
GetVisualChild: Vrátí podřízený prvek na daném indexu z kolekce podřízených prvků.
VisualChildrenCount: Získá počet vizuálních podřízených elementů v tomto prvku.
V následujícím příkladu se implementují překrytí obou FrameworkElement členů.
// Provide a required override for the VisualChildrenCount property.
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return _children[index];
}
' Provide a required override for the VisualChildrenCount property.
Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
Get
Return _children.Count
End Get
End Property
' Provide a required override for the GetVisualChild method.
Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
If index < 0 OrElse index >= _children.Count Then
Throw New ArgumentOutOfRangeException()
End If
Return _children(index)
End Function
Poskytování podpory detekce interakcí
Objekt kontejneru hostitele může poskytovat zpracování událostí, i když nezobrazuje žádné viditelné vlastnosti – jeho Visibility vlastnost však musí být nastavena na Visiblehodnotu . To vám umožní vytvořit rutinu pro zpracování událostí pro kontejner hostitele, která může zachytávat události myši, jako je uvolnění levého tlačítka myši. Rutina zpracování událostí pak může implementovat testování zásahu vyvoláním metody HitTest. Parametr metody HitTestResultCallback odkazuje na uživatelem definovanou proceduru, kterou můžete použít k určení výsledné akce detekce zásahu.
V následujícím příkladu je detekce stisknutí implementována pro objekt hostitelského kontejneru a jeho podřízené objekty.
// Capture the mouse event and hit test the coordinate point value against
// the child visual objects.
void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// Retrieve the coordinates of the mouse button event.
System.Windows.Point pt = e.GetPosition((UIElement)sender);
// Initiate the hit test by setting up a hit test result callback method.
VisualTreeHelper.HitTest(this, null, new HitTestResultCallback(myCallback), new PointHitTestParameters(pt));
}
// If a child visual object is hit, toggle its opacity to visually indicate a hit.
public HitTestResultBehavior myCallback(HitTestResult result)
{
if (result.VisualHit.GetType() == typeof(DrawingVisual))
{
if (((DrawingVisual)result.VisualHit).Opacity == 1.0)
{
((DrawingVisual)result.VisualHit).Opacity = 0.4;
}
else
{
((DrawingVisual)result.VisualHit).Opacity = 1.0;
}
}
// Stop the hit test enumeration of objects in the visual tree.
return HitTestResultBehavior.Stop;
}
' Capture the mouse event and hit test the coordinate point value against
' the child visual objects.
Private Sub MyVisualHost_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
' Retrieve the coordinates of the mouse button event.
Dim pt As Point = e.GetPosition(CType(sender, UIElement))
' Initiate the hit test by setting up a hit test result callback method.
VisualTreeHelper.HitTest(Me, Nothing, New HitTestResultCallback(AddressOf myCallback), New PointHitTestParameters(pt))
End Sub
' If a child visual object is hit, toggle its opacity to visually indicate a hit.
Public Function myCallback(ByVal result As HitTestResult) As HitTestResultBehavior
If result.VisualHit.GetType() Is GetType(DrawingVisual) Then
If (CType(result.VisualHit, DrawingVisual)).Opacity = 1.0 Then
CType(result.VisualHit, DrawingVisual).Opacity = 0.4
Else
CType(result.VisualHit, DrawingVisual).Opacity = 1.0
End If
End If
' Stop the hit test enumeration of objects in the visual tree.
Return HitTestResultBehavior.Stop
End Function
Viz také
- DrawingVisual
- HitTest
- přehled vykreslování grafiky WPF
- Detekce kolizí ve vizuální vrstvě
.NET Desktop feedback