MeasureRectangle.cs 43 KB

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