As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD's depth of features and ease of use. Today's project comes from Hadi.
What it Does
This C# application will create several custom annotations using LEADTOOLS Version 19.
Features Used
Development Progress Journal
Hello, my name is Hadi and I am writing a sample application that will demonstrate the various capabilities of the LEADTOOLS Annotation SDK, more specifically, how to create Custom Annotations and Designers.
My plan is to start off by writing a simple project that uses the default Annotations and
ImageViewerso that I have a base to start with. The WinForms Annotations demo located in the \Examples\DotNet\CS\AnnotationsDemo directory will work perfectly.Now that I have the sample project created, I will start with my first custom object, the
AnnCircleObject. I want this custom object to be very similar to theAnnEllipseObjectbut instead of an oval, it will be a perfect circle.To get started, I created new directories to keep things organized and will start by creating the new object which is based on the Ellipse.
Next, I want to control how the object is drawn, so I will need to create my own custom Draw Designer based on the
AnnEllipseDrawDesigner. I will override theOnPointerDownandOnPointerMoveevents so that I can control the ellipse and force it into a perfect circle.In the
OnPointerDownevent I set the begin and end points, and in theOnPointerMoveevent I do some math to get the width and height of the drawing rectangle so I can force the circle to be the same width and height, therefore creating a perfect circle.I also don't want the new
AnnCircleObjectto be modified and changed to an oval or ellipse, so I have created a customObjectRendererbased on theAnnEllipseObjectRendererand overrode theRenderThumbsevent to not render the top, left, right and bottom resize thumbs so the user can't modify the height or width individually.Next I will create a text-based custom object, the
AnnNumberObject. This object will be a custom text-based Annotation object that will, when drawn, count and display the number of otherAnnNumberObjectsin the sameAnnContainer.I based the
AnnNumberObjecton theAnnTextObjectand its Draw Designer on theAnnRectangleDrawDesigner. In the Draw Designer I overrode theOnPointerDownevent so that it will cycle through each object in theAnnContainerand increment a counter by 1 for each object it finds, setting theTextproperty of theNumberObjectto that value.For the
NumberObject, I also had to create a custom Edit Designer which I based on theAnnTextEditDesignerso that I can have the font size change as the size of the object changes. To do this, I overrode theOnPointerDownevent to capture the starting values for theNumberObject's bounds and font size, then I overrode theOnEditevent to do some math to get a ratio to see how the width was being edited and forces the font size to change with it.The third object I created was the
AnnStarObjectbased on theAnnPolylineObject. This object will let the user click and drag their mouse to set the center and radius of a perfect gold star. To achieve this, I created a custom Draw Designer based on theAnnPolylineDrawDesigner. I overrode theOnPointerDownto set the center of the star then I overrode theOnPointerMoveevent and did the math necessary to create the perfect star. I calculated the distance between the center point and the current location of the mouse to generate the radius, then used a formula to find the 10 points of the star and add them to the object.The final object added was the
AnnTinyStampObjectwhich is based on theAnnStampObject. This object is very similar to theStampObjectexcept I have it draw the stamp on a single click. To achieve this, I created a custom Draw Designer which isn't based on another object's draw designer. I overrode theOnPointerDownevent and calculated the 4 points of the rectangle based on the mouse location and added those to the tiny stamp object and ended the designer in theOnPointerUpevent.After all my objects were created, I had to add them to my
AnnAutomationManager.AutomationObjectlist so that the user can access and use them. I created a method to handle adding the objects, as well as setting theToolbarImageto make it easy for the user to find the object in the toolbar.Overall this project took me less than a day to write since I had the
AnnotationDemoto get started with and a helpful tutorial within the LEADTOOLS documentation about implementing custom annotations located here:Documentation: Implementing User-Defined Objects With LEADTOOLS Annotations in Windows Forms
Download the Project
The source code for this sample project can be downloaded from here. To run the project, extract it to the C:\LEADTOOLS 19\Examples\DotNet\CS directory.