DrawTimeMark.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.Startup;
  9. using PaintDotNet.Base.CommTool;
  10. using System.IO;
  11. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. using PaintDotNet.Annotation.Enum;
  13. using System.Collections.Generic;
  14. using PaintDotNet.Base.Enum;
  15. namespace PaintDotNet.Annotation.Label
  16. {
  17. /// <summary>
  18. /// 标注->标记->日期
  19. /// </summary>
  20. public class DrawTimeMark : DrawObject
  21. {
  22. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  23. /// <summary>
  24. /// 标注样式信息model
  25. /// </summary>
  26. public TimeMark labelTimeMarkStyleModel;
  27. /// <summary>
  28. /// 时间信息
  29. /// </summary>
  30. private string timeStr = "";
  31. /// <summary>
  32. /// 辅助为第一次创建时间对象赋值
  33. /// </summary>
  34. public bool isFirstClick = true;
  35. public DrawTimeMark(ISurfaceBox surfaceBox) : base()
  36. {
  37. this.objectType = DrawClass.Label;
  38. this.drawToolType = DrawToolType.DrawTimeMark;
  39. Initialize();
  40. }
  41. public DrawTimeMark(ISurfaceBox surfaceBox, string nowTime) : base()
  42. {
  43. this.objectType = DrawClass.Label;
  44. this.drawToolType = DrawToolType.DrawTimeMark;
  45. timeStr = nowTime;
  46. labelTimeMarkStyleModel = surfaceBox.GetLabelStyleModel().timeMark;
  47. startPoint = new PointF(0, 0);
  48. Initialize();
  49. }
  50. public DrawTimeMark(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) :base()
  51. {
  52. this.objectType = DrawClass.Label;
  53. this.drawToolType = DrawToolType.DrawTimeMark;
  54. this.ISurfaceBox = surfaceBox;
  55. labelTimeMarkStyleModel = (LabelStyleModel.TimeMark)parentStyleModel;
  56. startPoint = points[0];
  57. timeStr = content.ToString();
  58. isFirstClick = false;
  59. }
  60. /// <summary>
  61. /// Clone this instance
  62. /// </summary>
  63. public override DrawObject Clone()
  64. {
  65. DrawTimeMark timeMark = new DrawTimeMark(ISurfaceBox);
  66. timeMark.objectType = DrawClass.Label;
  67. timeMark.drawToolType = DrawToolType.DrawTimeMark;
  68. timeMark.ISurfaceBox = this.ISurfaceBox;
  69. timeMark.startPoint = this.startPoint;
  70. timeMark.timeStr = this.timeStr;
  71. timeMark.isFirstClick = false;
  72. timeMark.labelTimeMarkStyleModel = this.labelTimeMarkStyleModel;
  73. FillDrawObjectFields(timeMark);
  74. return timeMark;
  75. }
  76. public override DrawObject Clone(ISurfaceBox surfaceBox)
  77. {
  78. DrawTimeMark timeMark = new DrawTimeMark(surfaceBox);
  79. timeMark.objectType = DrawClass.Label;
  80. timeMark.drawToolType = DrawToolType.DrawTimeMark;
  81. timeMark.ISurfaceBox = surfaceBox;
  82. timeMark.startPoint = this.startPoint;
  83. timeMark.timeStr = this.timeStr;
  84. timeMark.isFirstClick = false;
  85. timeMark.labelTimeMarkStyleModel = this.labelTimeMarkStyleModel;
  86. FillDrawObjectFields(timeMark);
  87. return timeMark;
  88. }
  89. public override void Draw(Graphics g)
  90. {
  91. g.SmoothingMode = SmoothingMode.AntiAlias;
  92. Font textfont = new Font(this.labelTimeMarkStyleModel.font, this.labelTimeMarkStyleModel.fontSize);
  93. Brush textbrush = new SolidBrush(Color.FromArgb(this.labelTimeMarkStyleModel.textColor));
  94. Color color = Color.FromArgb(this.labelTimeMarkStyleModel.backgroundBoxColor);
  95. Pen pen = new Pen(color, this.labelTimeMarkStyleModel.lineWidth);
  96. Color fillColor = Color.FromArgb(this.labelTimeMarkStyleModel.backgroundColor);
  97. SolidBrush fillBrush = new SolidBrush(fillColor);
  98. Size fontSize = g.MeasureString(timeStr, textfont).ToSize();
  99. float positionSize = 0;//位置大小
  100. float backgroundSize = 0;//背景大小
  101. if (fontSize.Width > fontSize.Height)
  102. {
  103. positionSize = fontSize.Width * (this.labelTimeMarkStyleModel.positionSize / 100f);
  104. backgroundSize = fontSize.Width * (this.labelTimeMarkStyleModel.backgroundSize / 100f);
  105. }
  106. else
  107. {
  108. positionSize = fontSize.Height * (this.labelTimeMarkStyleModel.positionSize / 100f);
  109. backgroundSize = fontSize.Height * (this.labelTimeMarkStyleModel.backgroundSize / 100f);
  110. }
  111. if (isFirstClick)
  112. {
  113. string gainPosition = this.labelTimeMarkStyleModel.position;
  114. SizeF windowSize = this.ISurfaceBox.GetDocumentSize();
  115. switch ((TagLocation)System.Enum.ToObject(typeof(TagLocation), int.Parse(gainPosition)))
  116. {
  117. case TagLocation.UpLeft:
  118. startPoint.X = 0 + positionSize;
  119. startPoint.Y = 0 + positionSize;
  120. break;
  121. case TagLocation.UpRight:
  122. startPoint.X = Convert.ToInt32(windowSize.Width) - fontSize.Width - 2 * backgroundSize - positionSize;
  123. startPoint.Y = 0 + positionSize;
  124. break;
  125. case TagLocation.DownLeft:
  126. startPoint.X = 0 + positionSize;
  127. startPoint.Y = Convert.ToInt32(windowSize.Height) - fontSize.Height - 2 * backgroundSize - positionSize;
  128. break;
  129. case TagLocation.DownRight:
  130. startPoint.X = Convert.ToInt32(windowSize.Width) - fontSize.Width - 2 * backgroundSize - positionSize;
  131. startPoint.Y = Convert.ToInt32(windowSize.Height) - fontSize.Height - 2 * backgroundSize - positionSize;
  132. break;
  133. }
  134. isFirstClick = false;
  135. }
  136. g.FillRectangle(fillBrush, startPoint.X, startPoint.Y, fontSize.Width + 2 * backgroundSize, fontSize.Height + 2 * backgroundSize);
  137. g.DrawRectangle(pen, startPoint.X, startPoint.Y, fontSize.Width + 2 * backgroundSize, fontSize.Height + 2 * backgroundSize);
  138. g.DrawString(timeStr, textfont, textbrush, startPoint.X + backgroundSize, startPoint.Y + backgroundSize);
  139. rectangle.X = startPoint.X;
  140. rectangle.Y = startPoint.Y;
  141. rectangle.Width = fontSize.Width + 2 * backgroundSize;
  142. rectangle.Height = fontSize.Height + 2 * backgroundSize;
  143. fillBrush.Dispose();
  144. pen.Dispose();
  145. }
  146. public override int HandleCount
  147. {
  148. get
  149. {
  150. return 1;
  151. }
  152. }
  153. /// <summary>
  154. /// Get handle pointscroll by 1-based number
  155. /// </summary>
  156. /// <param name="handleNumber"></param>
  157. /// <returns></returns>
  158. public override PointF GetHandle(int handleNumber)
  159. {
  160. return startPoint;
  161. }
  162. /// <summary>
  163. /// Hit test.
  164. /// Return value: -1 - no hit
  165. /// 0 - hit anywhere
  166. /// > 1 - handle number
  167. /// </summary>
  168. /// <param name="pointscroll"></param>
  169. /// <returns></returns>
  170. public override int HitTest(Point point)
  171. {
  172. if (Selected)
  173. {
  174. for (int i = 1; i <= HandleCount; i++)
  175. {
  176. if (GetHandleRectangle(i).Contains(point))
  177. return i;
  178. }
  179. }
  180. if (PointInObject(point))
  181. return 0;
  182. return -1;
  183. }
  184. protected override bool PointInObject(Point point)
  185. {
  186. return rectangle.Contains(point);
  187. }
  188. public override bool IntersectsWith(Rectangle rectangle)
  189. {
  190. return Rectangle.IntersectsWith(rectangle);
  191. }
  192. public override Cursor GetHandleCursor(int handleNumber)
  193. {
  194. return Cursors.Default;
  195. }
  196. public override void MoveHandleTo(Point point, int handleNumber)
  197. {
  198. startPoint = point;
  199. }
  200. public override void Move(int deltaX, int deltaY)
  201. {
  202. int x = ISurfaceBox.UnscaleScalar(deltaX);
  203. int y = ISurfaceBox.UnscaleScalar(deltaY);
  204. startPoint.X += x;
  205. startPoint.Y += y;
  206. rectangle.X += x;
  207. rectangle.Y += y;
  208. }
  209. public override RectangleF GetBoundingBox()
  210. {
  211. return rectangle;
  212. }
  213. public override List<PointF> GetPoints()
  214. {
  215. List<PointF> points = new List<PointF>();
  216. points.Add(startPoint);
  217. return points;
  218. }
  219. public override ParentStyleModel GetStyle()
  220. {
  221. return labelTimeMarkStyleModel;
  222. }
  223. public override string GetContent()
  224. {
  225. return timeStr;
  226. }
  227. }
  228. }