Private Class MyForm1 : Inherits Form
Private myAnnContainer As AnnContainer
Private viewer As RasterImageViewer
Private codecs As RasterCodecs
Private currentDesigner As AnnDesigner
Public Sub New(ByVal title As String)
Text = title
Size = New Size(500, 200)
viewer = New RasterImageViewer()
AddHandler viewer.TransformChanged, AddressOf viewer_TransformChanged
AddHandler viewer.PostImagePaint, AddressOf viewer_PostImagePaint
AddHandler viewer.MouseDown, AddressOf viewer_MouseDown
AddHandler viewer.MouseMove, AddressOf viewer_MouseMove
AddHandler viewer.MouseUp, AddressOf viewer_MouseUp
AddHandler viewer.LostFocus, AddressOf viewer_LostFocus
RasterCodecs.Startup()
codecs = New RasterCodecs()
viewer.Image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "image1.cmp")
RasterCodecs.Shutdown()
myAnnContainer = New AnnContainer()
myAnnContainer.Bounds = New AnnRectangle(0, 0, viewer.ImageSize.Width, viewer.ImageSize.Height)
myAnnContainer.UnitConverter = New AnnUnitConverter(viewer.ImageDpiX, viewer.ImageDpiY)
Dim panel As Panel = New Panel()
panel.Dock = DockStyle.Left
Controls.Add(panel)
panel.BringToFront()
Dim buttonLine As Button = New Button()
AddHandler buttonLine.Click, AddressOf buttonLine_Click
buttonLine.Text = "Line"
buttonLine.Dock = DockStyle.Top
panel.Controls.Add(buttonLine)
buttonLine.BringToFront()
Dim buttonRectangle As Button = New Button()
AddHandler buttonRectangle.Click, AddressOf buttonRectangle_Click
buttonRectangle.Text = "Rectangle"
buttonRectangle.Dock = DockStyle.Bottom
panel.Controls.Add(buttonRectangle)
buttonRectangle.BringToFront()
Dim buttonSelect As Button = New Button()
AddHandler buttonSelect.Click, AddressOf buttonSelect_Click
buttonSelect.Text = "Select"
buttonSelect.Dock = DockStyle.Left
panel.Controls.Add(buttonSelect)
buttonSelect.BringToFront()
viewer.Dock = DockStyle.Fill
Controls.Add(viewer)
viewer.BringToFront()
End Sub
Private Sub viewer_TransformChanged(ByVal sender As Object, ByVal e As EventArgs)
If Not viewer.Image Is Nothing AndAlso Not myAnnContainer Is Nothing Then
myAnnContainer.Transform = viewer.Transform.Clone()
End If
End Sub
Private Sub viewer_PostImagePaint(ByVal sender As Object, ByVal e As PaintEventArgs)
If Not viewer.Image Is Nothing AndAlso Not myAnnContainer Is Nothing Then
myAnnContainer.Draw(e.Graphics)
End If
End Sub
Private Sub buttonSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs)
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 System.EventArgs)
EndDesigner()
Dim lineDrawDesigner As AnnLineDrawDesigner = New AnnLineDrawDesigner()
Dim lineObject As AnnLineObject = New AnnLineObject()
lineObject.Pen = New AnnPen(Color.Red, New AnnLength(2, AnnUnit.Pixel))
lineDrawDesigner.ObjectTemplate = lineObject
StartDrawDesigner(lineDrawDesigner)
End Sub
Private Sub buttonRectangle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
EndDesigner()
Dim rectangleDrawDesigner As AnnRectangleDrawDesigner = New AnnRectangleDrawDesigner()
Dim rectangleObject As AnnRectangleObject = New AnnRectangleObject()
rectangleObject.Pen = New AnnPen(Color.Red, New AnnLength(2, AnnUnit.Pixel))
rectangleObject.Brush = New AnnSolidBrush(Color.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()
ElseIf 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.Owner = viewer
drawDesigner.ClipCursor = True
drawDesigner.Container = myAnnContainer
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}, EndPoint: {1}", lineObject.StartPoint, lineObject.EndPoint)
ElseIf TypeOf e.Object Is AnnRectangleObject Then
Dim rectangleObject As AnnRectangleObject = CType(IIf(TypeOf e.Object Is AnnRectangleObject, e.Object, Nothing), AnnRectangleObject)
Console.WriteLine("Bounds: {0}", rectangleObject.Bounds)
End If
End Sub
Private Sub viewer_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim handled As Boolean = False
If Not currentDesigner Is Nothing Then
handled = currentDesigner.MouseDown(e)
End If
If (Not handled) Then
Dim pt As AnnPoint = New AnnPoint(e.X, e.Y)
Dim obj As AnnObject = myAnnContainer.HitTest(pt, 2)
If Not obj Is Nothing Then
If TypeOf obj Is AnnLineObject Then
Dim lineEditDesigner As AnnLineEditDesigner = New AnnLineEditDesigner()
StartEditing(lineEditDesigner, obj, pt)
lineEditDesigner.MouseDown(e)
ElseIf TypeOf obj Is AnnRectangleObject Then
Dim rectangleEditDesigner As AnnRectangleEditDesigner = New AnnRectangleEditDesigner()
StartEditing(rectangleEditDesigner, obj, pt)
rectangleEditDesigner.MouseDown(e)
End If
End If
End If
End Sub
Private Sub StartEditing(ByVal editDesigner As AnnEditDesigner, ByVal obj As AnnObject, ByVal pt As AnnPoint)
EndDesigner()
AddHandler editDesigner.Edit, AddressOf OnDesignerEdit
editDesigner.Owner = viewer
editDesigner.Container = myAnnContainer
editDesigner.ClipCursor = True
editDesigner.EditObject = obj
editDesigner.HitTestBuffer = 2
editDesigner.ControlPointsHitTestBuffer = 2
editDesigner.RotateModifierKey = Keys.Shift
editDesigner.HitTestObject = obj.HitTest(pt, 2)
Dim rectangleControlPoint As AnnRectangleControlPoint = New AnnRectangleControlPoint()
rectangleControlPoint.Size = New AnnSize(10, 10, AnnUnit.Pixel)
rectangleControlPoint.Pen = New AnnPen(Color.Blue, New AnnLength(1, AnnUnit.Pixel))
rectangleControlPoint.Brush = New AnnSolidBrush(Color.White)
Dim i As Integer = 0
Do While i < editDesigner.ControlPointCount
editDesigner.ControlPoints(i) = rectangleControlPoint
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)
Else
Console.WriteLine("HitTestObject: {0}", e.HitTestObject)
End If
End Sub
Private Sub viewer_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim handled As Boolean = False
If Not currentDesigner Is Nothing Then
handled = currentDesigner.MouseMove(e)
End If
End Sub
Private Sub viewer_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim handled As Boolean = False
If Not currentDesigner Is Nothing Then
handled = currentDesigner.MouseUp(e)
End If
End Sub
Private Sub viewer_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
If Not currentDesigner Is Nothing Then
currentDesigner.Cancel()
End If
End Sub
End Class
Public Sub AnnDesigner_AnnDesigner(ByVal title As String)
Dim form As MyForm1 = New MyForm1(title)
form.ShowDialog()
End Sub
|