MeasureHLine.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using PaintDotNet.Base.CommTool;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Runtime.Serialization;
  11. using System.Windows.Forms;
  12. namespace PaintDotNet.Annotation.Measure
  13. {
  14. using PointList = List<PointF>;
  15. /// <summary>
  16. /// 测量->长度测量->水平线
  17. /// </summary>
  18. public class MeasureHLine : MeasureDrawObject
  19. {
  20. /// <summary>
  21. /// Graphic objects for hit test
  22. /// </summary>
  23. private GraphicsPath areaPath = null;
  24. /// <summary>
  25. /// Pen
  26. /// </summary>
  27. private Pen areaPen = null;
  28. /// <summary>
  29. /// Region
  30. /// </summary>
  31. private Region areaRegion = null;
  32. /// <summary>
  33. /// 保存为文件的时候的标记,可能用不到
  34. /// </summary>
  35. private const string entryStart = "Start";
  36. /// <summary>
  37. /// 保存为文件的时候的标记,可能用不到
  38. /// </summary>
  39. private const string entryEnd = "End";
  40. /// <summary>
  41. /// 直线长度
  42. /// </summary>
  43. //private double length = 0.0;
  44. public PointList pointArray;
  45. private MeasureStyleModel.MeasureHLine measureHLine;
  46. /// <summary>
  47. /// 文本矩形定义
  48. /// </summary>
  49. private RectangleF rectangleF1 = new RectangleF();
  50. private RectangleF rectangleF2 = new RectangleF();
  51. private RectangleF rectangleF3 = new RectangleF();
  52. private RectangleF rectangleF4 = new RectangleF();
  53. private RectangleF rectangleF5 = new RectangleF();
  54. private RectangleF rectangleF6 = new RectangleF();
  55. private RectangleF rectangleF7 = new RectangleF();
  56. private RectangleF rectangleF8 = new RectangleF();
  57. private RectangleF rectangleF9 = new RectangleF();
  58. /// <summary>
  59. /// 文本上用于拖动的点
  60. /// </summary>
  61. private Point pointL = new Point();
  62. /// <summary>
  63. /// 绘制限制(第一次绘制时)
  64. /// </summary>
  65. public bool pointChange = true;
  66. /// <summary>
  67. /// 属性绘制点
  68. /// </summary>
  69. private PointF rotationPoint;
  70. /// <summary>
  71. /// 区分移动文本
  72. /// </summary>
  73. private int moveKb;
  74. /// <summary>
  75. /// 绘制限制(从配置文件加载)
  76. /// </summary>
  77. public bool configurationFile = false;
  78. /// <summary>
  79. /// 绘制限制(更改属性时)
  80. /// </summary>
  81. private bool SavePointChange;
  82. /// <summary>
  83. /// 绘制属性
  84. /// </summary>
  85. private string[] drawingPropertiesList;
  86. /// <summary>
  87. /// 绘制属性(克隆)
  88. /// </summary>
  89. private string[] drawingPropertiesListClone;
  90. /// <summary>
  91. /// 是否移动了鼠标
  92. /// </summary>
  93. public bool moved = false;
  94. /// <summary>
  95. /// 限制绘制(绘制中)
  96. /// </summary>
  97. public bool mouseUpPointChange = false;
  98. public MeasureHLine(ISurfaceBox surfaceBox, PointF x, PointF y) : base(surfaceBox)
  99. {
  100. this.objectType = DrawClass.Measure;
  101. this.drawToolType = DrawToolType.MeasureHLine;
  102. startPoint =x;
  103. endPoint=y;
  104. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  105. this.unitString = surfaceBox.GetPxPerUnit()[1];
  106. this.unit = surfaceBox.GetPxPerUnit()[2];
  107. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  108. measureHLine = this.ISurfaceBox.GetMeasureStyleModel().measureHLine;
  109. }
  110. public MeasureHLine(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  111. {
  112. this.objectType = DrawClass.Measure;
  113. this.drawToolType = DrawToolType.MeasureHLine;
  114. this.ISurfaceBox = surfaceBox;
  115. measureHLine = (MeasureStyleModel.MeasureHLine)parentStyleModel;
  116. this.startPoint = points[0];
  117. this.endPoint = points[1];
  118. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  119. this.unitString = surfaceBox.GetPxPerUnit()[1];
  120. this.unit = surfaceBox.GetPxPerUnit()[2];
  121. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  122. this.configurationFile = true;
  123. this.moved = true;
  124. }
  125. /// <summary>
  126. /// Clone this instance
  127. /// </summary>
  128. public override DrawObject Clone()
  129. {
  130. MeasureHLine drawLine = new MeasureHLine(ISurfaceBox, this.startPoint, this.endPoint);
  131. if (drawingPropertiesList != null)
  132. drawLine.drawingPropertiesListClone = drawingPropertiesList;
  133. drawLine.moved = true;
  134. FillDrawObjectFields(drawLine);
  135. return drawLine;
  136. }
  137. public override DrawObject Clone(ISurfaceBox surfaceBox)
  138. {
  139. MeasureHLine drawLine = new MeasureHLine(surfaceBox, this.GetPoints(), this.measureHLine, null);
  140. if (drawingPropertiesList != null)
  141. drawLine.drawingPropertiesListClone = drawingPropertiesList;
  142. drawLine.moved = true;
  143. FillDrawObjectFields(drawLine);
  144. return drawLine;
  145. }
  146. /// <summary>
  147. /// 测量属性
  148. /// </summary>
  149. /// <returns></returns>
  150. public override Dictionary<System.Enum, object> GetData()
  151. {
  152. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  153. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text");
  154. else
  155. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"));
  156. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  157. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  158. else
  159. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  160. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  161. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  162. else
  163. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  164. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  165. data[MeasureAttributes.PixelStartX] = startPoint.X;
  166. else
  167. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  168. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  169. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  170. else
  171. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  172. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  173. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  174. else
  175. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  176. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  177. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  178. else
  179. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  180. if (data.ContainsKey(MeasureAttributes.PixelLength))
  181. data[MeasureAttributes.PixelLength] = Math.Round(length, decimalPlaces);
  182. else
  183. data.Add(MeasureAttributes.PixelLength, Math.Round(length, decimalPlaces));
  184. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  185. if (s.IndexOf(".") == -1)
  186. {
  187. for (int i = 0; i < decimalPlaces; i++)
  188. {
  189. if (i == 0)
  190. s += ".";
  191. s += "0";
  192. }
  193. }
  194. else
  195. {
  196. int a = s.Length - s.IndexOf(".") - 1;
  197. if (a < decimalPlaces)
  198. {
  199. for (int i = 0; i < decimalPlaces - a; i++)
  200. {
  201. s += "0";
  202. }
  203. }
  204. }
  205. if (data.ContainsKey(MeasureAttributes.PhysicalLength))
  206. data[MeasureAttributes.PhysicalLength] = s;
  207. else
  208. data.Add(MeasureAttributes.PhysicalLength, s);
  209. return data;
  210. }
  211. public override void Draw(Graphics g)
  212. {
  213. if(this.moved)
  214. {
  215. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  216. if (drawingPropertiesList == null)
  217. {
  218. drawingPropertiesList = drawingPropertiesListClone;
  219. this.configurationFile = true;
  220. }
  221. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  222. Font textfont = new Font(measureHLine.font, measureHLine.fontSize);
  223. Brush textbrush = new SolidBrush(Color.FromArgb(measureHLine.textColor));
  224. Pen linePen = new Pen(Color.FromArgb(measureHLine.lineColor), measureHLine.lineWidth);
  225. linePen.DashStyle = (DashStyle)measureHLine.lineStyle;
  226. g.SmoothingMode = SmoothingMode.AntiAlias;
  227. // 水平线长度
  228. length = BasicCalculationHelper.GetDistance(startPoint, endPoint, 10);
  229. PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(startPoint.X, startPoint.Y + measureHLine.vLineLength), startPoint, 0);
  230. PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(startPoint.X, startPoint.Y - measureHLine.vLineLength), startPoint, 0);
  231. PointF point2 = BasicCalculationHelper.GetAnglePoint(new PointF(endPoint.X, endPoint.Y + measureHLine.vLineLength), endPoint, 180);
  232. PointF point3 = BasicCalculationHelper.GetAnglePoint(new PointF(endPoint.X, endPoint.Y - measureHLine.vLineLength), endPoint, 180);
  233. g.DrawLine(linePen, point.X, point.Y, point1.X, point1.Y);
  234. g.DrawLine(linePen, point2.X, point2.Y, point3.X, point3.Y);
  235. // 绘制水平线
  236. g.DrawLine(linePen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  237. SizeF sizeF = new SizeF();
  238. int offsetValuePoint = measureHLine.fontSize + measureHLine.fontSize / 2;
  239. int offsetValue1Point = measureHLine.lineWidth * 2 / 3;
  240. int offsetPoint = offsetValue1Point;
  241. if (drawingPropertiesList != null)
  242. {
  243. // 像素长度
  244. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  245. {
  246. offsetPoint += offsetValuePoint;
  247. }
  248. // 物理长度
  249. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  250. {
  251. offsetPoint += offsetValuePoint;
  252. }
  253. // 测量方式
  254. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  255. {
  256. offsetPoint += offsetValuePoint;
  257. }
  258. // 测量单位(中文)
  259. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  260. {
  261. offsetPoint += offsetValuePoint;
  262. }
  263. // 测量单位(英文)
  264. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  265. {
  266. offsetPoint += offsetValuePoint;
  267. }
  268. // 像素起始点X
  269. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  270. {
  271. offsetPoint += offsetValuePoint;
  272. }
  273. // 像素起始点Y
  274. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  275. {
  276. offsetPoint += offsetValuePoint;
  277. }
  278. // 物理起始点X
  279. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  280. {
  281. offsetPoint += offsetValuePoint;
  282. }
  283. // 物理起始点Y
  284. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  285. {
  286. offsetPoint += offsetValuePoint;
  287. }
  288. }
  289. if(measureHLine.linePosition == 0)
  290. this.rotationPoint = new PointF(startPoint.X, startPoint.Y - offsetPoint - offsetValue1Point);
  291. else if (measureHLine.linePosition == 1)
  292. this.rotationPoint = new PointF((startPoint.X + endPoint.X) / 2, startPoint.Y - offsetPoint - offsetValue1Point);
  293. else if (measureHLine.linePosition == 2)
  294. this.rotationPoint = new PointF(endPoint.X, endPoint.Y - offsetPoint - offsetValue1Point);
  295. else if (measureHLine.linePosition == 3)
  296. this.rotationPoint = startPoint;
  297. else if (measureHLine.linePosition == 4)
  298. this.rotationPoint = new PointF(Math.Abs(startPoint.X + endPoint.X) / 2, Math.Abs(startPoint.Y + endPoint.Y) / 2);
  299. else if (measureHLine.linePosition == 5)
  300. this.rotationPoint = endPoint;
  301. // 是否绘制
  302. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  303. {
  304. // 属性文本的间隔定义
  305. int offsetValue = measureHLine.fontSize + measureHLine.fontSize / 2;
  306. int offsetValue1 = measureHLine.lineWidth * 2 / 3;
  307. int offset = offsetValue1;
  308. this.pointL = new Point((int)rotationPoint.X, (int)rotationPoint.Y);
  309. if (drawingPropertiesList != null)
  310. {
  311. // 像素长度
  312. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  313. {
  314. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  315. rectangleF8.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  316. offset += offsetValue;
  317. }
  318. // 物理长度
  319. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  320. {
  321. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  322. if (s.IndexOf(".") == -1)
  323. {
  324. for (int i = 0; i < decimalPlaces; i++)
  325. {
  326. if (i == 0)
  327. s += ".";
  328. s += "0";
  329. }
  330. }
  331. else
  332. {
  333. int a = s.Length - s.IndexOf(".") - 1;
  334. if (a < decimalPlaces)
  335. {
  336. for (int i = 0; i < decimalPlaces - a; i++)
  337. {
  338. s += "0";
  339. }
  340. }
  341. }
  342. sizeF = g.MeasureString(s + this.unit, textfont);
  343. rectangleF9.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  344. offset += offsetValue;
  345. }
  346. // 测量方式
  347. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  348. {
  349. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  350. rectangleF1.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  351. offset += offsetValue;
  352. }
  353. // 测量单位(中文)
  354. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  355. {
  356. sizeF = g.MeasureString("" + this.unitString, textfont);
  357. rectangleF2.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  358. offset += offsetValue;
  359. }
  360. // 测量单位(英文)
  361. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  362. {
  363. sizeF = g.MeasureString("" + this.unit, textfont);
  364. rectangleF3.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  365. offset += offsetValue;
  366. }
  367. // 像素起始点X
  368. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  369. {
  370. sizeF = g.MeasureString("" + startPoint.X, textfont);
  371. rectangleF4.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  372. offset += offsetValue;
  373. }
  374. // 像素起始点Y
  375. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  376. {
  377. sizeF = g.MeasureString("" + startPoint.Y, textfont);
  378. rectangleF5.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  379. offset += offsetValue;
  380. }
  381. // 物理起始点X
  382. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  383. {
  384. sizeF = g.MeasureString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  385. rectangleF6.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  386. offset += offsetValue;
  387. }
  388. // 物理起始点Y
  389. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  390. {
  391. sizeF = g.MeasureString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  392. rectangleF7.Location = new Point((int)rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  393. offset += offsetValue;
  394. }
  395. }
  396. }
  397. if (drawingPropertiesList != null)
  398. {
  399. // 测量方式
  400. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  401. {
  402. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  403. rectangleF1.Width = sizeF.Width;
  404. rectangleF1.Height = sizeF.Height;
  405. g.DrawString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont, textbrush, rectangleF1);
  406. }
  407. else
  408. {
  409. rectangleF1 = new RectangleF();
  410. }
  411. // 测量单位(中文)
  412. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  413. {
  414. sizeF = g.MeasureString("" + this.unitString, textfont);
  415. rectangleF2.Width = sizeF.Width;
  416. rectangleF2.Height = sizeF.Height;
  417. g.DrawString("" + this.unitString, textfont, textbrush, rectangleF2);
  418. }
  419. else
  420. {
  421. rectangleF2 = new RectangleF();
  422. }
  423. // 测量单位(英文)
  424. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  425. {
  426. sizeF = g.MeasureString("" + this.unit, textfont);
  427. rectangleF3.Width = sizeF.Width;
  428. rectangleF3.Height = sizeF.Height;
  429. g.DrawString("" + this.unit, textfont, textbrush, rectangleF3);
  430. }
  431. else
  432. {
  433. rectangleF3 = new RectangleF();
  434. }
  435. // 像素起始点X
  436. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  437. {
  438. sizeF = g.MeasureString("" + startPoint.X, textfont);
  439. rectangleF4.Width = sizeF.Width;
  440. rectangleF4.Height = sizeF.Height;
  441. g.DrawString("" + startPoint.X, textfont, textbrush, rectangleF4);
  442. }
  443. else
  444. {
  445. rectangleF4 = new RectangleF();
  446. }
  447. // 像素起始点Y
  448. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  449. {
  450. sizeF = g.MeasureString("" + startPoint.Y, textfont);
  451. rectangleF5.Width = sizeF.Width;
  452. rectangleF5.Height = sizeF.Height;
  453. g.DrawString("" + startPoint.Y, textfont, textbrush, rectangleF5);
  454. }
  455. else
  456. {
  457. rectangleF5 = new RectangleF();
  458. }
  459. // 物理起始点X
  460. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  461. {
  462. sizeF = g.MeasureString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  463. rectangleF6.Width = sizeF.Width;
  464. rectangleF6.Height = sizeF.Height;
  465. g.DrawString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  466. }
  467. else
  468. {
  469. rectangleF6 = new RectangleF();
  470. }
  471. // 物理起始点Y
  472. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  473. {
  474. sizeF = g.MeasureString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  475. rectangleF7.Width = sizeF.Width;
  476. rectangleF7.Height = sizeF.Height;
  477. g.DrawString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  478. }
  479. else
  480. {
  481. rectangleF7 = new RectangleF();
  482. }
  483. // 像素长度
  484. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  485. {
  486. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  487. rectangleF8.Width = sizeF.Width;
  488. rectangleF8.Height = sizeF.Height;
  489. g.DrawString("" + Math.Round(length, decimalPlaces) + "px", textfont, textbrush, rectangleF8);
  490. }
  491. else
  492. {
  493. rectangleF8 = new RectangleF();
  494. }
  495. // 物理长度
  496. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  497. {
  498. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  499. if (s.IndexOf(".") == -1)
  500. {
  501. for (int i = 0; i < decimalPlaces; i++)
  502. {
  503. if (i == 0)
  504. s += ".";
  505. s += "0";
  506. }
  507. }
  508. else
  509. {
  510. int a = s.Length - s.IndexOf(".") - 1;
  511. if (a < decimalPlaces)
  512. {
  513. for (int i = 0; i < decimalPlaces - a; i++)
  514. {
  515. s += "0";
  516. }
  517. }
  518. }
  519. sizeF = g.MeasureString(s + this.unit, textfont);
  520. rectangleF9.Width = sizeF.Width;
  521. rectangleF9.Height = sizeF.Height;
  522. g.DrawString(s + this.unit, textfont, textbrush, rectangleF9);
  523. }
  524. else
  525. {
  526. rectangleF9 = new RectangleF();
  527. }
  528. }
  529. this.pointArray = new PointList();
  530. this.pointArray.Add(this.startPoint);
  531. this.pointArray.Add(this.endPoint);
  532. linePen.Dispose();
  533. if (this.configurationFile)
  534. this.pointChange = false;
  535. this.mouseUpAttribute = false;
  536. pointChangeObject.Remove(this.drawToolType);
  537. }
  538. }
  539. /// <summary>
  540. /// 停止绘制时
  541. /// </summary>
  542. /// <param name="up"></param>
  543. public override void MouseUp(bool up)
  544. {
  545. mouseUpPointChange = up;
  546. }
  547. public override int HandleCount
  548. {
  549. get
  550. {
  551. return 3;
  552. }
  553. }
  554. /// <summary>
  555. /// Get handle pointscroll by 1-based number
  556. /// </summary>
  557. /// <param name="handleNumber"></param>
  558. /// <returns></returns>
  559. public override PointF GetHandle(int handleNumber)
  560. {
  561. if (handleNumber == 1)
  562. return startPoint;
  563. else if (handleNumber == 2)
  564. return endPoint;
  565. else
  566. return pointL;
  567. }
  568. /// <summary>
  569. /// Hit test.
  570. /// Return value: -1 - no hit
  571. /// 0 - hit anywhere
  572. /// > 1 - handle number
  573. /// </summary>
  574. /// <param name="pointscroll"></param>
  575. /// <returns></returns>
  576. public override int HitTest(Point point)
  577. {
  578. if (Selected)
  579. {
  580. for (int i = 1; i <= HandleCount; i++)
  581. {
  582. if (GetHandleRectangle(i).Contains(point))
  583. return i;
  584. }
  585. if (this.rectangleF1.Contains(point))
  586. {
  587. this.pointL = point;
  588. moveKb = 1;
  589. return 3;
  590. }
  591. else if (this.rectangleF2.Contains(point))
  592. {
  593. this.pointL = point;
  594. moveKb = 2;
  595. return 3;
  596. }
  597. else if (this.rectangleF3.Contains(point))
  598. {
  599. this.pointL = point;
  600. moveKb = 3;
  601. return 3;
  602. }
  603. else if (this.rectangleF4.Contains(point))
  604. {
  605. this.pointL = point;
  606. moveKb = 4;
  607. return 3;
  608. }
  609. else if (this.rectangleF5.Contains(point))
  610. {
  611. this.pointL = point;
  612. moveKb = 5;
  613. return 3;
  614. }
  615. else if (this.rectangleF6.Contains(point))
  616. {
  617. this.pointL = point;
  618. moveKb = 6;
  619. return 3;
  620. }
  621. else if (this.rectangleF7.Contains(point))
  622. {
  623. this.pointL = point;
  624. moveKb = 7;
  625. return 3;
  626. }
  627. else if (this.rectangleF8.Contains(point))
  628. {
  629. this.pointL = point;
  630. moveKb = 8;
  631. return 3;
  632. }
  633. else if (this.rectangleF9.Contains(point))
  634. {
  635. this.pointL = point;
  636. moveKb = 9;
  637. return 3;
  638. }
  639. }
  640. if (PointInObject(point))
  641. return 0;
  642. return -1;
  643. }
  644. protected override bool PointInObject(Point point)
  645. {
  646. CreateObjects();
  647. return AreaRegion.IsVisible(point);
  648. }
  649. public override bool IntersectsWith(Rectangle rectangle)
  650. {
  651. CreateObjects();
  652. return AreaRegion.IsVisible(rectangle);
  653. }
  654. public override Cursor GetHandleCursor(int handleNumber)
  655. {
  656. switch (handleNumber)
  657. {
  658. case 1:
  659. case 2:
  660. case 3:
  661. return handleCursor;// Cursors.SizeAll;
  662. default:
  663. return Cursors.Default;
  664. }
  665. }
  666. public override void MoveHandleTo(Point point, int handleNumber)
  667. {
  668. if (handleNumber == 1)
  669. {
  670. this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  671. this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  672. this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  673. this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  674. this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  675. this.rectangleF6.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  676. this.rectangleF7.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  677. this.rectangleF8.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  678. this.rectangleF9.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  679. this.mouseUpPointChange = true;
  680. startPoint.X = point.X;
  681. }
  682. else if (handleNumber == 2)
  683. {
  684. this.mouseUpPointChange = true;
  685. endPoint.X = point.X;
  686. }
  687. else
  688. {
  689. if (this.moveKb == 1)
  690. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  691. else if (this.moveKb == 2)
  692. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  693. else if (this.moveKb == 3)
  694. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  695. else if (this.moveKb == 4)
  696. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  697. else if (this.moveKb == 5)
  698. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  699. else if (this.moveKb == 6)
  700. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  701. else if (this.moveKb == 7)
  702. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  703. else if (this.moveKb == 8)
  704. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  705. else if (this.moveKb == 9)
  706. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  707. this.pointL = point;
  708. }
  709. Invalidate();
  710. }
  711. public override void Move(int deltaX, int deltaY)
  712. {
  713. int x = ISurfaceBox.UnscaleScalar(deltaX);
  714. int y = ISurfaceBox.UnscaleScalar(deltaY);
  715. startPoint.X += x;
  716. startPoint.Y += y;
  717. endPoint.X += x;
  718. endPoint.Y += y;
  719. pointL.X += x;
  720. pointL.Y += y;
  721. this.rectangleF1.Offset(x, y);
  722. this.rectangleF2.Offset(x, y);
  723. this.rectangleF3.Offset(x, y);
  724. this.rectangleF4.Offset(x, y);
  725. this.rectangleF5.Offset(x, y);
  726. this.rectangleF6.Offset(x, y);
  727. this.rectangleF7.Offset(x, y);
  728. this.rectangleF8.Offset(x, y);
  729. this.rectangleF9.Offset(x, y);
  730. Invalidate();
  731. }
  732. /// <summary>
  733. /// Invalidate object.
  734. /// When object is invalidated, path used for hit test
  735. /// is released and should be created again.
  736. /// </summary>
  737. protected void Invalidate()
  738. {
  739. if (AreaPath != null)
  740. {
  741. AreaPath.Dispose();
  742. AreaPath = null;
  743. }
  744. if (AreaPen != null)
  745. {
  746. AreaPen.Dispose();
  747. AreaPen = null;
  748. }
  749. if (AreaRegion != null)
  750. {
  751. AreaRegion.Dispose();
  752. AreaRegion = null;
  753. }
  754. }
  755. /// <summary>
  756. /// Create graphic objects used from hit test.
  757. /// </summary>
  758. protected virtual void CreateObjects()
  759. {
  760. if (AreaPath != null)
  761. return;
  762. // Create path which contains wide line
  763. // for easy mouse selection
  764. AreaPath = new GraphicsPath();
  765. AreaPen = new Pen(Color.Black, 7);
  766. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  767. AreaPath.Widen(AreaPen);
  768. // Create region from the path
  769. AreaRegion = new Region(AreaPath);
  770. }
  771. protected GraphicsPath AreaPath
  772. {
  773. get
  774. {
  775. return areaPath;
  776. }
  777. set
  778. {
  779. areaPath = value;
  780. }
  781. }
  782. protected Pen AreaPen
  783. {
  784. get
  785. {
  786. return areaPen;
  787. }
  788. set
  789. {
  790. areaPen = value;
  791. }
  792. }
  793. protected Region AreaRegion
  794. {
  795. get
  796. {
  797. return areaRegion;
  798. }
  799. set
  800. {
  801. areaRegion = value;
  802. }
  803. }
  804. public override RectangleF GetBoundingBox()
  805. {
  806. return GetNotmalizedRectangle(startPoint, endPoint);
  807. }
  808. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  809. {
  810. RectangleF rect = new RectangleF();
  811. if (a.X < b.X)
  812. {
  813. rect.X = a.X;
  814. rect.Width = b.X - a.X;
  815. }
  816. else
  817. {
  818. rect.X = b.X;
  819. rect.Width = a.X - b.X;
  820. }
  821. if (a.Y < b.Y)
  822. {
  823. rect.Y = a.Y;
  824. rect.Height = b.Y - a.Y;
  825. }
  826. else
  827. {
  828. rect.Y = b.Y;
  829. rect.Height = a.Y - b.Y;
  830. }
  831. return rect;
  832. }
  833. /// <summary>
  834. /// Draw tracker for selected object
  835. /// </summary>
  836. /// <param name="g"></param>
  837. public override void DrawTracker(Graphics g)
  838. {
  839. if (!Selected)
  840. return;
  841. SolidBrush brush = new SolidBrush(Color.Black);
  842. for (int i = 1; i <= HandleCount; i++)
  843. {
  844. if (i == 3)
  845. brush = new SolidBrush(Color.Transparent);
  846. g.FillRectangle(brush, GetHandleRectangle(i));
  847. }
  848. brush.Dispose();
  849. }
  850. public override List<PointF> GetPoints()
  851. {
  852. return pointArray;
  853. }
  854. public override ParentStyleModel GetStyle()
  855. {
  856. return measureHLine;
  857. }
  858. }
  859. }