Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Simple SVG Editor

Rate me:
Please Sign up or sign in to vote.
4.38/5 (18 votes)
18 Sep 2007CPOL1 min read 168.1K   12.6K   92  
This application is a combination of two projects from The Code Project: DrawTools by Alex Fry and SVGPad by Maurizio Bigoloni
using System;
using System.Windows.Forms;
using System.Drawing;

using SVGLib;
using Draw;

namespace DrawTools
{
	/// <summary>
	/// Polygon tool
	/// </summary>
	public class ToolPolygon : DrawTools.ToolObject
	{
		public ToolPolygon()
		{
            Cursor = new Cursor(GetType(), "Pencil.cur");
        }

        private int lastX;
        private int lastY;
        private DrawPolygon newPolygon;
        private const int minDistance = 15*15;

        /// <summary>
        /// Left nouse button is pressed
        /// </summary>
        /// <param name="drawArea"></param>
        /// <param name="e"></param>
        public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
        {
            // Create new polygon, add it to the list
            // and keep reference to it
            newPolygon = new DrawPolygon(e.X, e.Y, e.X + 1, e.Y + 1);
            AddNewObject(drawArea, newPolygon);
            lastX = e.X;
            lastY = e.Y;
        }

        /// <summary>
        /// Mouse move - resize new polygon
        /// </summary>
        /// <param name="drawArea"></param>
        /// <param name="e"></param>
        public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
        {
            drawArea.Cursor = Cursor;

            if ( e.Button != MouseButtons.Left )
                return;

            if ( newPolygon == null )
                return;                 // precaution

            Point point = new Point(e.X, e.Y);
            int distance = (e.X - lastX)*(e.X - lastX) + (e.Y - lastY)*(e.Y - lastY);
			try
			{
				if ( distance < minDistance )
				{
					// Distance between last two points is less than minimum -
					// move last point
					newPolygon.MoveHandleTo(point, newPolygon.HandleCount);
				}
				else
				{
					// Add new point
					newPolygon.AddPoint(point);
					lastX = e.X;
					lastY = e.Y;
				}
				drawArea.Refresh();
			} 
			catch(Exception ex)
			{
				ErrH.Log("ToolPolygon", "OnMouse", ex.ToString(), ErrH._LogPriority.Info);
			}
        }

        public override void OnMouseUp(DrawArea drawArea, MouseEventArgs e)
        {
            newPolygon = null;

            base.OnMouseUp (drawArea, e);
        }


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions