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

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.Drawing.Drawing2D;
  7. using System.Runtime.Serialization;
  8. namespace DrawTools
  9. {
  10. /// <summary>
  11. /// Line graphic object
  12. /// </summary>
  13. class DrawLine : DrawTools.DrawObject
  14. {
  15.         private Point startPoint;
  16.         private Point endPoint;
  17.         private const string entryStart = "Start";
  18.         private const string entryEnd = "End";
  19.         /// <summary>
  20.         ///  Graphic objects for hit test
  21.         /// </summary>
  22.         private GraphicsPath areaPath = null;
  23.         private Pen areaPen = null;
  24.         private Region areaRegion = null;
  25. public DrawLine() : this(0, 0, 1, 0)
  26. {
  27. }
  28.         public DrawLine(int x1, int y1, int x2, int y2) : base()
  29.         {
  30.             startPoint.X = x1;
  31.             startPoint.Y = y1;
  32.             endPoint.X = x2;
  33.             endPoint.Y = y2;
  34.             Initialize();
  35.         }
  36.         /// <summary>
  37.         /// Clone this instance
  38.         /// </summary>
  39.         public override DrawObject Clone()
  40.         {
  41.             DrawLine drawLine = new DrawLine();
  42.             drawLine.startPoint = this.startPoint;
  43.             drawLine.endPoint = this.endPoint;
  44.             FillDrawObjectFields(drawLine);
  45.             return drawLine;
  46.         }
  47.         public override void Draw(Graphics g)
  48.         {
  49.             g.SmoothingMode = SmoothingMode.AntiAlias;
  50.             Pen pen = new Pen(Color, PenWidth);
  51.             g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  52.             pen.Dispose();
  53.         }
  54.         public override int HandleCount
  55.         {
  56.             get
  57.             {
  58.                 return 2;
  59.             }
  60.         }
  61.         /// <summary>
  62.         /// Get handle point by 1-based number
  63.         /// </summary>
  64.         /// <param name="handleNumber"></param>
  65.         /// <returns></returns>
  66.         public override Point GetHandle(int handleNumber)
  67.         {
  68.             if ( handleNumber == 1 )
  69.                 return startPoint;
  70.             else
  71.                 return endPoint;
  72.         }
  73.         /// <summary>
  74.         /// Hit test.
  75.         /// Return value: -1 - no hit
  76.         ///                0 - hit anywhere
  77.         ///                > 1 - handle number
  78.         /// </summary>
  79.         /// <param name="point"></param>
  80.         /// <returns></returns>
  81.         public override int HitTest(Point point)
  82.         {
  83.             if ( Selected )
  84.             {
  85.                 for ( int i = 1; i <= HandleCount; i++ )
  86.                 {
  87.                     if ( GetHandleRectangle(i).Contains(point) )
  88.                         return i;
  89.                 }
  90.             }
  91.             if ( PointInObject(point) )
  92.                 return 0;
  93.             return -1;
  94.         }
  95.         protected override bool PointInObject(Point point)
  96.         {
  97.             CreateObjects();
  98.             return AreaRegion.IsVisible(point);
  99.         }
  100.         public override bool IntersectsWith(Rectangle rectangle)
  101.         {
  102.             CreateObjects();
  103.             return AreaRegion.IsVisible(rectangle);
  104.         }
  105.         public override Cursor GetHandleCursor(int handleNumber)
  106.         {
  107.             switch ( handleNumber )
  108.             {
  109.                 case 1:
  110.                 case 2:
  111.                     return Cursors.SizeAll;
  112.                 default:
  113.                     return Cursors.Default;
  114.             }
  115.         }
  116.         public override void MoveHandleTo(Point point, int handleNumber)
  117.         {
  118.             if ( handleNumber == 1 )
  119.                 startPoint = point;
  120.             else
  121.                 endPoint = point;
  122.             Invalidate();
  123.         }
  124.         public override void Move(int deltaX, int deltaY)
  125.         {
  126.             startPoint.X += deltaX;
  127.             startPoint.Y += deltaY;
  128.             endPoint.X += deltaX;
  129.             endPoint.Y += deltaY;
  130.             Invalidate();
  131.         }
  132.         public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  133.         {
  134.             info.AddValue(
  135.                 String.Format(CultureInfo.InvariantCulture,
  136.                 "{0}{1}",
  137.                 entryStart, orderNumber),
  138.                 startPoint);
  139.             info.AddValue(
  140.                 String.Format(CultureInfo.InvariantCulture,
  141.                 "{0}{1}",
  142.                 entryEnd, orderNumber),
  143.                 endPoint);
  144.             base.SaveToStream (info, orderNumber);
  145.         }
  146.         public override void LoadFromStream(SerializationInfo info, int orderNumber)
  147.         {
  148.             startPoint = (Point)info.GetValue(
  149.                 String.Format(CultureInfo.InvariantCulture,
  150.                 "{0}{1}",
  151.                 entryStart, orderNumber),
  152.                 typeof(Point));
  153.             endPoint = (Point)info.GetValue(
  154.                 String.Format(CultureInfo.InvariantCulture,
  155.                 "{0}{1}",
  156.                 entryEnd, orderNumber),
  157.                 typeof(Point));
  158.             base.LoadFromStream (info, orderNumber);
  159.         }
  160.         /// <summary>
  161.         /// Invalidate object.
  162.         /// When object is invalidated, path used for hit test
  163.         /// is released and should be created again.
  164.         /// </summary>
  165.         protected void Invalidate()
  166.         {
  167.             if ( AreaPath != null )
  168.             {
  169.                 AreaPath.Dispose();
  170.                 AreaPath = null;
  171.             }
  172.             if ( AreaPen != null )
  173.             {
  174.                 AreaPen.Dispose();
  175.                 AreaPen = null;
  176.             }
  177.             if ( AreaRegion != null )
  178.             {
  179.                 AreaRegion.Dispose();
  180.                 AreaRegion = null;
  181.             }
  182.         }
  183.         /// <summary>
  184.         /// Create graphic objects used from hit test.
  185.         /// </summary>
  186.         protected virtual void CreateObjects()
  187.         {
  188.             if ( AreaPath != null )
  189.                 return;
  190.             // Create path which contains wide line
  191.             // for easy mouse selection
  192.             AreaPath = new GraphicsPath();
  193.             AreaPen = new Pen(Color.Black, 7);
  194.             AreaPath.AddLine(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  195.             AreaPath.Widen(AreaPen);
  196.             // Create region from the path
  197.             AreaRegion = new Region(AreaPath);
  198.         }
  199.         protected GraphicsPath AreaPath
  200.         {
  201.             get
  202.             {
  203.                 return areaPath;
  204.             }
  205.             set
  206.             {
  207.                 areaPath = value;
  208.             }
  209.         }
  210.         protected Pen AreaPen
  211.         {
  212.             get
  213.             {
  214.                 return areaPen;
  215.             }
  216.             set
  217.             {
  218.                 areaPen = value;
  219.             }
  220.         }
  221.         protected Region AreaRegion
  222.         {
  223.             get
  224.             {
  225.                 return areaRegion;
  226.             }
  227.             set
  228.             {
  229.                 areaRegion = value;
  230.             }
  231.         }
  232. }
  233. }