DrawObject.cs
上传用户:sxsgcs
上传日期:2013-10-21
资源大小:110k
文件大小:9k
源码类别:

CAD

开发平台:

C#

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Runtime.Serialization;
  7. namespace DrawTools
  8. {
  9. /// <summary>
  10. /// Base class for all draw objects
  11. /// </summary>
  12. abstract class DrawObject
  13. {
  14.         #region Members
  15.         // Object properties
  16.         private bool selected;
  17.         private Color color;
  18.         private int penWidth;
  19.         // Allows to write Undo - Redo functions and don't care about
  20.         // objects order in the list.
  21.         int id;   
  22.         // Last used property values (may be kept in the Registry)
  23.         private static Color lastUsedColor = Color.Black;
  24.         private static int lastUsedPenWidth = 1;
  25.         // Entry names for serialization
  26.         private const string entryColor = "Color";
  27.         private const string entryPenWidth = "PenWidth";
  28.         #endregion
  29.         public DrawObject()
  30.         {
  31.             id = this.GetHashCode();
  32.         }
  33.         #region Properties
  34.         /// <summary>
  35.         /// Selection flag
  36.         /// </summary>
  37.         public bool Selected
  38.         {
  39.             get
  40.             {
  41.                 return selected;
  42.             }
  43.             set
  44.             {
  45.                 selected = value;
  46.             }
  47.         }
  48.         /// <summary>
  49.         /// Color
  50.         /// </summary>
  51.         public Color Color
  52.         {
  53.             get
  54.             {
  55.                 return color;
  56.             }
  57.             set
  58.             {
  59.                 color = value;
  60.             }
  61.         }
  62.         /// <summary>
  63.         /// Pen width
  64.         /// </summary>
  65.         public int PenWidth
  66.         {
  67.             get
  68.             {
  69.                 return penWidth;
  70.             }
  71.             set
  72.             {
  73.                 penWidth = value;
  74.             }
  75.         }
  76.         /// <summary>
  77.         /// Number of handles
  78.         /// </summary>
  79.         public virtual int HandleCount
  80.         {
  81.             get
  82.             {
  83.                 return 0;
  84.             }
  85.         }
  86.         /// <summary>
  87.         /// Object ID
  88.         /// </summary>
  89.         public int ID
  90.         {
  91.             get { return id; }
  92.             set { id = value; }
  93.         }
  94.         /// <summary>
  95.         /// Last used color
  96.         /// </summary>
  97.         public static Color LastUsedColor
  98.         {
  99.             get
  100.             {
  101.                 return lastUsedColor;
  102.             }
  103.             set
  104.             {
  105.                 lastUsedColor = value;
  106.             }
  107.         }
  108.         /// <summary>
  109.         /// Last used pen width
  110.         /// </summary>
  111.         public static int LastUsedPenWidth
  112.         {
  113.             get
  114.             {
  115.                 return lastUsedPenWidth;
  116.             }
  117.             set
  118.             {
  119.                 lastUsedPenWidth = value;
  120.             }
  121.         }
  122.         #endregion
  123.         #region Virtual Functions
  124.         /// <summary>
  125.         /// Clone this instance.
  126.         /// </summary>
  127.         public abstract DrawObject Clone();
  128.         /// <summary>
  129.         /// Draw object
  130.         /// </summary>
  131.         /// <param name="g"></param>
  132.         public virtual void Draw(Graphics g)
  133.         {
  134.         }
  135.         /// <summary>
  136.         /// Get handle point by 1-based number
  137.         /// </summary>
  138.         /// <param name="handleNumber"></param>
  139.         /// <returns></returns>
  140.         public virtual Point GetHandle(int handleNumber)
  141.         {
  142.             return new Point(0, 0);
  143.         }
  144.         /// <summary>
  145.         /// Get handle rectangle by 1-based number
  146.         /// </summary>
  147.         /// <param name="handleNumber"></param>
  148.         /// <returns></returns>
  149.         public virtual Rectangle GetHandleRectangle(int handleNumber)
  150.         {
  151.             Point point = GetHandle(handleNumber);
  152.             return new Rectangle(point.X - 3, point.Y - 3, 7, 7);
  153.         }
  154.         /// <summary>
  155.         /// Draw tracker for selected object
  156.         /// </summary>
  157.         /// <param name="g"></param>
  158.         public virtual void DrawTracker(Graphics g)
  159.         {
  160.             if ( ! Selected )
  161.                 return;
  162.             SolidBrush brush = new SolidBrush(Color.Black);
  163.             for ( int i = 1; i <= HandleCount; i++ )
  164.             {
  165.                 g.FillRectangle(brush, GetHandleRectangle(i));
  166.             }
  167.             brush.Dispose();
  168.         }
  169.         /// <summary>
  170.         /// Hit test.
  171.         /// Return value: -1 - no hit
  172.         ///                0 - hit anywhere
  173.         ///                > 1 - handle number
  174.         /// </summary>
  175.         /// <param name="point"></param>
  176.         /// <returns></returns>
  177.         public virtual int HitTest(Point point)
  178.         {
  179.             return -1;
  180.         }
  181.         /// <summary>
  182.         /// Test whether point is inside of the object
  183.         /// </summary>
  184.         /// <param name="point"></param>
  185.         /// <returns></returns>
  186.         protected virtual bool PointInObject(Point point)
  187.         {
  188.             return false;
  189.         }
  190.         
  191.         /// <summary>
  192.         /// Get cursor for the handle
  193.         /// </summary>
  194.         /// <param name="handleNumber"></param>
  195.         /// <returns></returns>
  196.         public virtual Cursor GetHandleCursor(int handleNumber)
  197.         {
  198.             return Cursors.Default;
  199.         }
  200.         /// <summary>
  201.         /// Test whether object intersects with rectangle
  202.         /// </summary>
  203.         /// <param name="rectangle"></param>
  204.         /// <returns></returns>
  205.         public virtual bool IntersectsWith(Rectangle rectangle)
  206.         {
  207.             return false;
  208.         }
  209.         /// <summary>
  210.         /// Move object
  211.         /// </summary>
  212.         /// <param name="deltaX"></param>
  213.         /// <param name="deltaY"></param>
  214.         public virtual void Move(int deltaX, int deltaY)
  215.         {
  216.         }
  217.         /// <summary>
  218.         /// Move handle to the point
  219.         /// </summary>
  220.         /// <param name="point"></param>
  221.         /// <param name="handleNumber"></param>
  222.         public virtual void MoveHandleTo(Point point, int handleNumber)
  223.         {
  224.         }
  225.         /// <summary>
  226.         /// Dump (for debugging)
  227.         /// </summary>
  228.         public virtual void Dump()
  229.         {
  230.             Trace.WriteLine(this.GetType().Name);
  231.             Trace.WriteLine("Selected = " + 
  232.                 selected.ToString(CultureInfo.InvariantCulture)
  233.                 + " ID = " + id.ToString(CultureInfo.InvariantCulture));
  234.         }
  235.         /// <summary>
  236.         /// Normalize object.
  237.         /// Call this function in the end of object resizing.
  238.         /// </summary>
  239.         public virtual void Normalize()
  240.         {
  241.         }
  242.         /// <summary>
  243.         /// Save object to serialization stream
  244.         /// </summary>
  245.         /// <param name="info"></param>
  246.         /// <param name="orderNumber"></param>
  247.         public virtual void SaveToStream(SerializationInfo info, int orderNumber)
  248.         {
  249.             info.AddValue(
  250.                 String.Format(CultureInfo.InvariantCulture,
  251.                     "{0}{1}",
  252.                     entryColor, orderNumber),
  253.                 Color.ToArgb());
  254.             info.AddValue(
  255.                 String.Format(CultureInfo.InvariantCulture,
  256.                 "{0}{1}",
  257.                 entryPenWidth, orderNumber),
  258.                 PenWidth);
  259.         }
  260.         /// <summary>
  261.         /// Load object from serialization stream
  262.         /// </summary>
  263.         /// <param name="info"></param>
  264.         /// <param name="orderNumber"></param>
  265.         public virtual void LoadFromStream(SerializationInfo info, int orderNumber)
  266.         {
  267.             int n = info.GetInt32(
  268.                 String.Format(CultureInfo.InvariantCulture,
  269.                     "{0}{1}",
  270.                     entryColor, orderNumber));
  271.             Color = Color.FromArgb(n);
  272.             PenWidth = info.GetInt32(
  273.                 String.Format(CultureInfo.InvariantCulture,
  274.                 "{0}{1}",
  275.                 entryPenWidth, orderNumber));
  276.             id = this.GetHashCode();
  277.         }
  278.         #endregion
  279.         #region Other functions
  280.         /// <summary>
  281.         /// Initialization
  282.         /// </summary>
  283.         protected void Initialize()
  284.         {
  285.             color = lastUsedColor;
  286.             penWidth = LastUsedPenWidth;
  287.         }
  288.         /// <summary>
  289.         /// Copy fields from this instance to cloned instance drawObject.
  290.         /// Called from Clone functions of derived classes.
  291.         /// </summary>
  292.         protected void FillDrawObjectFields(DrawObject drawObject)
  293.         {
  294.             drawObject.selected = this.selected;
  295.             drawObject.color = this.color;
  296.             drawObject.penWidth = this.penWidth;
  297.             drawObject.ID = this.ID;
  298.         }
  299.         #endregion
  300.     }
  301. }