Implementing User-Defined Objects With LEADTOOLS Annotations in WinRT (Windows Store)

Show in webframe

Take the following steps to start a project and to add some code that will demonstrate the custom annotation features of the LEADTOOLS WinRT Annotations.

  1. Start Visual Studio .NET 2012.
  2. Start with the project that you created in Working With Automated Annotations in WinRT.
  3. Switch to MainPage.xaml code view (right-click MainPage.xaml in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    [C#]

    
                Leadtools.Annotations.Designers;
                
    
  4. Update the MainPage() function as shown below:

    [C#]

    
                public MainPage()
                {
                   this.InitializeComponent();
                   RasterSupport.Initialize();
                   AnnAutomationManager manager = new AnnAutomationManager();
                   manager.CreateDefaultObjects();
                   AutomationControl viewerControl = new AutomationControl(_viewer);
                   _automation = new AnnAutomation(manager, viewerControl);
                   _rbDesignMode.IsChecked = true;
                   _automation.Active = true;
                
                   AddItem(_annObjects, AnnObject.SelectObjectId);
                   AddItem(_annObjects, AnnObject.PointerObjectId);
                   AddItem(_annObjects, AnnObject.RectangleObjectId);
                   AddItem(_annObjects, AnnObject.TextObjectId);
                   AddItem(_annObjects, AnnObject.RulerObjectId);
                   _annObjects.SelectedIndex = 0;
                
                   AnnTriangleObject triangle = new AnnTriangleObject();
                   // Create user defined automation object
                   AnnAutomationObject triangleAutomation = CreateTriangleAutomationObject(manager, triangle);
                   _automation.Manager.Objects.Add(triangleAutomation);
                
                   AddItem(_annObjects, triangleAutomation.Id);
                }
                
    
  5. Add the following class functions:

    [C#]

    
                private AnnAutomationObject CreateTriangleAutomationObject(AnnAutomationManager manager, AnnObject annObject)
                {
                   AnnAutomationObject automationObj = new AnnAutomationObject();
                   automationObj.Id = annObject.Id;
                   automationObj.Name = "Triangle";
                   automationObj.DrawDesignerType = typeof(AnnTriangleDrawDesigner); // hook the custom draw designer
                   automationObj.EditDesignerType = typeof(AnnPolylineEditDesigner); // hook the custom edit designer
                   automationObj.RunDesignerType = typeof(AnnRunDesigner);
                
                   AnnTriangleRenderer annTriangleRenderer = new AnnTriangleRenderer();
                   IAnnObjectRenderer annPolylineRenderer = manager.Renderers[AnnObject.PolylineObjectId];
                
                   annTriangleRenderer.LocationsThumbStyle = annPolylineRenderer.LocationsThumbStyle;
                   annTriangleRenderer.RotateCenterThumbStyle = annPolylineRenderer.RotateCenterThumbStyle;
                   annTriangleRenderer.RotateGripperThumbStyle = annPolylineRenderer.RotateGripperThumbStyle;
                
                   AnnRenderingEngine.Renderers[annObject.Id] = annTriangleRenderer; // hook the custom renderer
                   automationObj.ObjectTemplate = annObject;
                   return automationObj;
                }
                
    
  6. In the "Solution Explorer" window, right-click on the "Project Name" and select "Add" from the context menu then select "New Item...". In the "Add New Item" dialog box, select Classand Name it TriangleObject from Code tree view item in Visual C#

    Click the Add button to add the above file to the application.

  7. Open TriangleObject.cs code view (right-click TriangleObject.cs in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    [C#]

    
                using Leadtools;
                using Leadtools.Annotations.Rendering;
                using Windows.UI.Xaml;
                using Windows.UI.Xaml.Media;
                using Leadtools.Annotations.Core;
                using Windows.Foundation;
                using Windows.UI.Xaml.Shapes;
                using Windows.UI;
                using Leadtools.Annotations.Designers;
                
    
  8. Open TriangleObject.cs code view (right-click TriangleObject.cs in the solution explorer then select View Code) and replace class TriangleObject with:

    [C#]

    
                public class AnnTriangleRenderer : AnnPolylineObjectRenderer
                {
                   private LeadPointD[] _points;
                   private GeometryGroup _group = new GeometryGroup();
                   private AnnObject _annObject;
                
                   public override List<FrameworkElement> VisualComponents
                   {
                      get
                      {
                         List<FrameworkElement> _visualComponents = base.VisualComponents;
                
                         if (_group.Children.Count < 2)
                         {
                            _group.Children.Add(new EllipseGeometry());
                            _group.Children.Add(new EllipseGeometry());
                         }
                
                         if (_points.Length > 0)
                         {
                            EllipseGeometry ellipse = (EllipseGeometry)_group.Children[0];
                            ellipse.RadiusX = 10;
                            ellipse.RadiusY = 10;
                            ellipse.Center = new Point(_points[0].X, _points[0].Y);
                            if (_points.Length == 2)
                            {
                               ellipse = (EllipseGeometry)_group.Children[1];
                               ellipse.RadiusX = 10;
                               ellipse.RadiusY = 10;
                               ellipse.Center = new Point(_points[1].X, _points[1].Y);
                            }
                         }
                
                         if (_visualComponents.Count == 1)
                         {
                            _visualComponents.Add(new Path());
                         }
                
                         Path path = (Path)_visualComponents[1];
                         // if we are finished 'drawing', allow the base class AnnPolylineObjectRenderer to handle the job
                         if (_annObject.Tag == null || "drawing".CompareTo((string)_annObject.Tag) != 0)
                         {
                            path.Data = null;
                            _group.Children.Clear();
                         }
                         else
                         {
                            path.Data = _group;
                         }
                         path.Stroke = new SolidColorBrush(Colors.Green);
                         path.StrokeThickness = 2;
                
                         return _visualComponents;
                      }
                
                   }
                   public override void Render(AnnContainerMapper mapper, AnnObject annObject)
                   {
                      _annObject = annObject;
                      _points = mapper.PointsFromContainerCoordinates(annObject.Points.ToArray(), annObject.FixedStateOperations);
                
                      base.Render(mapper, annObject);
                   }
                }
                
                public class AnnTriangleDrawDesigner : AnnDrawDesigner
                {
                   public AnnTriangleDrawDesigner(IAnnAutomationControl automationControl, AnnContainer container, AnnObject annObject)
                      : base(automationControl, container, annObject)
                   {
                   }
                
                   // override the onPointerDown method and add 3 points for our triangle
                   public override bool OnPointerDown(AnnContainer container, AnnPointerEventArgs e)
                   {
                      bool handled = base.OnPointerDown(container, e);
                      AnnObject targetObject = this.TargetObject;
                      if (targetObject.Points.Count < 3)
                      {
                         targetObject.Tag = "drawing";
                         this.StartWorking();
                         targetObject.Points.Add(e.Location);
                         handled = true;
                      }
                
                      return handled;
                   }
                
                   // override the onPointerUp method and end the drawing when we have our 3 points
                   public override bool OnPointerUp(AnnContainer container, AnnPointerEventArgs e)
                   {
                      bool handled = base.OnPointerUp(container, e);
                      handled = true;
                      AnnObject targetObject = this.TargetObject;
                      if (targetObject.Points.Count >= 3)
                      {
                         targetObject.Tag = null;
                         this.EndWorking();
                      }
                      return handled;
                   }
                }
                
                public class AnnTriangleObject : AnnPolylineObject
                {
                   public AnnTriangleObject()
                      : base()
                   {
                      this.IsClosed = true; // triangle is a closed figure
                      this.SetId(-99); // set the object id
                      this.Tag = null;
                   }
                
                   protected override AnnObject Create()
                   {
                      return new AnnTriangleObject();
                   }
                
                   public override string FriendlyName
                   {
                      get
                      {
                         return "Triangle";
                      }
                   }
                }
                
    
  9. Build, and Run the program to test it.

    Use the combo box below the viewer to select annotations. The radio buttons will allow you to switch between design and run mode.

 

 


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