DrawPointMark.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using PaintDotNet.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 PaintDotNet.Base.CommTool;
  9. using System.IO;
  10. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  11. using PaintDotNet.Annotation.Enum;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using PaintDotNet.Base.Enum;
  15. namespace PaintDotNet.Annotation.Label
  16. {
  17. /// <summary>
  18. /// 标注->标记->点标记
  19. /// </summary>
  20. public class DrawPointMark : DrawObject
  21. {
  22. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  23. /// <summary>
  24. /// 标注样式信息model
  25. /// </summary>
  26. public PointMark labelPointMarkStyleModel;
  27. public DrawPointMark(ISurfaceBox surfaceBox) : base()
  28. {
  29. this.objectType = DrawClass.Label;
  30. this.drawToolType = DrawToolType.DrawPointMark;
  31. Initialize();
  32. }
  33. public DrawPointMark(ISurfaceBox surfaceBox, int x1, int y1) : base()
  34. {
  35. this.objectType = DrawClass.Label;
  36. this.drawToolType = DrawToolType.DrawPointMark;
  37. startPoint = new PointF(x1, y1);
  38. labelPointMarkStyleModel = surfaceBox.GetLabelStyleModel().pointMark;
  39. Initialize();
  40. }
  41. public DrawPointMark(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  42. {
  43. this.objectType = DrawClass.Label;
  44. this.drawToolType = DrawToolType.DrawPointMark;
  45. this.ISurfaceBox = surfaceBox;
  46. labelPointMarkStyleModel = (LabelStyleModel.PointMark)parentStyleModel;
  47. startPoint = points[0];
  48. }
  49. /// <summary>
  50. /// Clone this instance
  51. /// </summary>
  52. public override DrawObject Clone()
  53. {
  54. DrawPointMark pointMark = new DrawPointMark(ISurfaceBox);
  55. pointMark.objectType = DrawClass.Label;
  56. pointMark.drawToolType = DrawToolType.DrawPointMark;
  57. pointMark.ISurfaceBox = this.ISurfaceBox;
  58. pointMark.startPoint = this.startPoint;
  59. pointMark.rectangle = this.rectangle;
  60. pointMark.labelPointMarkStyleModel = this.labelPointMarkStyleModel;
  61. FillDrawObjectFields(pointMark);
  62. return pointMark;
  63. }
  64. public override DrawObject Clone(ISurfaceBox surfaceBox)
  65. {
  66. DrawPointMark pointMark = new DrawPointMark(surfaceBox);
  67. pointMark.objectType = DrawClass.Label;
  68. pointMark.drawToolType = DrawToolType.DrawPointMark;
  69. pointMark.ISurfaceBox = surfaceBox;
  70. pointMark.startPoint = this.startPoint;
  71. pointMark.rectangle = this.rectangle;
  72. pointMark.labelPointMarkStyleModel = this.labelPointMarkStyleModel;
  73. FillDrawObjectFields(pointMark);
  74. return pointMark;
  75. }
  76. public override void Draw(Graphics g)
  77. {
  78. SizeF windowSize = this.ISurfaceBox.GetDocumentSize();//窗体尺寸
  79. int newHeight = Convert.ToInt32(windowSize.Height / 40);
  80. string markStyle = this.labelPointMarkStyleModel.pointStyle;
  81. Color color = Color.FromArgb(this.labelPointMarkStyleModel.pointColor);
  82. Pen pen = new Pen(color);
  83. pen.Width = newHeight / 5;
  84. g.SmoothingMode = SmoothingMode.AntiAlias;
  85. switch ((SomeStyle)System.Enum.ToObject(typeof(SomeStyle), int.Parse(markStyle)))
  86. {
  87. case SomeStyle.Arrow:
  88. rectangle.X = startPoint.X - newHeight / 2;
  89. rectangle.Y = startPoint.Y;
  90. rectangle.Width = newHeight;
  91. rectangle.Height = newHeight;
  92. pen.EndCap = LineCap.ArrowAnchor;
  93. g.DrawLine(pen, new PointF(startPoint.X, startPoint.Y), new PointF(startPoint.X, startPoint.Y + newHeight));
  94. break;
  95. case SomeStyle.Dot:
  96. rectangle.X = startPoint.X - newHeight / 2;
  97. rectangle.Y = startPoint.Y - newHeight / 2;
  98. rectangle.Width = newHeight;
  99. rectangle.Height = newHeight;
  100. g.DrawEllipse(pen, rectangle);
  101. break;
  102. case SomeStyle.Triangle:
  103. rectangle.X = startPoint.X - newHeight / 2;
  104. rectangle.Y = startPoint.Y - newHeight / 2;
  105. rectangle.Width = newHeight;
  106. rectangle.Height = newHeight;
  107. PointF p1 = new PointF(rectangle.X, rectangle.Y);
  108. PointF p2 = new PointF(rectangle.Right, rectangle.Top);
  109. PointF p3 = new PointF(rectangle.X + rectangle.Width / 2, rectangle.Bottom);
  110. PointF[] pntArr = { p1, p2, p3 };
  111. g.DrawPolygon(pen, pntArr);
  112. break;
  113. }
  114. pen.Dispose();
  115. }
  116. public override int HandleCount
  117. {
  118. get
  119. {
  120. return 1;
  121. }
  122. }
  123. /// <summary>
  124. /// Get handle pointscroll by 1-based number
  125. /// </summary>
  126. /// <param name="handleNumber"></param>
  127. /// <returns></returns>
  128. public override PointF GetHandle(int handleNumber)
  129. {
  130. return startPoint;
  131. }
  132. /// <summary>
  133. /// Hit test.
  134. /// Return value: -1 - no hit
  135. /// 0 - hit anywhere
  136. /// > 1 - handle number
  137. /// </summary>
  138. /// <param name="pointscroll"></param>
  139. /// <returns></returns>
  140. public override int HitTest(Point point)
  141. {
  142. if (Selected)
  143. {
  144. for (int i = 1; i <= HandleCount; i++)
  145. {
  146. if (GetHandleRectangle(i).Contains(point))
  147. return i;
  148. }
  149. }
  150. if (PointInObject(point))
  151. return 0;
  152. return -1;
  153. }
  154. protected override bool PointInObject(Point point)
  155. {
  156. return rectangle.Contains(point);
  157. }
  158. public override bool IntersectsWith(Rectangle rectangle)
  159. {
  160. return Rectangle.IntersectsWith(rectangle);
  161. }
  162. public override Cursor GetHandleCursor(int handleNumber)
  163. {
  164. return Cursors.Default;
  165. }
  166. public override void MoveHandleTo(Point point, int handleNumber)
  167. {
  168. startPoint = point;
  169. }
  170. public override void Move(int deltaX, int deltaY)
  171. {
  172. int x = ISurfaceBox.UnscaleScalar(deltaX);
  173. int y = ISurfaceBox.UnscaleScalar(deltaY);
  174. startPoint.X += x;
  175. startPoint.Y += y;
  176. rectangle.X += x;
  177. rectangle.Y += y;
  178. }
  179. public override RectangleF GetBoundingBox()
  180. {
  181. return rectangle;
  182. }
  183. public override List<PointF> GetPoints()
  184. {
  185. List<PointF> points = new List<PointF>();
  186. points.Add(startPoint);
  187. return points;
  188. }
  189. public override ParentStyleModel GetStyle()
  190. {
  191. return labelPointMarkStyleModel;
  192. }
  193. }
  194. }