DrawDateMark.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 DrawDateMark : DrawObject
  21. {
  22. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  23. /// <summary>
  24. /// 标注样式信息model
  25. /// </summary>
  26. public DateMark labelDateMarkStyleModel;
  27. /// <summary>
  28. /// 日期信息
  29. /// </summary>
  30. private string dateStr = "";
  31. /// <summary>
  32. /// 辅助为第一次创建日期对象赋值
  33. /// </summary>
  34. public bool isFirstClick = true;
  35. public DrawDateMark(ISurfaceBox surfaceBox) : base()
  36. {
  37. this.objectType = DrawClass.Label;
  38. this.drawToolType = DrawToolType.DrawDateMark;
  39. Initialize();
  40. }
  41. public DrawDateMark(ISurfaceBox surfaceBox, string nowDate) : base()
  42. {
  43. this.objectType = DrawClass.Label;
  44. this.drawToolType = DrawToolType.DrawDateMark;
  45. dateStr = nowDate;
  46. labelDateMarkStyleModel = surfaceBox.GetLabelStyleModel().dateMark;
  47. startPoint = new PointF(0, 0);
  48. Initialize();
  49. }
  50. public DrawDateMark(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  51. {
  52. this.objectType = DrawClass.Label;
  53. this.drawToolType = DrawToolType.DrawDateMark;
  54. this.ISurfaceBox = surfaceBox;
  55. labelDateMarkStyleModel = (LabelStyleModel.DateMark)parentStyleModel;
  56. startPoint = points[0];
  57. dateStr = content.ToString();
  58. isFirstClick = false;
  59. }
  60. /// <summary>
  61. /// Clone this instance
  62. /// </summary>
  63. public override DrawObject Clone()
  64. {
  65. DrawDateMark dateMark = new DrawDateMark(ISurfaceBox);
  66. dateMark.objectType = DrawClass.Label;
  67. dateMark.drawToolType = DrawToolType.DrawDateMark;
  68. dateMark.ISurfaceBox = ISurfaceBox;
  69. dateMark.startPoint = this.startPoint;
  70. dateMark.dateStr = this.dateStr;
  71. dateMark.isFirstClick = false;
  72. dateMark.labelDateMarkStyleModel = this.labelDateMarkStyleModel;
  73. FillDrawObjectFields(dateMark);
  74. return dateMark;
  75. }
  76. public override DrawObject Clone(ISurfaceBox surfaceBox)
  77. {
  78. DrawDateMark dateMark = new DrawDateMark(surfaceBox);
  79. dateMark.objectType = DrawClass.Label;
  80. dateMark.drawToolType = DrawToolType.DrawDateMark;
  81. dateMark.ISurfaceBox = surfaceBox;
  82. dateMark.startPoint = this.startPoint;
  83. dateMark.dateStr = this.dateStr;
  84. dateMark.isFirstClick = false;
  85. dateMark.labelDateMarkStyleModel = this.labelDateMarkStyleModel;
  86. FillDrawObjectFields(dateMark);
  87. return dateMark;
  88. }
  89. public override void Draw(Graphics g)
  90. {
  91. g.SmoothingMode = SmoothingMode.AntiAlias;
  92. Font textfont = new Font(this.labelDateMarkStyleModel.font, this.labelDateMarkStyleModel.fontSize);
  93. Brush textbrush = new SolidBrush(Color.FromArgb(this.labelDateMarkStyleModel.textColor));
  94. Color color = Color.FromArgb(this.labelDateMarkStyleModel.backgroundBoxColor);
  95. Pen pen = new Pen(color, this.labelDateMarkStyleModel.lineWidth);
  96. Color fillColor = Color.FromArgb(this.labelDateMarkStyleModel.backgroundColor);
  97. SolidBrush fillBrush = new SolidBrush(fillColor);
  98. Size fontSize = g.MeasureString(dateStr, textfont).ToSize();
  99. float positionSize = 0;//位置大小
  100. float backgroundSize = 0;//背景大小
  101. if(fontSize.Width > fontSize.Height)
  102. {
  103. positionSize = fontSize.Width * (this.labelDateMarkStyleModel.positionSize / 100f);
  104. backgroundSize = fontSize.Width * (this.labelDateMarkStyleModel.backgroundSize / 100f);
  105. }
  106. else
  107. {
  108. positionSize = fontSize.Height * (this.labelDateMarkStyleModel.positionSize / 100f);
  109. backgroundSize = fontSize.Height * (this.labelDateMarkStyleModel.backgroundSize / 100f);
  110. }
  111. if (isFirstClick)
  112. {
  113. string gainPosition = this.labelDateMarkStyleModel.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(dateStr, 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 labelDateMarkStyleModel;
  222. }
  223. public override string GetContent()
  224. {
  225. return dateStr;
  226. }
  227. }
  228. }