MeasureSquare.cs 46 KB

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