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
ImageViewer
so 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 theAnnEllipseObject
but 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 theOnPointerDown
andOnPointerMove
events so that I can control the ellipse and force it into a perfect circle.In the
OnPointerDown
event I set the begin and end points, and in theOnPointerMove
event 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
AnnCircleObject
to be modified and changed to an oval or ellipse, so I have created a customObjectRenderer
based on theAnnEllipseObjectRenderer
and overrode theRenderThumbs
event 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 otherAnnNumberObjects
in the sameAnnContainer
.I based the
AnnNumberObject
on theAnnTextObject
and its Draw Designer on theAnnRectangleDrawDesigner
. In the Draw Designer I overrode theOnPointerDown
event so that it will cycle through each object in theAnnContainer
and increment a counter by 1 for each object it finds, setting theText
property of theNumberObject
to that value.For the
NumberObject
, I also had to create a custom Edit Designer which I based on theAnnTextEditDesigner
so that I can have the font size change as the size of the object changes. To do this, I overrode theOnPointerDown
event to capture the starting values for theNumberObject
's bounds and font size, then I overrode theOnEdit
event 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
AnnStarObject
based 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 theOnPointerDown
to set the center of the star then I overrode theOnPointerMove
event 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
AnnTinyStampObject
which is based on theAnnStampObject
. This object is very similar to theStampObject
except 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 theOnPointerDown
event 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 theOnPointerUp
event.After all my objects were created, I had to add them to my
AnnAutomationManager.AutomationObject
list so that the user can access and use them. I created a method to handle adding the objects, as well as setting theToolbarImage
to 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
AnnotationDemo
to 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.