Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / Windows Forms

BizDraw framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (21 votes)
30 May 20075 min read 127.8K   3.2K   102  
A small framework to design and print documents containing shapes, text, images, bar codes...
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
namespace BizDraw.Objects
{
    [System.Runtime.InteropServices.ComVisible(true)]
    [Serializable()]
	/// <summary>
	/// Base class for all draw objects
	/// </summary>
	public abstract class DrawObject: ICloneable
	{
        #region Members
        private bool cut;
        internal GraphicsList Parent;
        // Object properties
        private bool selected;
        private Color color = Color.Black ;
        private int penWidth;

        // Last used property values (may be kept in the Registry)
        [NonSerialized()]
        private static Color lastUsedColor = Color.Black;
        [NonSerialized()]
        private static int lastUsedPenWidth = 1;
        #endregion
        public virtual Editors.IDrawObjectEditor GetEditor()
        {
            return null;
        }
        #region Properties
        /// <summary>
        /// Selection flag
        /// </summary>
        public bool Selected
        {
            get
            {
                return selected;
            }
        }
        public void SetSelectedState(bool value)
        {
            if (value != selected)
            {
                selected = value;
                OnSelectChanged(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// Color
        /// </summary>
        public Color Color
        {
            get
            {
                if (cut)
                    return Color.Gray;
                else 
                    return color;
            }
            set
            {
                color = value;
            }
        }
        /// <summary>
        /// Pen width
        /// </summary>
        public int PenWidth
        {
            get
            {
                return penWidth;
            }
            set
            {
                penWidth = value;
            }
        }
        /// <summary>
        /// Number of handles
        /// </summary>
        public virtual int HandleCount
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// Last used color
        /// </summary>
        public static Color LastUsedColor
        {
            get
            {
                return lastUsedColor;
            }
            protected set
            {
                lastUsedColor = value;
            }
        }
        /// <summary>
        /// Last used pen width
        /// </summary>
        public static int LastUsedPenWidth
        {
            get
            {
                return lastUsedPenWidth;
            }
            protected set
            {
                lastUsedPenWidth = value;
            }
        }
        public bool Cut
        {
            get { return cut; }
        }
        public void SetCutState(bool value)
        {
            cut = value;
        }
        #endregion

        #region Virtual Functions

        /// <summary>
        /// Draw object
        /// </summary>
        /// <param name="g"></param>
        public virtual void Draw(Graphics g)
        {
        }
        /// <summary>
        /// Get handle point by 1-based number
        /// </summary>
        /// <param name="handleNumber"></param>
        /// <returns></returns>
        public virtual Point GetHandle(int handleNumber)
        {
            return new Point(0, 0);
        }

        /// <summary>
        /// Get handle rectangle by 1-based number
        /// </summary>
        /// <param name="handleNumber"></param>
        /// <returns></returns>
        public virtual Rectangle GetHandleRectangle(int handleNumber)
        {
            Point point = GetHandle(handleNumber);

            return new Rectangle(point.X - 3, point.Y - 3, 7, 7);
        }

        /// <summary>
        /// Draw tracker for selected object
        /// </summary>
        /// <param name="g"></param>
        public virtual void DrawTracker(Graphics g)
        {
            if ( ! Selected )
                return;

            SolidBrush brush = new SolidBrush(Color.Black);

            for ( int i = 1; i <= HandleCount; i++ )
            {
                g.FillRectangle(brush, GetHandleRectangle(i));
            }

            brush.Dispose();
        }

        /// <summary>
        /// Hit test.
        /// Return value: -1 - no hit
        ///                0 - hit anywhere
        ///                > 1 - handle number
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public virtual int HitTest(Point point)
        {
            return -1;
        }


        /// <summary>
        /// Test whether point is inside of the object
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        protected virtual bool PointInObject(Point point)
        {
            return false;
        }
        

        /// <summary>
        /// Get curesor for the handle
        /// </summary>
        /// <param name="handleNumber"></param>
        /// <returns></returns>
        public virtual Cursor GetHandleCursor(int handleNumber)
        {
            return Cursors.Default;
        }

        /// <summary>
        /// Test whether object intersects with rectangle
        /// </summary>
        /// <param name="rectangle"></param>
        /// <returns></returns>
        public virtual bool IntersectsWith(Rectangle rectangle)
        {
            return false;
        }

        /// <summary>
        /// Move object
        /// </summary>
        /// <param name="deltaX"></param>
        /// <param name="deltaY"></param>
        public virtual void Move(int deltaX, int deltaY)
        {
        }

        /// <summary>
        /// Move handle to the point
        /// </summary>
        /// <param name="point"></param>
        /// <param name="handleNumber"></param>
        public virtual void MoveHandleTo(Point point, int handleNumber)
        {
        }

        /// <summary>
        /// Dump (for debugging)
        /// </summary>
        public virtual void Dump()
        {
            Trace.WriteLine("");
            Trace.WriteLine(this.GetType().Name);
            Trace.WriteLine("Selected = " + selected.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// Normalize object.
        /// Call this function in the end of object resizing.
        /// </summary>
        public virtual void Normalize()
        {
        }
        #endregion

        #region Other functions

        /// <summary>
        /// Initialization
        /// </summary>
        protected void Initialize()
        {
            color = lastUsedColor;
            penWidth = LastUsedPenWidth;
        }
        #endregion
        #region events
        public event EventHandler SelectChanged;
        protected void OnSelectChanged(object sender, EventArgs e)
        {
            if (SelectChanged != null)
                SelectChanged(sender, e);
        }
        #endregion

        #region ICloneable Members

        public virtual object Clone()
        {
            return base.MemberwiseClone();
        }
        #endregion
    }

}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
MCSD Asp.Net certified developer

Comments and Discussions