Private Class MyWindow1 : Inherits Window
Private container As AnnContainer
Private viewer As BitmapSourceViewer
Private currentDesigner As AnnDesigner
Public Sub New(ByVal title As String)
Me.Title = title
Me.Width = 500
Me.Height = 200
viewer = New BitmapSourceViewer()
viewer.Source = New BitmapImage(New Uri(LeadtoolsExamples.Common.ImagesPath.Path + "ScarletMacaws.jpg"))
viewer.Width = Double.NaN
viewer.Height = Double.NaN
container = New AnnContainer()
container.Width = viewer.Source.Width
container.Height = viewer.Source.Height
AddHandler container.MouseLeftButtonDown, AddressOf container_MouseLeftButtonDown
AddHandler container.MouseMove, AddressOf container_MouseMove
AddHandler container.MouseLeftButtonUp, AddressOf container_MouseLeftButtonUp
viewer.Content = container
Dim stackPanel As StackPanel = New StackPanel()
DockPanel.SetDock(stackPanel, Dock.Top)
Dim buttonLine As Button = New Button()
AddHandler buttonLine.Click, AddressOf buttonLine_Click
buttonLine.Content = "Line"
stackPanel.Children.Add(buttonLine)
Dim buttonRectangle As Button = New Button()
AddHandler buttonRectangle.Click, AddressOf buttonRectangle_Click
buttonRectangle.Content = "Rectangle"
stackPanel.Children.Add(buttonRectangle)
Dim buttonSelect As Button = New Button()
AddHandler buttonSelect.Click, AddressOf buttonSelect_Click
buttonSelect.Content = "Select"
stackPanel.Children.Add(buttonSelect)
Dim panel As DockPanel = New DockPanel()
panel.Children.Add(viewer)
Me.Content = panel
End Sub
Private Sub buttonSelect_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
If Not currentDesigner Is Nothing AndAlso TypeOf currentDesigner Is AnnDrawDesigner Then
Dim drawDesigner As AnnDrawDesigner = CType(IIf(TypeOf currentDesigner Is AnnDrawDesigner, currentDesigner, Nothing), AnnDrawDesigner)
drawDesigner.Cancel()
currentDesigner = Nothing
End If
End Sub
Private Sub buttonLine_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
EndDesigner()
Dim lineDrawDesigner As AnnLineDrawDesigner = New AnnLineDrawDesigner(Me.container)
Dim lineObject As AnnLineObject = New AnnLineObject()
lineObject.Stroke = Brushes.Red
lineObject.StrokeThickness = 2.0
lineDrawDesigner.ObjectTemplate = lineObject
StartDrawDesigner(lineDrawDesigner)
End Sub
Private Sub buttonRectangle_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
EndDesigner()
Dim rectangleDrawDesigner As AnnRectangleDrawDesigner = New AnnRectangleDrawDesigner(Me.container)
Dim rectangleObject As AnnRectangleObject = New AnnRectangleObject()
rectangleObject.Stroke = Brushes.Red
rectangleObject.StrokeThickness = 2.0
rectangleObject.Fill = Brushes.Yellow
rectangleDrawDesigner.ObjectTemplate = rectangleObject
StartDrawDesigner(rectangleDrawDesigner)
End Sub
Private Sub EndDesigner()
If Not currentDesigner Is Nothing Then
If TypeOf currentDesigner Is AnnEditDesigner Then
Dim editDesigner As AnnEditDesigner = CType(IIf(TypeOf currentDesigner Is AnnEditDesigner, currentDesigner, Nothing), AnnEditDesigner)
editDesigner.End()
Else If TypeOf currentDesigner Is AnnDrawDesigner Then
Dim drawDesigner As AnnDrawDesigner = CType(IIf(TypeOf currentDesigner Is AnnDrawDesigner, currentDesigner, Nothing), AnnDrawDesigner)
drawDesigner.Cancel()
End If
End If
End Sub
Private Sub StartDrawDesigner(ByVal drawDesigner As AnnDrawDesigner)
AddHandler drawDesigner.Draw, AddressOf OnDesignerDraw
drawDesigner.IsClipCursor = True
currentDesigner = drawDesigner
End Sub
Private Sub OnDesignerDraw(ByVal sender As Object, ByVal e As AnnDrawDesignerEventArgs)
Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name)
If TypeOf e.Object Is AnnLineObject Then
Dim lineObject As AnnLineObject = CType(IIf(TypeOf e.Object Is AnnLineObject, e.Object, Nothing), AnnLineObject)
Console.WriteLine("Start Point: {{0},{1}}, EndPoint: {{2},{3}}", lineObject.X1, lineObject.Y1, lineObject.X2, lineObject.Y2)
Else If TypeOf e.Object Is AnnRectangleObject Then
Dim rectangleObject As AnnRectangleObject = CType(IIf(TypeOf e.Object Is AnnRectangleObject, e.Object, Nothing), AnnRectangleObject)
Console.WriteLine("Left: {0}", rectangleObject.Left)
Console.WriteLine("Top: {0}", rectangleObject.Top)
Console.WriteLine("Width: {0}", rectangleObject.Width)
Console.WriteLine("Height: {0}", rectangleObject.Height)
End If
End Sub
Private Sub container_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
If Not currentDesigner Is Nothing Then
e = New MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp, MouseButton.Left)
e.RoutedEvent = AnnDesigner.MouseLeftButtonDownEvent
currentDesigner.RaiseEvent(e)
End If
If (Not e.Handled) Then
Dim pt As System.Windows.Point = e.GetPosition(container)
Dim obj As AnnObjectBase = container.HitTest(pt)
If Not obj Is Nothing Then
If TypeOf obj Is AnnLineObject Then
Dim lineEditDesigner As AnnLineEditDesigner = New AnnLineEditDesigner(CType(IIf(TypeOf obj Is AnnLineObject, obj, Nothing), AnnLineObject))
StartEditing(lineEditDesigner, obj, pt)
lineEditDesigner.RaiseEvent(e)
Else If TypeOf obj Is AnnRectangleObject Then
Dim rectangleEditDesigner As AnnRectangleEditDesigner = New AnnRectangleEditDesigner(CType(IIf(TypeOf obj Is AnnRectangleObject, obj, Nothing), AnnRectangleObject))
StartEditing(rectangleEditDesigner, obj, pt)
rectangleEditDesigner.RaiseEvent(e)
End If
End If
End If
End Sub
Private Sub StartEditing(ByVal editDesigner As AnnEditDesigner, ByVal obj As AnnObjectBase, ByVal pt As System.Windows.Point)
EndDesigner()
AddHandler editDesigner.Edit, AddressOf OnDesignerEdit
editDesigner.IsClipCursor = True
editDesigner.RotateModifierKey = ModifierKeys.Shift
Dim i As Integer = 0
Do While i < editDesigner.ControlPointCount
Dim cp As System.Windows.Controls.Primitives.Thumb = New System.Windows.Controls.Primitives.Thumb()
cp.Width = 10
cp.Height = 10
cp.BorderBrush = Brushes.Blue
cp.BorderThickness = New Thickness(1.0)
cp.Background = Brushes.White
editDesigner.ControlPoints(i) = cp
i += 1
Loop
editDesigner.Start()
currentDesigner = editDesigner
End Sub
Private Sub OnDesignerEdit(ByVal sender As Object, ByVal e As AnnEditDesignerEventArgs)
Console.Write("Object: {0}, Operation: {1}, Status: {2}, ", e.Object.GetType().Name, e.Operation, e.OperationStatus)
If e.Operation = AnnEditDesignerOperation.MoveControlPoint Then
Console.WriteLine("Control Point index: {0}", e.MoveControlPointIndex)
End If
End Sub
Private Sub container_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If Not currentDesigner Is Nothing Then
e = New MouseEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp)
currentDesigner.RaiseEvent(e)
End If
End Sub
Private Sub container_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
If Not currentDesigner Is Nothing Then
e = New MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp, MouseButton.Left)
e.RoutedEvent = AnnDesigner.MouseLeftButtonUpEvent
currentDesigner.RaiseEvent(e)
End If
End Sub
End Class
Public Sub AnnDesigner_AnnDesigner(ByVal title As String)
Dim form As MyWindow1 = New MyWindow1(title)
form.ShowDialog()
End Sub
|