DrawTextString.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Globalization;
  8. using System.Runtime.Serialization;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.Annotation.Label
  11. {
  12. /// <summary>
  13. /// 标注->文本
  14. /// </summary>
  15. public class DrawTextString : DrawObject
  16. {
  17. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationLine.cur"));
  18. /// <summary>
  19. /// 标注样式信息model
  20. /// </summary>
  21. public LabelStyleModel.Text labelTextModel;
  22. /// <summary>
  23. /// 辅助为第一次创建文本矩形对象赋值
  24. /// </summary>
  25. public bool isFirstClick = true;
  26. public DrawTextString(ISurfaceBox surfaceBox) : base()
  27. {
  28. this.objectType = DrawClass.Label;
  29. this.drawToolType = DrawToolType.DrawTextString;
  30. Initialize();
  31. }
  32. public DrawTextString(ISurfaceBox surfaceBox, int x1, int y1, string textStr) : base()
  33. {
  34. this.objectType = DrawClass.Label;
  35. this.drawToolType = DrawToolType.DrawTextString;
  36. startPoint.X = x1;
  37. startPoint.Y = y1;
  38. textboxMessage = textStr;
  39. labelTextModel = surfaceBox.GetLabelStyleModel().text;
  40. Initialize();
  41. }
  42. public DrawTextString(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  43. {
  44. this.objectType = DrawClass.Label;
  45. this.drawToolType = DrawToolType.DrawTextString;
  46. this.ISurfaceBox = surfaceBox;
  47. labelTextModel = (LabelStyleModel.Text)parentStyleModel;
  48. startPoint = points[0];
  49. rectangle.X = points[0].X;
  50. rectangle.Y = points[0].Y;
  51. rectangle.Width = Math.Abs(points[1].X - points[0].X);
  52. rectangle.Height = Math.Abs(points[1].Y - points[0].Y);
  53. textboxMessage = content.ToString();
  54. isFirstClick = false;
  55. }
  56. /// <summary>
  57. /// Clone this instance
  58. /// </summary>
  59. public override DrawObject Clone()
  60. {
  61. DrawTextString textString = new DrawTextString(ISurfaceBox);
  62. textString.objectType = DrawClass.Label;
  63. textString.drawToolType = DrawToolType.DrawTextString;
  64. textString.ISurfaceBox = this.ISurfaceBox;
  65. textString.startPoint = this.startPoint;
  66. textString.textboxMessage = this.textboxMessage;
  67. textString.rectangle = this.rectangle;
  68. textString.isFirstClick = false;
  69. textString.labelTextModel = this.labelTextModel;
  70. FillDrawObjectFields(textString);
  71. return textString;
  72. }
  73. public override DrawObject Clone(ISurfaceBox surfaceBox)
  74. {
  75. DrawTextString textString = new DrawTextString(surfaceBox);
  76. textString.objectType = DrawClass.Label;
  77. textString.drawToolType = DrawToolType.DrawTextString;
  78. textString.ISurfaceBox = surfaceBox;
  79. textString.startPoint = this.startPoint;
  80. textString.textboxMessage = this.textboxMessage;
  81. textString.rectangle = this.rectangle;
  82. textString.isFirstClick = false;
  83. textString.labelTextModel = this.labelTextModel;
  84. FillDrawObjectFields(textString);
  85. return textString;
  86. }
  87. public override void Draw(Graphics g)
  88. {
  89. g.SmoothingMode = SmoothingMode.AntiAlias;
  90. Font textfont = new Font(this.labelTextModel.font, this.labelTextModel.fontSize);
  91. Brush textbrush = new SolidBrush(Color.FromArgb(this.labelTextModel.textColor));
  92. Color color = Color.FromArgb(this.labelTextModel.backgroundBoxColor);
  93. Pen pen = new Pen(color, this.labelTextModel.lineWidth);
  94. Color fillColor = Color.FromArgb(this.labelTextModel.backgroundColor);
  95. SolidBrush fillBrush = new SolidBrush(fillColor);
  96. if (isFirstClick)
  97. {
  98. Size fontSize = g.MeasureString(textboxMessage, textfont).ToSize();
  99. rectangle.X = startPoint.X;
  100. rectangle.Y = startPoint.Y;
  101. rectangle.Width = fontSize.Width;
  102. rectangle.Height = fontSize.Height;
  103. isFirstClick = false;
  104. }
  105. g.FillRectangle(fillBrush, rectangle);
  106. g.DrawRectangle(pen, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
  107. g.DrawString(textboxMessage, textfont, textbrush, rectangle);
  108. fillBrush.Dispose();
  109. pen.Dispose();
  110. }
  111. public override int HandleCount
  112. {
  113. get
  114. {
  115. return 8;
  116. }
  117. }
  118. protected void SetRectangle(float x, float y, float width, float height)
  119. {
  120. this.rectangle.X = x;
  121. this.rectangle.Y = y;
  122. this.rectangle.Width = width;
  123. this.rectangle.Height = height;
  124. }
  125. /// <summary>
  126. /// Get handle pointscroll by 1-based number
  127. /// </summary>
  128. /// <param name="handleNumber"></param>
  129. /// <returns></returns>
  130. public override PointF GetHandle(int handleNumber)
  131. {
  132. float x, y, xCenter, yCenter;
  133. xCenter = this.rectangle.X + this.rectangle.Width / 2;
  134. yCenter = this.rectangle.Y + this.rectangle.Height / 2;
  135. x = this.rectangle.X;
  136. y = this.rectangle.Y;
  137. switch (handleNumber)
  138. {
  139. case 1:
  140. x = this.rectangle.X;
  141. y = this.rectangle.Y;
  142. break;
  143. case 2:
  144. x = xCenter;
  145. y = this.rectangle.Y;
  146. break;
  147. case 3:
  148. x = this.rectangle.Right;
  149. y = this.rectangle.Y;
  150. break;
  151. case 4:
  152. x = this.rectangle.Right;
  153. y = yCenter;
  154. break;
  155. case 5:
  156. x = this.rectangle.Right;
  157. y = this.rectangle.Bottom;
  158. break;
  159. case 6:
  160. x = xCenter;
  161. y = this.rectangle.Bottom;
  162. break;
  163. case 7:
  164. x = this.rectangle.X;
  165. y = this.rectangle.Bottom;
  166. break;
  167. case 8:
  168. x = this.rectangle.X;
  169. y = yCenter;
  170. break;
  171. }
  172. return new PointF(x, y);
  173. }
  174. /// <summary>
  175. /// Hit test.
  176. /// Return value: -1 - no hit
  177. /// 0 - hit anywhere
  178. /// > 1 - handle number
  179. /// </summary>
  180. /// <param name="pointscroll"></param>
  181. /// <returns></returns>
  182. public override int HitTest(Point point)
  183. {
  184. if (Selected)
  185. {
  186. for (int i = 1; i <= HandleCount; i++)
  187. {
  188. if (GetHandleRectangle(i).Contains(point))
  189. return i;
  190. }
  191. }
  192. if (PointInObject(point))
  193. return 0;
  194. return -1;
  195. }
  196. protected override bool PointInObject(Point point)
  197. {
  198. return rectangle.Contains(point);
  199. }
  200. public override bool IntersectsWith(Rectangle rectangle)
  201. {
  202. return Rectangle.IntersectsWith(rectangle);
  203. }
  204. public override Cursor GetHandleCursor(int handleNumber)
  205. {
  206. switch (handleNumber)
  207. {
  208. case 1:
  209. return Cursors.SizeNWSE;
  210. case 2:
  211. return Cursors.SizeNS;
  212. case 3:
  213. return Cursors.SizeNESW;
  214. case 4:
  215. return Cursors.SizeWE;
  216. case 5:
  217. return Cursors.SizeNWSE;
  218. case 6:
  219. return Cursors.SizeNS;
  220. case 7:
  221. return Cursors.SizeNESW;
  222. case 8:
  223. return Cursors.SizeWE;
  224. default:
  225. return Cursors.Default;
  226. }
  227. }
  228. public override void MoveHandleTo(Point point, int handleNumber)
  229. {
  230. float left = rectangle.Left;
  231. float top = rectangle.Top;
  232. float right = rectangle.Right;
  233. float bottom = rectangle.Bottom;
  234. switch (handleNumber)
  235. {
  236. case 1:
  237. left = point.X;
  238. top = point.Y;
  239. break;
  240. case 2:
  241. top = point.Y;
  242. break;
  243. case 3:
  244. right = point.X;
  245. top = point.Y;
  246. break;
  247. case 4:
  248. right = point.X;
  249. break;
  250. case 5:
  251. right = point.X;
  252. bottom = point.Y;
  253. break;
  254. case 6:
  255. bottom = point.Y;
  256. break;
  257. case 7:
  258. left = point.X;
  259. bottom = point.Y;
  260. break;
  261. case 8:
  262. left = point.X;
  263. break;
  264. }
  265. SetRectangle(left, top, right - left, bottom - top);
  266. }
  267. public override void Move(int deltaX, int deltaY)
  268. {
  269. int x = ISurfaceBox.UnscaleScalar(deltaX);
  270. int y = ISurfaceBox.UnscaleScalar(deltaY);
  271. startPoint.X += x;
  272. startPoint.Y += y;
  273. rectangle.X += x;
  274. rectangle.Y += y;
  275. }
  276. public override RectangleF GetBoundingBox()
  277. {
  278. return rectangle;
  279. }
  280. public override List<PointF> GetPoints()
  281. {
  282. List<PointF> points = new List<PointF>();
  283. points.Add(new PointF(rectangle.X, rectangle.Y));
  284. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  285. return points;
  286. }
  287. public override ParentStyleModel GetStyle()
  288. {
  289. return labelTextModel;
  290. }
  291. public override string GetContent()
  292. {
  293. return textboxMessage;
  294. }
  295. }
  296. }