LEADTOOLS Annotations for WPF and Silverlight (Leadtools.Windows.Annotations assembly)

AnnDesigner Class

Show in webframe
Example 





Members 
The base class for all the designers in the annotation toolkit.
Object Model
Syntax
public abstract class AnnDesigner 
'Declaration
 
Public MustInherit Class AnnDesigner 
'Usage
 
Dim instance As AnnDesigner

            

            
public ref class AnnDesigner abstract 
Remarks

An AnnDesigner derived class controls the user interface functionality involved in drawing, editing or running an object. This class provides the basic functionality common to all the designers such as hooking into a container, clipping the mouse cursor, etc.

You pass an AnnContainer object to this class constructor. Then based on the exact designer functionality, it will hook into the various mouse events to draw a new, edit an existing (by moving, resizing, etc.) or run (if the container is in run user mode) an AnnObject.

Example
Copy Code  
Imports Leadtools.Windows.Controls
Imports Leadtools.Windows.Annotations

''' Shows how to use designers to create and select annotation objects
Private Class MyWindow1 : Inherits Window
   Private container As AnnContainer
   'AnnAutomationManager manager;
   Private viewer As ImageViewer
   Private currentDesigner As AnnDesigner
   Public Sub New(ByVal title As String)
      Me.Title = title
      Me.Width = 500
      Me.Height = 200

      viewer = New ImageViewer()

      ' load an image into the viewer
      ' fix this path to an existing image file on your system
      viewer.Source = New BitmapImage(New Uri(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg")))
      viewer.Width = Double.NaN
      viewer.Height = Double.NaN

      ' create and set up the container
      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

      ' Set the container as a content of viewer
      viewer.Content = container

      ' Create a panel with three buttons
      Dim stackPanel As StackPanel = New StackPanel()
      DockPanel.SetDock(stackPanel, Dock.Top)

      ' Create three buttons
      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)

      ' Add the viewer
      Dim panel As DockPanel = New DockPanel()
      panel.Children.Add(viewer)

      ' set panel as content
      Me.Content = panel
   End Sub

   Private Sub buttonSelect_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      ' Select button is clicked
      ' cancel any draw designer running
      If Not currentDesigner Is Nothing AndAlso TypeOf currentDesigner Is AnnDrawDesigner Then
         Dim drawDesigner As AnnDrawDesigner = DirectCast(currentDesigner, AnnDrawDesigner)
         drawDesigner.Cancel()
         currentDesigner = Nothing
      End If
   End Sub

   Private Sub buttonLine_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      ' Line button is clicked

      ' first end any current designer (if any)
      EndDesigner()

      ' start a new Line draw designer
      Dim lineDrawDesigner As AnnLineDrawDesigner = New AnnLineDrawDesigner(Me.container)

      ' set up the object template (a 2 pixels-wide pen)
      Dim lineObject As AnnLineObject = New AnnLineObject()
      lineObject.Stroke = Colors.Red
      lineObject.StrokeThickness = 2.0
      lineDrawDesigner.ObjectTemplate = lineObject

      StartDrawDesigner(lineDrawDesigner)
   End Sub


   Private Sub buttonRectangle_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      ' Rectangle button is clicked

      ' first end any current designer (if any)
      EndDesigner()

      ' start a new Rectangle draw designer
      Dim rectangleDrawDesigner As AnnRectangleDrawDesigner = New AnnRectangleDrawDesigner(Me.container)

      ' set up the object template (a 2 pixels-wide pen)
      Dim rectangleObject As AnnRectangleObject = New AnnRectangleObject()
      rectangleObject.Stroke = Colors.Red
      rectangleObject.StrokeThickness = 2.0
      rectangleObject.Fill = Colors.Yellow
      rectangleDrawDesigner.ObjectTemplate = rectangleObject

      StartDrawDesigner(rectangleDrawDesigner)
   End Sub

   Private Sub EndDesigner()
      ' ends any running designer
      If Not currentDesigner Is Nothing Then
         If TypeOf currentDesigner Is AnnEditDesigner Then
            Dim editDesigner As AnnEditDesigner = DirectCast(currentDesigner, AnnEditDesigner)
            editDesigner.End()
         ElseIf TypeOf currentDesigner Is AnnDrawDesigner Then
            Dim drawDesigner As AnnDrawDesigner = DirectCast(currentDesigner, AnnDrawDesigner)
            drawDesigner.Cancel()
         End If
      End If
   End Sub

   Private Sub StartDrawDesigner(ByVal drawDesigner As AnnDrawDesigner)
      ' set up the current designer
      AddHandler drawDesigner.Draw, AddressOf OnDesignerDraw
      currentDesigner = drawDesigner
   End Sub

   Private Sub OnDesignerDraw(ByVal sender As Object, ByVal e As AnnDrawDesignerEventArgs)
      ' show information on the current draw operation
      Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name)
      If TypeOf e.Object Is AnnLineObject Then
         Dim lineObject As AnnLineObject = DirectCast(e.Object, AnnLineObject)
         Console.WriteLine("Start Point: {{0},{1}}, EndPoint: {{2},{3}}", _
          lineObject.Start.X, lineObject.Start.Y, lineObject.End.X, lineObject.End.Y)
      ElseIf TypeOf e.Object Is AnnRectangleObject Then
         Dim rectangleObject As AnnRectangleObject = DirectCast(e.Object, AnnRectangleObject)
         Console.WriteLine("Left: {0}", rectangleObject.Rect.Left)
         Console.WriteLine("Top: {0}", rectangleObject.Rect.Top)
         Console.WriteLine("Width: {0}", rectangleObject.Rect.Width)
         Console.WriteLine("Height: {0}", rectangleObject.Rect.Height)
      End If
   End Sub

   Private Sub container_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         e = New MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp, MouseButton.Left)
         currentDesigner.OnMouseLeftButtonDown(DirectCast(sender, AnnContainer), e)
      End If

      If (Not e.Handled) Then
         ' the mouse click was ! handled by a designer
         ' check if the click was on top of an existing object that we can start editing
         Dim pt As System.Windows.Point = e.GetPosition(container)
         Dim obj As AnnObject = container.HitTest(pt)
         If Not obj Is Nothing Then
            ' yes, start the edit designer for this object
            If TypeOf obj Is AnnLineObject Then
               Dim lineEditDesigner As New AnnLineEditDesigner(DirectCast(obj, AnnLineObject), DirectCast(viewer.Content, AnnContainer))
               StartEditing(lineEditDesigner, obj, pt)
               lineEditDesigner.OnMouseLeftButtonDown(DirectCast(sender, AnnContainer), e)
            ElseIf TypeOf obj Is AnnRectangleObject Then
               Dim rectangleEditDesigner As New AnnRectangleEditDesigner(DirectCast(obj, AnnRectangleObject), _
                  DirectCast(viewer.Content, AnnContainer))
               StartEditing(rectangleEditDesigner, obj, pt)
               rectangleEditDesigner.OnMouseLeftButtonDown(DirectCast(sender, AnnContainer), e)
            End If
         End If
      End If
   End Sub

   Private Sub StartEditing(ByVal editDesigner As AnnEditDesigner, ByVal obj As AnnObject, ByVal pt As System.Windows.Point)
      ' first end any running designers
      EndDesigner()

      ' set up the current designer
      AddHandler editDesigner.Edit, AddressOf OnDesignerEdit
      editDesigner.RotateModifierKey = ModifierKeys.Shift

      Dim i As Integer = 0
      Do While i < editDesigner.ControlPointCount
         ' set up the edit designer control points
         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)
      ' show information on the current draw operation
      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)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         e = New MouseEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp)

         currentDesigner.OnMouseMove(DirectCast(sender, AnnContainer), e)
      End If
   End Sub

   Private Sub container_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         e = New MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, e.Timestamp, MouseButton.Left)
         currentDesigner.OnMouseLeftButtonUp(DirectCast(sender, AnnContainer), 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


Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools.Windows.Controls;
using Leadtools.Windows.Annotations;
using Leadtools.Demos;
using Leadtools.Help;

/// Shows how to use designers to create and select annotation objects
class MyWindow1 : Window
{
   AnnContainer container;
   //AnnAutomationManager manager;
   ImageViewer viewer;
   AnnDesigner currentDesigner;
   public MyWindow1(string title)
   {
      this.Title = title;
      this.Width = 500;
      this.Height = 200;

      viewer = new ImageViewer();

      // load an image into the viewer
      // fix this path to an existing image file on your system
      viewer.Source = new BitmapImage(new Uri(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg")));  
      viewer.Width = double.NaN;
      viewer.Height = double.NaN;

      // create and set up the container
      container = new AnnContainer();
      container.Width = viewer.Source.Width;
      container.Height = viewer.Source.Height;

      container.MouseLeftButtonDown += new MouseButtonEventHandler(container_MouseLeftButtonDown);
      container.MouseMove += new MouseEventHandler(container_MouseMove);
      container.MouseLeftButtonUp += new MouseButtonEventHandler(container_MouseLeftButtonUp);

      // Set the container as a content of viewer
      viewer.Content = container;

      // Create a panel with three buttons
      StackPanel stackPanel = new StackPanel();
      DockPanel.SetDock(stackPanel, Dock.Top);

      // Create three buttons
      Button buttonLine = new Button();
      buttonLine.Click += new RoutedEventHandler(buttonLine_Click);
      buttonLine.Content = "Line";
      stackPanel.Children.Add(buttonLine);

      Button buttonRectangle = new Button();
      buttonRectangle.Click += new RoutedEventHandler(buttonRectangle_Click);
      buttonRectangle.Content = "Rectangle";
      stackPanel.Children.Add(buttonRectangle);

      Button buttonSelect = new Button();
      buttonSelect.Click += new RoutedEventHandler(buttonSelect_Click);
      buttonSelect.Content = "Select";
      stackPanel.Children.Add(buttonSelect);

      // Add the viewer
      DockPanel panel = new DockPanel();
      panel.Children.Add(viewer);

      // set panel as content
      this.Content = panel;
   }

   private void buttonSelect_Click(object sender, System.EventArgs e)
   {
      // Select button is clicked
      // cancel any draw designer running
      if(currentDesigner != null && currentDesigner is AnnDrawDesigner)
      {
         AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
         drawDesigner.Cancel();
         currentDesigner = null;
      }
   }

   private void buttonLine_Click(object sender, RoutedEventArgs e)
   {
      // Line button is clicked

      // first end any current designer (if any)
      EndDesigner();

      // start a new Line draw designer
      AnnLineDrawDesigner lineDrawDesigner = new AnnLineDrawDesigner(this.container);

      // set up the object template (a 2 pixels-wide pen)
      AnnLineObject lineObject = new AnnLineObject();
      lineObject.Stroke = Colors.Red;
      lineObject.StrokeThickness = 2.0;
      lineDrawDesigner.ObjectTemplate = lineObject;

      StartDrawDesigner(lineDrawDesigner);
   }


   private void buttonRectangle_Click(object sender, RoutedEventArgs e)
   {
      // Rectangle button is clicked

      // first end any current designer (if any)
      EndDesigner();

      // start a new Rectangle draw designer
      AnnRectangleDrawDesigner rectangleDrawDesigner = new AnnRectangleDrawDesigner(this.container);

      // set up the object template (a 2 pixels-wide pen)
      AnnRectangleObject rectangleObject = new AnnRectangleObject();
      rectangleObject.Stroke = Colors.Red;
      rectangleObject.Fill = Colors.Yellow;
      rectangleObject.StrokeThickness = 2.0;
      rectangleDrawDesigner.ObjectTemplate = rectangleObject;

      StartDrawDesigner(rectangleDrawDesigner);
   }

   private void EndDesigner()
   {
      // ends any running designer
      if(currentDesigner != null)
      {
         if(currentDesigner is AnnEditDesigner)
         {
            AnnEditDesigner editDesigner = currentDesigner as AnnEditDesigner;
            editDesigner.End();
         }
         else if(currentDesigner is AnnDrawDesigner)
         {
            AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
            drawDesigner.Cancel();
         }
      }
   }

   private void StartDrawDesigner(AnnDrawDesigner drawDesigner)
   {
      // set up the current designer
      drawDesigner.Draw += new EventHandler<AnnDrawDesignerEventArgs>(OnDesignerDraw);
      currentDesigner = drawDesigner;
   }

   private void OnDesignerDraw(object sender, AnnDrawDesignerEventArgs e)
   {
      // show information on the current draw operation
      Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name);
      if(e.Object is AnnLineObject)
      {
         AnnLineObject lineObject = e.Object as AnnLineObject;
         Console.WriteLine("Start Point: {{0},{1}}, EndPoint: {{2},{3}}", 
             lineObject.Start.X, lineObject.Start.Y, lineObject.End.X, lineObject.End.Y);
      }
      else if(e.Object is AnnRectangleObject)
      {
         AnnRectangleObject rectangleObject = e.Object as AnnRectangleObject;
         Console.WriteLine("Left: {0}", rectangleObject.Rect.Left);
         Console.WriteLine("Top: {0}", rectangleObject.Rect.Top);
         Console.WriteLine("Width: {0}", rectangleObject.Rect.Width);
         Console.WriteLine("Height: {0}", rectangleObject.Rect.Height);
      }
   }

   void container_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         e = new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice,
            e.Timestamp,
            MouseButton.Left);
         currentDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
      }

      if(!e.Handled)
      {
         // the mouse click was ! handled by a designer
         // check if the click was on top of an existing object that we can start editing
         Point pt = e.GetPosition(container);
         AnnObject obj = container.HitTest(pt);
         if (obj != null)
         {
            // yes, start the edit designer for this object
            if(obj is AnnLineObject)
            {
               AnnLineEditDesigner lineEditDesigner = new AnnLineEditDesigner(obj as AnnLineObject, viewer.Content as AnnContainer);
               StartEditing(lineEditDesigner, obj, pt);
               lineEditDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
            }
            else if(obj is AnnRectangleObject)
            {
               AnnRectangleEditDesigner rectangleEditDesigner = new AnnRectangleEditDesigner(obj as AnnRectangleObject, 
                   viewer.Content as AnnContainer);
               StartEditing(rectangleEditDesigner, obj, pt);
               rectangleEditDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
            }
         }
      }
   }

   private void StartEditing(AnnEditDesigner editDesigner, AnnObject obj, Point pt)
   {
      // first end any running designers
      EndDesigner();

      // set up the current designer
      editDesigner.Edit += new EventHandler<AnnEditDesignerEventArgs>(OnDesignerEdit);
      editDesigner.RotateModifierKey = ModifierKeys.Shift;

      for(int i = 0; i < editDesigner.ControlPointCount; i++)
      {
         // set up the edit designer control points
         System.Windows.Controls.Primitives.Thumb cp = new System.Windows.Controls.Primitives.Thumb();
         cp.Width = cp.Height = 10;;
         cp.BorderBrush = Brushes.Blue;
         cp.BorderThickness = new Thickness(1.0);
         cp.Background = Brushes.White;
         editDesigner.ControlPoints[i] = cp;
      }

      editDesigner.Start();
      currentDesigner = editDesigner;
   }

   private void OnDesignerEdit(object sender, AnnEditDesignerEventArgs e)
   {
      // show information on the current draw operation
      Console.Write("Object: {0}, Operation: {1}, Status: {2}, ", e.Object.GetType().Name, e.Operation, e.OperationStatus);
      if(e.Operation == AnnEditDesignerOperation.MoveControlPoint)
         Console.WriteLine("Control Point index: {0}", e.MoveControlPointIndex);
   }

   void container_MouseMove(object sender, MouseEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         e = new MouseEventArgs(InputManager.Current.PrimaryMouseDevice,
            e.Timestamp);
         currentDesigner.OnMouseMove(sender as AnnContainer,e);
      }
   }

   void container_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         e = new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice,
            e.Timestamp,
            MouseButton.Left);

         currentDesigner.OnMouseLeftButtonUp(sender as AnnContainer, e);
      }
   }
}

public void AnnDesigner_AnnDesigner(string title)
{
   MyWindow1 form = new MyWindow1(title);
   form.ShowDialog();
}


static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
using Leadtools.Windows.Controls;
using Leadtools.Windows.Annotations;
using Leadtools.Examples;

/// Shows how to use designers to create and select annotation objects
class MyWindow1 : ChildWindow
{
   AnnContainer container;
   //AnnAutomationManager manager;
   ImageViewer viewer;
   AnnDesigner currentDesigner;
   public MyWindow1(string title)
   {
      this.Title = title;
      this.Width = 500;
      this.Height = 200;

      viewer = new ImageViewer();

      // load an image into the viewer
      // fix this path to an existing image file on your system
      viewer.Source = new BitmapImage(new Uri(LeadtoolsExamples.Common.ImagesPath.Path + "ScarletMacaws.jpg"));  
      viewer.Width = double.NaN;
      viewer.Height = double.NaN;

      // create and set up the container
      container = new AnnContainer();
      container.Width = ((BitmapSource)viewer.Source).PixelWidth;
      container.Height = ((BitmapSource)viewer.Source).PixelHeight;

      container.MouseLeftButtonDown += new MouseButtonEventHandler(container_MouseLeftButtonDown);
      container.MouseMove += new MouseEventHandler(container_MouseMove);
      container.MouseLeftButtonUp += new MouseButtonEventHandler(container_MouseLeftButtonUp);

      // Set the container as a content of viewer
      viewer.Content = container;

      // Create a panel with three buttons
      StackPanel stackPanel = new StackPanel();

      // Create three buttons
      Button buttonLine = new Button();
      buttonLine.Click += new RoutedEventHandler(buttonLine_Click);
      buttonLine.Content = "Line";
      stackPanel.Children.Add(buttonLine);

      Button buttonRectangle = new Button();
      buttonRectangle.Click += new RoutedEventHandler(buttonRectangle_Click);
      buttonRectangle.Content = "Rectangle";
      stackPanel.Children.Add(buttonRectangle);

      Button buttonSelect = new Button();
      buttonSelect.Click += new RoutedEventHandler(buttonSelect_Click);
      buttonSelect.Content = "Select";
      stackPanel.Children.Add(buttonSelect);

      // Add the viewer
      StackPanel panel = new StackPanel();
      panel.Children.Add(viewer);

      // set panel as content
      this.Content = panel;
   }

   private void buttonSelect_Click(object sender, System.EventArgs e)
   {
      // Select button is clicked
      // cancel any draw designer running
      if(currentDesigner != null && currentDesigner is AnnDrawDesigner)
      {
         AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
         drawDesigner.Cancel();
         currentDesigner = null;
      }
   }

   private void buttonLine_Click(object sender, RoutedEventArgs e)
   {
      // Line button is clicked

      // first end any current designer (if any)
      EndDesigner();

      // start a new Line draw designer
      AnnLineDrawDesigner lineDrawDesigner = new AnnLineDrawDesigner(this.container);

      // set up the object template (a 2 pixels-wide pen)
      AnnLineObject lineObject = new AnnLineObject();
      lineObject.Stroke = Colors.Red;
      lineObject.StrokeThickness = 2.0;
      lineDrawDesigner.ObjectTemplate = lineObject;

      StartDrawDesigner(lineDrawDesigner);
   }


   private void buttonRectangle_Click(object sender, RoutedEventArgs e)
   {
      // Rectangle button is clicked

      // first end any current designer (if any)
      EndDesigner();

      // start a new Rectangle draw designer
      AnnRectangleDrawDesigner rectangleDrawDesigner = new AnnRectangleDrawDesigner(this.container);

      // set up the object template (a 2 pixels-wide pen)
      AnnRectangleObject rectangleObject = new AnnRectangleObject();
      rectangleObject.Stroke = Colors.Red;
      rectangleObject.Fill = Colors.Yellow;
      rectangleObject.StrokeThickness = 2.0;
      rectangleDrawDesigner.ObjectTemplate = rectangleObject;

      StartDrawDesigner(rectangleDrawDesigner);
   }

   private void EndDesigner()
   {
      // ends any running designer
      if(currentDesigner != null)
      {
         if(currentDesigner is AnnEditDesigner)
         {
            AnnEditDesigner editDesigner = currentDesigner as AnnEditDesigner;
            editDesigner.End();
         }
         else if(currentDesigner is AnnDrawDesigner)
         {
            AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
            drawDesigner.Cancel();
         }
      }
   }

   private void StartDrawDesigner(AnnDrawDesigner drawDesigner)
   {
      // set up the current designer
      drawDesigner.Draw += new EventHandler<AnnDrawDesignerEventArgs>(OnDesignerDraw);
      currentDesigner = drawDesigner;
   }

   private void OnDesignerDraw(object sender, AnnDrawDesignerEventArgs e)
   {
      // show information on the current draw operation
      Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name);
      if(e.Object is AnnLineObject)
      {
         AnnLineObject lineObject = e.Object as AnnLineObject;
         Console.WriteLine("Start Point:"+
          "{{0},{1}}, EndPoint: {{2},{3}}", lineObject.Start.X, lineObject.Start.Y, lineObject.End.X, lineObject.End.Y);
      }
      else if(e.Object is AnnRectangleObject)
      {
         AnnRectangleObject rectangleObject = e.Object as AnnRectangleObject;
         Console.WriteLine("Left: {0}", rectangleObject.Rect.Left);
         Console.WriteLine("Top: {0}", rectangleObject.Rect.Top);
         Console.WriteLine("Width: {0}", rectangleObject.Rect.Width);
         Console.WriteLine("Height: {0}", rectangleObject.Rect.Height);
      }
   }

   void container_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         currentDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
      }

      if(!e.Handled)
      {
         // the mouse click was ! handled by a designer
         // check if the click was on top of an existing object that we can start editing
         Point pt = e.GetPosition(container);
         AnnObject obj = container.HitTest(pt);
         if (obj != null)
         {
            // yes, start the edit designer for this object
            if(obj is AnnLineObject)
            {
               AnnLineEditDesigner lineEditDesigner = new AnnLineEditDesigner(obj as AnnLineObject, viewer.Content as AnnContainer);
               StartEditing(lineEditDesigner, obj, pt);
               lineEditDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
            }
            else if(obj is AnnRectangleObject)
            {
               AnnRectangleEditDesigner rectangleEditDesigner = new AnnRectangleEditDesigner(obj as AnnRectangleObject, 
                   viewer.Content as AnnContainer);
               StartEditing(rectangleEditDesigner, obj, pt);
               rectangleEditDesigner.OnMouseLeftButtonDown(sender as AnnContainer, e);
            }
         }
      }
   }

   private void StartEditing(AnnEditDesigner editDesigner, AnnObject obj, Point pt)
   {
      // first end any running designers
      EndDesigner();

      // set up the current designer
      editDesigner.Edit += new EventHandler<AnnEditDesignerEventArgs>(OnDesignerEdit);
      editDesigner.RotateModifierKey = ModifierKeys.Shift;

      for(int i = 0; i < editDesigner.ControlPointCount; i++)
      {
         // set up the edit designer control points
         System.Windows.Controls.Primitives.Thumb cp = new System.Windows.Controls.Primitives.Thumb();
         cp.Width = cp.Height = 10;;
         cp.BorderBrush = new SolidColorBrush(Colors.Blue);
         cp.BorderThickness = new Thickness(1.0);
         cp.Background = new SolidColorBrush(Colors.White);
         editDesigner.ControlPoints[i] = cp;
      }

      editDesigner.Start();
      currentDesigner = editDesigner;
   }

   private void OnDesignerEdit(object sender, AnnEditDesignerEventArgs e)
   {
      // show information on the current draw operation
      Console.Write("Object: {0}, Operation: {1}, Status: {2}, ", e.Object.GetType().Name, e.Operation, e.OperationStatus);
      if(e.Operation == AnnEditDesignerOperation.MoveControlPoint)
         Console.WriteLine("Control Point index: {0}", e.MoveControlPointIndex);
   }

   void container_MouseMove(object sender, MouseEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         currentDesigner.OnMouseMove(sender as AnnContainer,e);
      }
   }

   void container_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
   {
      // see if a designer is currently running, if so, let it handle this event
      if(currentDesigner != null)
      {
         currentDesigner.OnMouseLeftButtonUp(sender as AnnContainer, e);
      }
   }
}

public void AnnDesigner_AnnDesigner(string title)
{
   MyWindow1 form = new MyWindow1(title);
   form.Show();
}
Imports Leadtools.Windows.Controls
Imports Leadtools.Windows.Annotations

''' Shows how to use designers to create and select annotation objects
Private Class MyWindow1 : Inherits ChildWindow
   Private container As AnnContainer
   'AnnAutomationManager manager;
   Private viewer As ImageViewer
   Private currentDesigner As AnnDesigner
   Public Sub New(ByVal title As String)
      Me.Title = title
      Me.Width = 500
      Me.Height = 200

      viewer = New ImageViewer()

      ' load an image into the viewer
      ' fix this path to an existing image file on your system
      viewer.Source = New BitmapImage(New Uri(LeadtoolsExamples.Common.ImagesPath.Path & "ScarletMacaws.jpg"))
      viewer.Width = Double.NaN
      viewer.Height = Double.NaN

      ' create and set up the container
      container = New AnnContainer()
      container.Width = (CType(viewer.Source, BitmapSource)).PixelWidth
      container.Height = (CType(viewer.Source, BitmapSource)).PixelHeight

      AddHandler container.MouseLeftButtonDown, AddressOf container_MouseLeftButtonDown
      AddHandler container.MouseMove, AddressOf container_MouseMove
      AddHandler container.MouseLeftButtonUp, AddressOf container_MouseLeftButtonUp

      ' Set the container as a content of viewer
      viewer.Content = container

      ' Create a panel with three buttons
      Dim stackPanel As StackPanel = New StackPanel()

      ' Create three buttons
      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)

      ' Add the viewer
      Dim panel As StackPanel = New StackPanel()
      panel.Children.Add(viewer)

      ' set panel as content
      Me.Content = panel
   End Sub

   Private Sub buttonSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      ' Select button is clicked
      ' cancel any draw designer running
      If Not currentDesigner Is Nothing AndAlso TypeOf currentDesigner Is AnnDrawDesigner Then
         Dim drawDesigner As AnnDrawDesigner = TryCast(currentDesigner, AnnDrawDesigner)
         drawDesigner.Cancel()
         currentDesigner = Nothing
      End If
   End Sub

   Private Sub buttonLine_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      ' Line button is clicked

      ' first end any current designer (if any)
      EndDesigner()

      ' start a new Line draw designer
      Dim lineDrawDesigner As AnnLineDrawDesigner = New AnnLineDrawDesigner(Me.container)

      ' set up the object template (a 2 pixels-wide pen)
      Dim lineObject As AnnLineObject = New AnnLineObject()
      lineObject.Stroke = Colors.Red
      lineObject.StrokeThickness = 2.0
      lineDrawDesigner.ObjectTemplate = lineObject

      StartDrawDesigner(lineDrawDesigner)
   End Sub


   Private Sub buttonRectangle_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      ' Rectangle button is clicked

      ' first end any current designer (if any)
      EndDesigner()

      ' start a new Rectangle draw designer
      Dim rectangleDrawDesigner As AnnRectangleDrawDesigner = New AnnRectangleDrawDesigner(Me.container)

      ' set up the object template (a 2 pixels-wide pen)
      Dim rectangleObject As AnnRectangleObject = New AnnRectangleObject()
      rectangleObject.Stroke = Colors.Red
      rectangleObject.Fill = Colors.Yellow
      rectangleObject.StrokeThickness = 2.0
      rectangleDrawDesigner.ObjectTemplate = rectangleObject

      StartDrawDesigner(rectangleDrawDesigner)
   End Sub

   Private Sub EndDesigner()
      ' ends any running designer
      If Not currentDesigner Is Nothing Then
         If TypeOf currentDesigner Is AnnEditDesigner Then
            Dim editDesigner As AnnEditDesigner = TryCast(currentDesigner, AnnEditDesigner)
            editDesigner.End()
         Else If TypeOf currentDesigner Is AnnDrawDesigner Then
            Dim drawDesigner As AnnDrawDesigner = TryCast(currentDesigner, AnnDrawDesigner)
            drawDesigner.Cancel()
         End If
      End If
   End Sub

   Private Sub StartDrawDesigner(ByVal drawDesigner As AnnDrawDesigner)
      ' set up the current designer
      AddHandler drawDesigner.Draw, AddressOf OnDesignerDraw
      currentDesigner = drawDesigner
   End Sub

   Private Sub OnDesignerDraw(ByVal sender As Object, ByVal e As AnnDrawDesignerEventArgs)
      ' show information on the current draw operation
      Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name)
      If TypeOf e.Object Is AnnLineObject Then
         Dim lineObject As AnnLineObject = TryCast(e.Object, AnnLineObject)
         Console.WriteLine("Start Point: {{0},{1}}, EndPoint: {{2},{3}}",
          lineObject.Start.X, lineObject.Start.Y, lineObject.End.X, lineObject.End.Y)
      Else If TypeOf e.Object Is AnnRectangleObject Then
         Dim rectangleObject As AnnRectangleObject = TryCast(e.Object, AnnRectangleObject)
         Console.WriteLine("Left: {0}", rectangleObject.Rect.Left)
         Console.WriteLine("Top: {0}", rectangleObject.Rect.Top)
         Console.WriteLine("Width: {0}", rectangleObject.Rect.Width)
         Console.WriteLine("Height: {0}", rectangleObject.Rect.Height)
      End If
   End Sub

   Private Sub container_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         currentDesigner.OnMouseLeftButtonDown(TryCast(sender, AnnContainer), e)
      End If

      If (Not e.Handled) Then
         ' the mouse click was ! handled by a designer
         ' check if the click was on top of an existing object that we can start editing
         Dim pt As Point = e.GetPosition(container)
         Dim obj As AnnObject = container.HitTest(pt)
         If Not obj Is Nothing Then
            ' yes, start the edit designer for this object
            If TypeOf obj Is AnnLineObject Then
               Dim lineEditDesigner As AnnLineEditDesigner = New AnnLineEditDesigner(TryCast(obj, AnnLineObject),
                  TryCast(viewer.Content, AnnContainer))
               StartEditing(lineEditDesigner, obj, pt)
               lineEditDesigner.OnMouseLeftButtonDown(TryCast(sender, AnnContainer), e)
            Else If TypeOf obj Is AnnRectangleObject Then
               Dim rectangleEditDesigner As AnnRectangleEditDesigner = New AnnRectangleEditDesigner(TryCast(obj, AnnRectangleObject), _
                                                                                                    TryCast(viewer.Content, AnnContainer))
               StartEditing(rectangleEditDesigner, obj, pt)
               rectangleEditDesigner.OnMouseLeftButtonDown(TryCast(sender, AnnContainer), e)
            End If
         End If
      End If
   End Sub

   Private Sub StartEditing(ByVal editDesigner As AnnEditDesigner, ByVal obj As AnnObject, ByVal pt As Point)
      ' first end any running designers
      EndDesigner()

      ' set up the current designer
      AddHandler editDesigner.Edit, AddressOf OnDesignerEdit
      editDesigner.RotateModifierKey = ModifierKeys.Shift

      Dim i As Integer = 0
      Do While i < editDesigner.ControlPointCount
         ' set up the edit designer control points
         Dim cp As System.Windows.Controls.Primitives.Thumb = New System.Windows.Controls.Primitives.Thumb()
         cp.Width = 10
         cp.Height = 10

         cp.BorderBrush = New SolidColorBrush(Colors.Blue)
         cp.BorderThickness = New Thickness(1.0)
         cp.Background = New SolidColorBrush(Colors.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)
      ' show information on the current draw operation
      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)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         currentDesigner.OnMouseMove(TryCast(sender, AnnContainer),e)
      End If
   End Sub

   Private Sub container_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
      ' see if a designer is currently running, if so, let it handle this event
      If Not currentDesigner Is Nothing Then
         currentDesigner.OnMouseLeftButtonUp(TryCast(sender, AnnContainer), e)
      End If
   End Sub
End Class

Public Sub AnnDesigner_AnnDesigner(ByVal title As String)
   Dim form As MyWindow1 = New MyWindow1(title)
   form.Show()
End Sub
Requirements

Target Platforms

See Also

Reference

AnnDesigner Members
Leadtools.Windows.Annotations Namespace

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.