Start with the project created in Draw and Edit Annotations on Images.
The object being created is a simple triangle. This object will have three points for the end points of the triangle and it will use a stroke for the triangle edges and a fill for the interior.
Add a new C# class to your project and add the following code:
// The Triangle annotation objectpublic class AnnTriangleObject : AnnPolylineObject{// The object ID, let us select a user defined valuepublic const int MyId = AnnObject.UserObjectId;public AnnTriangleObject() :base(){// Set an IDSetId(MyId);// We are a closed-figureIsClosed = true;}// Our friendly namepublic override string FriendlyName{get{return "Triangle";}}}
Since there are is no existing draw designer that can be used with our AnnTriangleObject class, we need to create our own:
// Our draw designerpublic class AnnTriangleDrawDesigner : AnnDrawDesigner{public AnnTriangleDrawDesigner(IAnnAutomationControl automationControl, AnnContainer container, AnnObject annObject) :base(automationControl, container, annObject){}// Override the Pointer Down event and add 3 points for our trianglepublic override bool OnPointerDown(AnnContainer sender, AnnPointerEventArgs e){// See if the base class wants to handle the eventbool handled = base.OnPointerDown(sender, e);if (handled)return true;// We will work on left button onlyif (e.Button != AnnMouseButton.Left)return false;// Get the current number of points in the objectAnnObject annObject = this.TargetObject;int pointCount = annObject.Points.Count;if (pointCount < 3){// Add the current pointannObject.Points.Add(e.Location);// If we had 0 points, then add another one. When we move the pointer, we will// move the last point (second in this case)if (pointCount == 0)annObject.Points.Add(e.Location);if (pointCount == 0){if (!this.StartWorking())return true;}}else{// We have 3 points, we are finishedthis.EndWorking();}return true;}// Override the Pointer Move eventpublic override bool OnPointerMove(AnnContainer sender, AnnPointerEventArgs e){bool handled = base.OnPointerMove(sender, e);// See if we have points// If so, move the last point in the object to this new locationAnnObject annObject = this.TargetObject;if (annObject.Points.Count > 0){annObject.Points[annObject.Points.Count - 1] = e.Location;Working();return true;}return false;}public override bool OnPointerUp(AnnContainer sender, AnnPointerEventArgs e){base.OnPointerUp(sender, e);return true;}}
Next, we need to add the automation object for the Triangle:
private static void InitializeTriangleObject(AnnAutomationManagerHelper annotations){// Create a new automation objectAnnAutomationObject automationObj = new AnnAutomationObject();// Set the object IDautomationObj.Id = AnnTriangleObject.MyId;automationObj.Name = "Triangle";// Set its designersautomationObj.DrawDesignerType = typeof(AnnTriangleDrawDesigner);automationObj.EditDesignerType = typeof(AnnPolylineEditDesigner);automationObj.RunDesignerType = typeof(AnnRunDesigner);// Set the templateAnnTriangleObject annObj = new AnnTriangleObject();// Set our default strokeannObj.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("red"), LeadLengthD.Create(2));annObj.Fill = AnnSolidColorBrush.Create("yellow");automationObj.ObjectTemplate = annObj;// Set our renderer, same as the AnnPolylineObjectIAnnObjectRenderer polylineRenderer = annotations.AutomationManager.RenderingEngine.Renderers[AnnObject.PolylineObjectId];IAnnObjectRenderer renderer = new AnnPolylineObjectRenderer();renderer.LabelRenderer = polylineRenderer.LabelRenderer;renderer.LocationsThumbStyle = polylineRenderer.LocationsThumbStyle;renderer.RotateGripperThumbStyle = polylineRenderer.RotateGripperThumbStyle;renderer.RotateCenterThumbStyle = polylineRenderer.RotateCenterThumbStyle;annotations.AutomationManager.RenderingEngine.Renderers[AnnTriangleObject.MyId] = renderer;// Set its context menu and toolbar imageAnnAutomationObjectExtData extData = new AnnAutomationObjectExtData();extData.ContextMenu = new Leadtools.Annotations.WinForms.ObjectContextMenu();extData.DrawCursor = Cursors.Cross;Bitmap bitmap = new Bitmap(24, 24, System.Drawing.Imaging.PixelFormat.Format32bppArgb);using (Graphics g = Graphics.FromImage(bitmap)){g.Clear(Color.Transparent);g.DrawLine(Pens.Black, 12, 2, 22, 22);g.DrawLine(Pens.Black, 22, 22, 2, 22);g.DrawLine(Pens.Black, 2, 22, 12, 2);}extData.ToolBarImage = bitmap;extData.ToolBarToolTipText = "Triangle";automationObj.UserData = extData;annotations.AutomationManager.Objects.Add(automationObj);}
And finally, modify the original code like this:
// Create the helper_annotations = new AnnAutomationManagerHelper(automationManager);// --------------------------// Create our triangle automation objectInitializeTriangleObject(_annotations);// --------------------------// Create the toolbar and add it to our form_annotations.CreateToolBar();ToolBar toolBar = _annotations.ToolBar;