DrawLine.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using SmartCoalApplication.Base.SettingModel;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Globalization;
  6. using System.Runtime.Serialization;
  7. using System.Windows.Forms;
  8. using static SmartCoalApplication.Base.SettingModel.LabelStyleModel;
  9. using SmartCoalApplication.Annotation.Enum;
  10. using System.Collections.Generic;
  11. using Resources;
  12. namespace SmartCoalApplication.Annotation.Label
  13. {
  14. /// <summary>
  15. /// 标注->直线->直线
  16. /// </summary>
  17. public class DrawLine : DrawObject
  18. {
  19. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  20. /// <summary>
  21. /// 可能最后用不到
  22. /// </summary>
  23. private const string entryStart = "Start";
  24. /// <summary>
  25. /// 可能最后用不到
  26. /// </summary>
  27. private const string entryEnd = "End";
  28. /// <summary>
  29. /// Graphic objects for hit test
  30. /// </summary>
  31. private GraphicsPath areaPath = null;
  32. private Pen areaPen = null;
  33. private Region areaRegion = null;
  34. /// <summary>
  35. /// 标注样式信息model
  36. /// </summary>
  37. public LineChildLine labelLineStyleModel;
  38. public DrawLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  39. {
  40. this.objectType = DrawClass.Label;
  41. this.drawToolType = DrawToolType.DrawLine;
  42. //labelLineStyleModel = surfaceBox.GetLabelStyleModel().lineChildLine;
  43. startPoint.X = x1;
  44. startPoint.Y = y1;
  45. endPoint.X = x2;
  46. endPoint.Y = y2;
  47. Initialize();
  48. }
  49. public DrawLine(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  50. {
  51. this.objectType = DrawClass.Label;
  52. this.drawToolType = DrawToolType.DrawLine;
  53. this.ISurfaceBox = surfaceBox;
  54. labelLineStyleModel = (LineChildLine)parentStyleModel;
  55. startPoint.X = points[0].X;
  56. startPoint.Y = points[0].Y;
  57. endPoint.X = points[1].X;
  58. endPoint.Y = points[1].Y;
  59. }
  60. /// <summary>
  61. /// Clone this instance
  62. /// </summary>
  63. public override DrawObject Clone()
  64. {
  65. DrawLine drawLine = new DrawLine(ISurfaceBox, 0, 0, 1, 0);
  66. drawLine.objectType = DrawClass.Label;
  67. drawLine.drawToolType = DrawToolType.DrawLine;
  68. drawLine.ISurfaceBox = this.ISurfaceBox;
  69. drawLine.startPoint = this.startPoint;
  70. drawLine.endPoint = this.endPoint;
  71. drawLine.labelLineStyleModel = this.labelLineStyleModel;
  72. FillDrawObjectFields(drawLine);
  73. return drawLine;
  74. }
  75. public override DrawObject Clone(ISurfaceBox surfaceBox)
  76. {
  77. DrawLine drawLine = new DrawLine(surfaceBox, 0, 0, 1, 0);
  78. drawLine.objectType = DrawClass.Label;
  79. drawLine.drawToolType = DrawToolType.DrawLine;
  80. drawLine.ISurfaceBox = surfaceBox;
  81. drawLine.startPoint = this.startPoint;
  82. drawLine.endPoint = this.endPoint;
  83. drawLine.labelLineStyleModel = this.labelLineStyleModel;
  84. FillDrawObjectFields(drawLine);
  85. return drawLine;
  86. }
  87. public override void Draw(Graphics g)
  88. {
  89. g.SmoothingMode = SmoothingMode.AntiAlias;
  90. Color color = Color.Blue;
  91. Pen pen = new Pen(color,3);
  92. //pen.DashStyle = (DashStyle)this.labelLineStyleModel.lineStyle;
  93. g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  94. pen.Dispose();
  95. }
  96. public override int HandleCount
  97. {
  98. get
  99. {
  100. return 2;
  101. }
  102. }
  103. /// <summary>
  104. /// Get handle pointscroll by 1-based number
  105. /// </summary>
  106. /// <param name="handleNumber"></param>
  107. /// <returns></returns>
  108. public override PointF GetHandle(int handleNumber)
  109. {
  110. if (handleNumber == 1)
  111. return startPoint;
  112. else
  113. return endPoint;
  114. }
  115. /// <summary>
  116. /// Hit test.
  117. /// Return value: -1 - no hit
  118. /// 0 - hit anywhere
  119. /// > 1 - handle number
  120. /// </summary>
  121. /// <param name="pointscroll"></param>
  122. /// <returns></returns>
  123. public override int HitTest(Point point)
  124. {
  125. if (Selected)
  126. {
  127. for (int i = 1; i <= HandleCount; i++)
  128. {
  129. if (GetHandleRectangle(i).Contains(point))
  130. return i;
  131. }
  132. }
  133. if (PointInObject(point))
  134. return 0;
  135. return -1;
  136. }
  137. protected override bool PointInObject(Point point)
  138. {
  139. CreateObjects();
  140. return AreaRegion.IsVisible(point);
  141. }
  142. public override bool IntersectsWith(Rectangle rectangle)
  143. {
  144. CreateObjects();
  145. return AreaRegion.IsVisible(rectangle);
  146. }
  147. public override Cursor GetHandleCursor(int handleNumber)
  148. {
  149. switch (handleNumber)
  150. {
  151. case 1:
  152. case 2:
  153. return handleCursor;// Cursors.SizeAll;
  154. default:
  155. return Cursors.Default;
  156. }
  157. }
  158. public override void MoveHandleTo(Point point, int handleNumber)
  159. {
  160. if (handleNumber == 1)
  161. startPoint = point;
  162. else
  163. endPoint = point;
  164. Invalidate();
  165. }
  166. public override void Move(int deltaX, int deltaY)
  167. {
  168. int x = ISurfaceBox.UnscaleScalar(deltaX);
  169. int y = ISurfaceBox.UnscaleScalar(deltaY);
  170. startPoint.X += x;
  171. startPoint.Y += y;
  172. endPoint.X += x;
  173. endPoint.Y += y;
  174. Invalidate();
  175. }
  176. /// <summary>
  177. /// Invalidate object.
  178. /// When object is invalidated, path used for hit test
  179. /// is released and should be created again.
  180. /// </summary>
  181. protected void Invalidate()
  182. {
  183. if (AreaPath != null)
  184. {
  185. AreaPath.Dispose();
  186. AreaPath = null;
  187. }
  188. if (AreaPen != null)
  189. {
  190. AreaPen.Dispose();
  191. AreaPen = null;
  192. }
  193. if (AreaRegion != null)
  194. {
  195. AreaRegion.Dispose();
  196. AreaRegion = null;
  197. }
  198. }
  199. /// <summary>
  200. /// Create graphic objects used from hit test.
  201. /// </summary>
  202. protected virtual void CreateObjects()
  203. {
  204. if (AreaPath != null)
  205. return;
  206. // Create path which contains wide line
  207. // for easy mouse selection
  208. AreaPath = new GraphicsPath();
  209. AreaPen = new Pen(Color.Black, 7);
  210. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  211. AreaPath.Widen(AreaPen);
  212. // Create region from the path
  213. AreaRegion = new Region(AreaPath);
  214. }
  215. protected GraphicsPath AreaPath
  216. {
  217. get
  218. {
  219. return areaPath;
  220. }
  221. set
  222. {
  223. areaPath = value;
  224. }
  225. }
  226. protected Pen AreaPen
  227. {
  228. get
  229. {
  230. return areaPen;
  231. }
  232. set
  233. {
  234. areaPen = value;
  235. }
  236. }
  237. protected Region AreaRegion
  238. {
  239. get
  240. {
  241. return areaRegion;
  242. }
  243. set
  244. {
  245. areaRegion = value;
  246. }
  247. }
  248. public override RectangleF GetBoundingBox()
  249. {
  250. RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
  251. return rectangle;
  252. }
  253. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  254. {
  255. RectangleF rect = new RectangleF();
  256. if (a.X < b.X)
  257. {
  258. rect.X = a.X;
  259. rect.Width = b.X - a.X;
  260. }
  261. else
  262. {
  263. rect.X = b.X;
  264. rect.Width = a.X - b.X;
  265. }
  266. if (a.Y < b.Y)
  267. {
  268. rect.Y = a.Y;
  269. rect.Height = b.Y - a.Y;
  270. }
  271. else
  272. {
  273. rect.Y = b.Y;
  274. rect.Height = a.Y - b.Y;
  275. }
  276. return rect;
  277. }
  278. /// <summary>
  279. /// Draw tracker for selected object
  280. /// </summary>
  281. /// <param name="g"></param>
  282. public override void DrawTracker(Graphics g)
  283. {
  284. if (!Selected)
  285. return;
  286. SolidBrush brush = new SolidBrush(Color.Black);
  287. for (int i = 1; i <= HandleCount; i++)
  288. {
  289. g.FillRectangle(brush, GetHandleRectangle(i));
  290. }
  291. brush.Dispose();
  292. }
  293. public override List<PointF> GetPoints()
  294. {
  295. List<PointF> points = new List<PointF>();
  296. points.Add(startPoint);
  297. points.Add(endPoint);
  298. return points;
  299. }
  300. public override ParentStyleModel GetStyle()
  301. {
  302. return labelLineStyleModel;
  303. }
  304. }
  305. }