DrawPolygon.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #region Using directives
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Drawing.Drawing2D;
  8. using System.Collections.Generic;
  9. #endregion
  10. namespace DrawTools
  11. {
  12. using PointList = List<Point>;
  13. using PointEnumerator = IEnumerator<Point>;
  14. /// <summary>
  15. /// Polygon graphic object
  16. /// </summary>
  17. class DrawPolygon : DrawTools.DrawLine
  18. {
  19. private PointList pointArray; // list of points
  20. private static Cursor handleCursor = new Cursor(typeof(DrawPolygon), "PolyHandle.cur");
  21. private const string entryLength = "Length";
  22. private const string entryPoint = "Point";
  23. public DrawPolygon() : base()
  24. {
  25. pointArray = new PointList();
  26. Initialize();
  27. }
  28. public DrawPolygon(int x1, int y1, int x2, int y2) : base()
  29. {
  30. pointArray = new PointList();
  31. pointArray.Add(new Point(x1, y1));
  32. pointArray.Add(new Point(x2, y2));
  33. Initialize();
  34. }
  35. /// <summary>
  36. /// Clone this instance
  37. /// </summary>
  38. public override DrawObject Clone()
  39. {
  40. DrawPolygon drawPolygon = new DrawPolygon();
  41. foreach(Point p in this.pointArray)
  42. {
  43. drawPolygon.pointArray.Add(p);
  44. }
  45. FillDrawObjectFields(drawPolygon);
  46. return drawPolygon;
  47. }
  48. public override void Draw(Graphics g)
  49. {
  50. int x1 = 0, y1 = 0; // previous point
  51. int x2, y2; // current point
  52. g.SmoothingMode = SmoothingMode.AntiAlias;
  53. Pen pen = new Pen(Color, PenWidth);
  54. PointEnumerator enumerator = pointArray.GetEnumerator();
  55. if (enumerator.MoveNext())
  56. {
  57. x1 = ((Point)enumerator.Current).X;
  58. y1 = ((Point)enumerator.Current).Y;
  59. }
  60. while (enumerator.MoveNext())
  61. {
  62. x2 = ((Point)enumerator.Current).X;
  63. y2 = ((Point)enumerator.Current).Y;
  64. g.DrawLine(pen, x1, y1, x2, y2);
  65. x1 = x2;
  66. y1 = y2;
  67. }
  68. pen.Dispose();
  69. }
  70. public void AddPoint(Point point)
  71. {
  72. pointArray.Add(point);
  73. }
  74. public override int HandleCount
  75. {
  76. get
  77. {
  78. return pointArray.Count;
  79. }
  80. }
  81. /// <summary>
  82. /// Get handle point by 1-based number
  83. /// </summary>
  84. /// <param name="handleNumber"></param>
  85. /// <returns></returns>
  86. public override Point GetHandle(int handleNumber)
  87. {
  88. if (handleNumber < 1)
  89. handleNumber = 1;
  90. if (handleNumber > pointArray.Count)
  91. handleNumber = pointArray.Count;
  92. return ((Point)pointArray[handleNumber - 1]);
  93. }
  94. public override Cursor GetHandleCursor(int handleNumber)
  95. {
  96. return handleCursor;
  97. }
  98. public override void MoveHandleTo(Point point, int handleNumber)
  99. {
  100. if (handleNumber < 1)
  101. handleNumber = 1;
  102. if (handleNumber > pointArray.Count)
  103. handleNumber = pointArray.Count;
  104. pointArray[handleNumber - 1] = point;
  105. Invalidate();
  106. }
  107. public override void Move(int deltaX, int deltaY)
  108. {
  109. int n = pointArray.Count;
  110. Point point;
  111. for (int i = 0; i < n; i++)
  112. {
  113. point = new Point(((Point)pointArray[i]).X + deltaX, ((Point)pointArray[i]).Y + deltaY);
  114. pointArray[i] = point;
  115. }
  116. Invalidate();
  117. }
  118. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  119. {
  120. info.AddValue(
  121. String.Format(CultureInfo.InvariantCulture,
  122. "{0}{1}",
  123. entryLength, orderNumber),
  124. pointArray.Count);
  125. int i = 0;
  126. foreach (Point p in pointArray)
  127. {
  128. info.AddValue(
  129. String.Format(CultureInfo.InvariantCulture,
  130. "{0}{1}-{2}",
  131. entryPoint, orderNumber, i++),
  132. p);
  133. }
  134. base.SaveToStream(info, orderNumber); // ??
  135. }
  136. public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  137. {
  138. Point point;
  139. int n = info.GetInt32(
  140. String.Format(CultureInfo.InvariantCulture,
  141. "{0}{1}",
  142. entryLength, orderNumber));
  143. for (int i = 0; i < n; i++)
  144. {
  145. point = (Point)info.GetValue(
  146. String.Format(CultureInfo.InvariantCulture,
  147. "{0}{1}-{2}",
  148. entryPoint, orderNumber, i),
  149. typeof(Point));
  150. pointArray.Add(point);
  151. }
  152. base.LoadFromStream(info, orderNumber);
  153. }
  154. /// <summary>
  155. /// Create graphic object used for hit test
  156. /// </summary>
  157. protected override void CreateObjects()
  158. {
  159. if (AreaPath != null)
  160. return;
  161. // Create closed path which contains all polygon vertexes
  162. AreaPath = new GraphicsPath();
  163. int x1 = 0, y1 = 0; // previous point
  164. int x2, y2; // current point
  165. PointEnumerator enumerator = pointArray.GetEnumerator();
  166. if (enumerator.MoveNext())
  167. {
  168. x1 = ((Point)enumerator.Current).X;
  169. y1 = ((Point)enumerator.Current).Y;
  170. }
  171. while (enumerator.MoveNext())
  172. {
  173. x2 = ((Point)enumerator.Current).X;
  174. y2 = ((Point)enumerator.Current).Y;
  175. AreaPath.AddLine(x1, y1, x2, y2);
  176. x1 = x2;
  177. y1 = y2;
  178. }
  179. AreaPath.CloseFigure();
  180. // Create region from the path
  181. AreaRegion = new Region(AreaPath);
  182. }
  183. }
  184. }