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

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. /// Rectangle graphic object
  11. /// </summary>
  12. class DrawRectangle : DrawTools.DrawObject
  13. {
  14.         private Rectangle rectangle;
  15.         private const string entryRectangle = "Rect";
  16.         protected Rectangle Rectangle
  17.         {
  18.             get
  19.             {
  20.                 return rectangle;
  21.             }
  22.             set
  23.             {
  24.                 rectangle = value;
  25.             }
  26.         }
  27.         
  28. public DrawRectangle() : this(0, 0, 1, 1)
  29. {
  30. }
  31.         
  32.         public DrawRectangle(int x, int y, int width, int height) : base()
  33.         {
  34.             rectangle.X = x;
  35.             rectangle.Y = y;
  36.             rectangle.Width = width;
  37.             rectangle.Height = height;
  38.             Initialize();
  39.         }
  40.         /// <summary>
  41.         /// Clone this instance
  42.         /// </summary>
  43.         public override DrawObject Clone()
  44.         {
  45.             DrawRectangle drawRectangle = new DrawRectangle();
  46.             drawRectangle.rectangle = this.rectangle;
  47.             FillDrawObjectFields(drawRectangle);
  48.             return drawRectangle;
  49.         }
  50.         /// <summary>
  51.         /// Draw rectangle
  52.         /// </summary>
  53.         /// <param name="g"></param>
  54.         public override void Draw(Graphics g)
  55.         {
  56.             Pen pen = new Pen(Color, PenWidth);
  57.             g.DrawRectangle(pen, DrawRectangle.GetNormalizedRectangle(Rectangle));
  58.             pen.Dispose();
  59.         }
  60.         protected void SetRectangle(int x, int y, int width, int height)
  61.         {
  62.             rectangle.X = x;
  63.             rectangle.Y = y;
  64.             rectangle.Width = width;
  65.             rectangle.Height = height;
  66.         }
  67.         /// <summary>
  68.         /// Get number of handles
  69.         /// </summary>
  70.         public override int HandleCount
  71.         {
  72.             get
  73.             {
  74.                 return 8;
  75.             }
  76.         }
  77.         /// <summary>
  78.         /// Get handle point by 1-based number
  79.         /// </summary>
  80.         /// <param name="handleNumber"></param>
  81.         /// <returns></returns>
  82.         public override Point GetHandle(int handleNumber)
  83.         {
  84.             int x, y, xCenter, yCenter;
  85.             xCenter = rectangle.X + rectangle.Width/2;
  86.             yCenter = rectangle.Y + rectangle.Height/2;
  87.             x = rectangle.X;
  88.             y = rectangle.Y;
  89.             switch ( handleNumber )
  90.             {
  91.                 case 1:
  92.                     x = rectangle.X;
  93.                     y = rectangle.Y;
  94.                     break;
  95.                 case 2:
  96.                     x = xCenter;
  97.                     y = rectangle.Y;
  98.                     break;
  99.                 case 3:
  100.                     x = rectangle.Right;
  101.                     y = rectangle.Y;
  102.                     break;
  103.                 case 4:
  104.                     x = rectangle.Right;
  105.                     y = yCenter;
  106.                     break;
  107.                 case 5:
  108.                     x = rectangle.Right;
  109.                     y = rectangle.Bottom;
  110.                     break;
  111.                 case 6:
  112.                     x = xCenter;
  113.                     y = rectangle.Bottom;
  114.                     break;
  115.                 case 7:
  116.                     x = rectangle.X;
  117.                     y = rectangle.Bottom;
  118.                     break;
  119.                 case 8:
  120.                     x = rectangle.X;
  121.                     y = yCenter;
  122.                     break;
  123.             }
  124.             return new Point(x, y);
  125.         }
  126.         /// <summary>
  127.         /// Hit test.
  128.         /// Return value: -1 - no hit
  129.         ///                0 - hit anywhere
  130.         ///                > 1 - handle number
  131.         /// </summary>
  132.         /// <param name="point"></param>
  133.         /// <returns></returns>
  134.         public override int HitTest(Point point)
  135.         {
  136.             if ( Selected )
  137.             {
  138.                 for ( int i = 1; i <= HandleCount; i++ )
  139.                 {
  140.                     if ( GetHandleRectangle(i).Contains(point) )
  141.                         return i;
  142.                 }
  143.             }
  144.             if ( PointInObject(point) )
  145.                 return 0;
  146.             return -1;
  147.         }
  148.         
  149.         protected override bool PointInObject(Point point)
  150.         {
  151.             return rectangle.Contains(point);
  152.         }
  153.         
  154.         /// <summary>
  155.         /// Get cursor for the handle
  156.         /// </summary>
  157.         /// <param name="handleNumber"></param>
  158.         /// <returns></returns>
  159.         public override Cursor GetHandleCursor(int handleNumber)
  160.         {
  161.             switch ( handleNumber )
  162.             {
  163.                 case 1:
  164.                     return Cursors.SizeNWSE;
  165.                 case 2:
  166.                     return Cursors.SizeNS;
  167.                 case 3:
  168.                     return Cursors.SizeNESW;
  169.                 case 4:
  170.                     return Cursors.SizeWE;
  171.                 case 5:
  172.                     return Cursors.SizeNWSE;
  173.                 case 6:
  174.                     return Cursors.SizeNS;
  175.                 case 7:
  176.                     return Cursors.SizeNESW;
  177.                 case 8:
  178.                     return Cursors.SizeWE;
  179.                 default:
  180.                     return Cursors.Default;
  181.             }
  182.         }
  183.         /// <summary>
  184.         /// Move handle to new point (resizing)
  185.         /// </summary>
  186.         /// <param name="point"></param>
  187.         /// <param name="handleNumber"></param>
  188.         public override void MoveHandleTo(Point point, int handleNumber)
  189.         {
  190.             int left = Rectangle.Left;
  191.             int top = Rectangle.Top;
  192.             int right = Rectangle.Right;
  193.             int bottom = Rectangle.Bottom;
  194.             switch ( handleNumber )
  195.             {
  196.                 case 1:
  197.                     left = point.X;
  198.                     top = point.Y;
  199.                     break;
  200.                 case 2:
  201.                     top = point.Y;
  202.                     break;
  203.                 case 3:
  204.                     right = point.X;
  205.                     top = point.Y;
  206.                     break;
  207.                 case 4:
  208.                     right = point.X;
  209.                     break;
  210.                 case 5:
  211.                     right = point.X;
  212.                     bottom = point.Y;
  213.                     break;
  214.                 case 6:
  215.                     bottom = point.Y;
  216.                     break;
  217.                 case 7:
  218.                     left = point.X;
  219.                     bottom = point.Y;
  220.                     break;
  221.                 case 8:
  222.                     left = point.X;
  223.                     break;
  224.             }
  225.             SetRectangle(left, top, right - left, bottom - top);
  226.         }
  227.         public override bool IntersectsWith(Rectangle rectangle)
  228.         {
  229.             return Rectangle.IntersectsWith(rectangle);
  230.         }
  231.         /// <summary>
  232.         /// Move object
  233.         /// </summary>
  234.         /// <param name="deltaX"></param>
  235.         /// <param name="deltaY"></param>
  236.         public override void Move(int deltaX, int deltaY)
  237.         {
  238.             rectangle.X += deltaX;
  239.             rectangle.Y += deltaY;
  240.         }
  241.         public override void Dump()
  242.         {
  243.             base.Dump ();
  244.             Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture));
  245.             Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture));
  246.             Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture));
  247.             Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture));
  248.         }
  249.         /// <summary>
  250.         /// Normalize rectangle
  251.         /// </summary>
  252.         public override void Normalize()
  253.         {
  254.             rectangle = DrawRectangle.GetNormalizedRectangle(rectangle);
  255.         }
  256.         /// <summary>
  257.         /// Save objevt to serialization stream
  258.         /// </summary>
  259.         /// <param name="info"></param>
  260.         /// <param name="orderNumber"></param>
  261.         public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  262.         {
  263.             info.AddValue(
  264.                 String.Format(CultureInfo.InvariantCulture,
  265.                 "{0}{1}",
  266.                 entryRectangle, orderNumber),
  267.                 rectangle);
  268.             base.SaveToStream (info, orderNumber);
  269.         }
  270.         /// <summary>
  271.         /// LOad object from serialization stream
  272.         /// </summary>
  273.         /// <param name="info"></param>
  274.         /// <param name="orderNumber"></param>
  275.         public override void LoadFromStream(SerializationInfo info, int orderNumber)
  276.         {
  277.             rectangle = (Rectangle)info.GetValue(
  278.                 String.Format(CultureInfo.InvariantCulture,
  279.                 "{0}{1}",
  280.                 entryRectangle, orderNumber),
  281.                 typeof(Rectangle));
  282.             base.LoadFromStream (info, orderNumber);
  283.         }
  284.         #region Helper Functions
  285.         public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
  286.         {
  287.             if ( x2 < x1 )
  288.             {
  289.                 int tmp = x2;
  290.                 x2 = x1;
  291.                 x1 = tmp;
  292.             }
  293.             if ( y2 < y1 )
  294.             {
  295.                 int tmp = y2;
  296.                 y2 = y1;
  297.                 y1 = tmp;
  298.             }
  299.             return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  300.         }
  301.         public static Rectangle GetNormalizedRectangle(Point p1, Point p2)
  302.         {
  303.             return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
  304.         }
  305.         public static Rectangle GetNormalizedRectangle(Rectangle r)
  306.         {
  307.             return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  308.         }
  309.         #endregion
  310.     }
  311. }