Start with the project created in Working with Automated Annotations in Windows Forms.
The object being created is a simple object, a 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:
[C#]
// 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:
[C#]
// 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;
}
// Ovveride the Pointer Move event
public override bool OnPointerMove(AnnContainer sender, AnnPointerEventArgs e)
{
bool handled = base.OnPointerMove(sender, e);
// See if we are have points
// Yes, 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:
[C#]
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.Automation.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 the original code like this:
[C#]
// 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;
Build and run the demo. Now you can click the triangle toolbar icon and draw instance of the new object.