Start with the project created in Working with Automated Annotations in Windows Forms.
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 object
public class AnnTriangleObject : AnnPolylineObject
{
// The object ID, let us select a user defined value
public const int MyId = AnnObject.UserObjectId;
public AnnTriangleObject() :
base()
{
// Set an ID
SetId(MyId);
// We are a closed-figure
IsClosed = true;
}
// Our friendly name
public 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 designer
public 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 triangle
public override bool OnPointerDown(AnnContainer sender, AnnPointerEventArgs e)
{
// See if the base class wants to handle the event
bool handled = base.OnPointerDown(sender, e);
if (handled)
return true;
// We will work on left button only
if (e.Button != AnnMouseButton.Left)
return false;
// Get the current number of points in the object
AnnObject annObject = this.TargetObject;
int pointCount = annObject.Points.Count;
if (pointCount < 3)
{
// Add the current point
annObject.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 finished
this.EndWorking();
}
return true;
}
// Override the Pointer Move event
public 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 location
AnnObject 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 object
AnnAutomationObject automationObj = new AnnAutomationObject();
// Set the object ID
automationObj.Id = AnnTriangleObject.MyId;
automationObj.Name = "Triangle";
// Set its designers
automationObj.DrawDesignerType = typeof(AnnTriangleDrawDesigner);
automationObj.EditDesignerType = typeof(AnnPolylineEditDesigner);
automationObj.RunDesignerType = typeof(AnnRunDesigner);
// Set the template
AnnTriangleObject annObj = new AnnTriangleObject();
// Set our default stroke
annObj.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("red"), LeadLengthD.Create(2));
annObj.Fill = AnnSolidColorBrush.Create("yellow");
automationObj.ObjectTemplate = annObj;
// Set our renderer, same as the AnnPolylineObject
IAnnObjectRenderer 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 image
AnnAutomationObjectExtData 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 object
InitializeTriangleObject(_annotations);
// --------------------------
// Create the toolbar and add it to our form
_annotations.CreateToolBar();
ToolBar toolBar = _annotations.ToolBar;
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.