Public Class MainWindow
Inherits System.Windows.Window
''' <summary>
''' Main annotation viewer.
''' </summary>
Private _viewer As Vintasoft.Imaging.Annotation.Wpf.UI.WpfAnnotationViewer
' ...
Public Sub New()
' ...
' subscribe to the FocusedAnnotationViewChanged event of viewer
' ...
AddHandler _viewer.FocusedAnnotationViewChanged, New System.EventHandler(Of Vintasoft.Imaging.Annotation.Wpf.UI.WpfAnnotationViewChangedEventArgs)(AddressOf _viewer_FocusedAnnotationViewChanged)
End Sub
''' <summary>
''' Handler of the FocusedAnnotationViewChanged event.
''' </summary>
Private Sub _viewer_FocusedAnnotationViewChanged(sender As Object, e As Vintasoft.Imaging.Annotation.Wpf.UI.WpfAnnotationViewChangedEventArgs)
' text transformer of old annotation
Dim textTransformer As Vintasoft.Imaging.Annotation.Wpf.UI.VisualTools.UserInteraction.WpfRectangularTextAnnotationTransformer = Nothing
If TypeOf e.OldValue Is Vintasoft.Imaging.Annotation.Wpf.UI.WpfTextAnnotationView Then
textTransformer = DirectCast(e.OldValue, Vintasoft.Imaging.Annotation.Wpf.UI.WpfTextAnnotationView).TextTransformer
ElseIf TypeOf e.OldValue Is Vintasoft.Imaging.Annotation.Wpf.UI.WpfFreeTextAnnotationView Then
textTransformer = DirectCast(e.OldValue, Vintasoft.Imaging.Annotation.Wpf.UI.WpfFreeTextAnnotationView).TextTransformer
End If
If textTransformer IsNot Nothing Then
' unsubscribe from the TextBoxUpdated event of viewer
RemoveHandler textTransformer.TextBoxTransformer.TextBoxUpdated, New System.EventHandler(Of Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfTextObjectTextBoxTransformerEventArgs)(AddressOf textTransformer_TextBoxUpdated)
End If
' text transformer of new annotation
textTransformer = Nothing
If TypeOf e.NewValue Is Vintasoft.Imaging.Annotation.Wpf.UI.WpfTextAnnotationView Then
textTransformer = DirectCast(e.NewValue, Vintasoft.Imaging.Annotation.Wpf.UI.WpfTextAnnotationView).TextTransformer
ElseIf TypeOf e.NewValue Is Vintasoft.Imaging.Annotation.Wpf.UI.WpfFreeTextAnnotationView Then
textTransformer = DirectCast(e.NewValue, Vintasoft.Imaging.Annotation.Wpf.UI.WpfFreeTextAnnotationView).TextTransformer
End If
If textTransformer IsNot Nothing Then
' set new font of the text box
textTransformer.TextBox.FontFamily = New System.Windows.Media.FontFamily("Arial")
textTransformer.TextBox.FontSize = 20.0
textTransformer.TextBox.FontWeight = System.Windows.FontWeights.Bold
' set text box location and size calculation mode to manual
textTransformer.AutomaticallyCalculateTextBoxLocationAndSize = False
' subscribe to the TextBoxUpdated event of viewer
AddHandler textTransformer.TextBoxTransformer.TextBoxUpdated, New System.EventHandler(Of Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfTextObjectTextBoxTransformerEventArgs)(AddressOf textTransformer_TextBoxUpdated)
End If
End Sub
''' <summary>
''' Handler of the TextBoxUpdated event.
''' Location and size of the text box are setting to such values that text box matches
''' the visible part of the text annotation.
''' </summary>
Private Sub textTransformer_TextBoxUpdated(sender As Object, e As Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfTextObjectTextBoxTransformerEventArgs)
' get the text annotation
Dim textAnnotation As Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.IWpfTextInteractiveObject = e.TextObject
' get bounding box of the annotation in the coordinate system of the annotation
Dim annotationBoundingBox As System.Windows.Rect = textAnnotation.GetBoundingBox()
' get bounding box of the annotation in the coordinate system of the viewer
annotationBoundingBox = TransformRect(annotationBoundingBox, textAnnotation.GetPointTransform(_viewer, _viewer.Image))
' get position of the auto-scroll of the viewer
Dim autoScrollPosition As System.Windows.Point = _viewer.ViewerState.AutoScrollPosition
' get visible area of the viewer
Dim clientRectangle As New System.Windows.Rect(autoScrollPosition.X, autoScrollPosition.Y, _viewer.ViewportWidth, _viewer.ViewportHeight)
' intersect the bounding box of the annotation with the visible area of the viewer
Dim newBounds As System.Windows.Rect = System.Windows.Rect.Intersect(annotationBoundingBox, clientRectangle)
' set new bounds
If newBounds.IsEmpty Then
e.TextBox.Width = 0.0
e.TextBox.Height = 0.0
Else
e.TextBox.Width = newBounds.Width
e.TextBox.Height = newBounds.Height
System.Windows.Controls.Canvas.SetLeft(e.TextBox, newBounds.Left)
System.Windows.Controls.Canvas.SetTop(e.TextBox, newBounds.Top)
End If
End Sub
''' <summary>
''' Transforms the rectangle and returns the bounding box.
''' </summary>
''' <param name="rect">The rectangle.</param>
''' <param name="pointTransform">The point transform.</param>
''' <returns>The bounding box of transformed rectangle.</returns>
Private Function TransformRect(rect As System.Windows.Rect, pointTransform As Vintasoft.Imaging.Wpf.WpfPointTransform) As System.Windows.Rect
Dim point1 As System.Windows.Point = pointTransform.TransformPoint(rect.TopLeft)
Dim point2 As System.Windows.Point = pointTransform.TransformPoint(rect.TopRight)
Dim point3 As System.Windows.Point = pointTransform.TransformPoint(rect.BottomRight)
Dim point4 As System.Windows.Point = pointTransform.TransformPoint(rect.BottomLeft)
Dim x As Double = System.Math.Min(System.Math.Min(point1.X, point2.X), System.Math.Min(point3.X, point4.X))
Dim y As Double = System.Math.Min(System.Math.Min(point1.Y, point2.Y), System.Math.Min(point3.Y, point4.Y))
Dim width As Double = System.Math.Max(System.Math.Max(point1.X, point2.X), System.Math.Max(point3.X, point4.X)) - x
Dim height As Double = System.Math.Max(System.Math.Max(point1.Y, point2.Y), System.Math.Max(point3.Y, point4.Y)) - y
Return New System.Windows.Rect(x, y, width, height)
End Function
End Class