Практическое руководство. Рисование текста в визуальном элементе
В следующем примере показано, как нарисовать текст в DrawingVisual с помощью объекта DrawingContext. Нарисованное содержимое возвращается путем вызова метода RenderOpen объекта DrawingVisual. Можно нарисовать рисунки и текст в контексте рисования.
Чтобы нарисовать текст в контексте рисования, используйте метод DrawText объекта DrawingContext. Закончив рисование содержимого в контексте рисования, вызовите метод Close, чтобы закрыть контекст рисования и сохранить содержимое.
Пример
' Create a DrawingVisual that contains text.
Private Function CreateDrawingVisualText() As DrawingVisual
' Create an instance of a DrawingVisual.
Dim drawingVisual As New DrawingVisual()
' Retrieve the DrawingContext from the DrawingVisual.
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
' Draw a formatted text string into the DrawingContext.
drawingContext.DrawText(New FormattedText("Click Me!", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Verdana"), 36, Brushes.Black), New Point(200, 116))
' Close the DrawingContext to persist changes to the DrawingVisual.
drawingContext.Close()
Return drawingVisual
End Function
// Create a DrawingVisual that contains text.
private DrawingVisual CreateDrawingVisualText()
{
// Create an instance of a DrawingVisual.
DrawingVisual drawingVisual = new DrawingVisual();
// Retrieve the DrawingContext from the DrawingVisual.
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Draw a formatted text string into the DrawingContext.
drawingContext.DrawText(
new FormattedText("Click Me!",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
36, System.Windows.Media.Brushes.Black),
new System.Windows.Point(200, 116));
// Close the DrawingContext to persist changes to the DrawingVisual.
drawingContext.Close();
return drawingVisual;
}
![]() |
---|
Полный пример кода, из которого был взят предыдущий пример, см. на веб-странице Hit Test Using DrawingVisuals Sample. |