DrawGainNumber.cs 9.0 KB

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