DrawObjects.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace SIASUN.Autopilot.View.Helper
  10. {
  11. /// <summary>
  12. /// DrawableObjectsCS 的摘要说明。
  13. /// </summary>
  14. //基类
  15. public abstract class DShape
  16. {
  17. public abstract void Draw(Graphics g);
  18. protected RectangleF bounding;
  19. public PointF[] pointlist;
  20. public abstract void pointchange(PointF[] p);
  21. public virtual GraphicsPath pathChange(GraphicsPath path, bool pick)
  22. {
  23. return null;
  24. }
  25. protected Color penColor;
  26. protected float penWidth;
  27. }
  28. //填充图形接口
  29. public interface IFillable
  30. {
  31. void Fill(Graphics g);
  32. Color FillBrushColor
  33. {
  34. get;
  35. set;
  36. }
  37. }
  38. //画线
  39. public class DLine : DShape
  40. {
  41. protected PointF p1;
  42. protected PointF p2;
  43. public DLine(PointF[] p, Color penColor, float penWidth)
  44. {
  45. this.pointchange(p);
  46. this.penColor = penColor;
  47. this.penWidth = penWidth;
  48. }
  49. public override void pointchange(PointF[] p)
  50. {
  51. pointlist = new PointF[p.Length];
  52. int i = 0;
  53. foreach (PointF tempPoint in p)
  54. {
  55. pointlist[i++] = tempPoint;
  56. }
  57. p1 = pointlist[0];
  58. p2 = pointlist[1];
  59. }
  60. public override void Draw(Graphics g)
  61. {
  62. using (Pen p = new Pen(penColor, penWidth))
  63. {
  64. g.DrawLine(p, p1, p2);
  65. }
  66. }
  67. }
  68. //画线--带样式
  69. public class DPLine : DShape
  70. {
  71. protected PointF p1;
  72. protected PointF p2;
  73. protected DashStyle style;
  74. protected System.Drawing.Drawing2D.LineCap scap;
  75. protected System.Drawing.Drawing2D.LineCap ecap;
  76. public DPLine(PointF start, PointF end, Color penColor, DashStyle style, System.Drawing.Drawing2D.LineCap scap, System.Drawing.Drawing2D.LineCap ecap)
  77. {
  78. p1 = start;
  79. p2 = end;
  80. this.penColor = penColor;
  81. this.style = style;
  82. this.scap = scap;
  83. this.ecap = ecap;
  84. }
  85. public override void pointchange(PointF[] p)
  86. {
  87. }
  88. public override void Draw(Graphics g)
  89. {
  90. using (Pen p = new Pen(penColor))
  91. {
  92. p.DashStyle = style;
  93. p.StartCap = scap;
  94. p.EndCap = ecap;
  95. p.Width = 8;
  96. g.DrawLine(p, p1, p2);
  97. }
  98. }
  99. }
  100. //画路径
  101. public class DPath : DShape
  102. {
  103. protected GraphicsPath Path;
  104. public DPath(GraphicsPath Path, Color penColor, float penWidth)
  105. {
  106. this.Path = Path;
  107. this.penColor = penColor;
  108. this.penWidth = penWidth;
  109. }
  110. public override void pointchange(PointF[] p)
  111. {
  112. }
  113. public override GraphicsPath pathChange(GraphicsPath path, bool pick)
  114. {
  115. if (pick == true)
  116. return this.Path;
  117. else
  118. {
  119. this.Path = path;
  120. return null;
  121. }
  122. }
  123. public override void Draw(Graphics g)
  124. {
  125. using (Pen p = new Pen(penColor, penWidth))
  126. {
  127. g.DrawPath(p, Path);
  128. }
  129. }
  130. public GraphicsPath mousePath
  131. {
  132. get
  133. {
  134. return Path;
  135. }
  136. }
  137. }
  138. //空心圆
  139. public class DHollowCircle : DShape
  140. {
  141. public DHollowCircle(PointF[] p, Color penColor, float penWidth)
  142. {
  143. this.pointchange(p);
  144. this.penColor = penColor;
  145. this.penWidth = penWidth;
  146. }
  147. public override void pointchange(PointF[] p)
  148. {
  149. //this.pointlist = new PointF[p.Length];
  150. //int i = 0;
  151. //foreach (PointF tempPoint in p)
  152. //{
  153. // pointlist[i++] = tempPoint;
  154. //}
  155. //int Distance = (int)Math.Sqrt(Math.Pow((pointlist[0].X - pointlist[1].X), 2.0) + Math.Pow((pointlist[0].Y - pointlist[1].Y), 2.0));
  156. //pointlist[0].Offset(-Distance, -Distance);
  157. //int diameter = Distance * 2;
  158. //bounding = new Rectangle(pointlist[0], new Size(diameter, diameter));
  159. }
  160. public override void Draw(Graphics g)
  161. {
  162. using (Pen p = new Pen(penColor, penWidth))
  163. {
  164. g.DrawEllipse(p, bounding);
  165. }
  166. }
  167. }
  168. //实心圆
  169. public class DFilledCircle : DHollowCircle, IFillable
  170. {
  171. protected Color brushColor;
  172. public DFilledCircle(PointF[] p, Color penColor, float penWidth, Color brushColor) : base(p, penColor, penWidth)
  173. {
  174. this.brushColor = brushColor;
  175. }
  176. public void Fill(Graphics g)
  177. {
  178. using (Brush b = new SolidBrush(brushColor))
  179. {
  180. g.FillEllipse(b, bounding);
  181. }
  182. }
  183. public Color FillBrushColor
  184. {
  185. get
  186. {
  187. return brushColor;
  188. }
  189. set
  190. {
  191. brushColor = value;
  192. }
  193. }
  194. public override void Draw(Graphics g)
  195. {
  196. Fill(g);
  197. //base.Draw(g);
  198. }
  199. }
  200. //画弧
  201. public class DArc : DHollowCircle
  202. {
  203. protected float angle1;
  204. protected float angle2;
  205. public DArc(PointF[] p, Color penColor, float penWidth, float angle1, float angle2) : base(p, penColor, penWidth)
  206. {
  207. this.angle1 = angle1;
  208. this.angle2 = angle2;
  209. }
  210. public override void Draw(Graphics g)
  211. {
  212. using (Pen p = new Pen(penColor, penWidth))
  213. {
  214. g.DrawArc(p, bounding, angle1, angle2);
  215. }
  216. }
  217. }
  218. //画多边形
  219. public class DPolygon : DShape
  220. {
  221. public DPolygon(PointF[] p, Color penColor, float penWidth)
  222. {
  223. this.pointchange(p);
  224. this.penColor = penColor;
  225. this.penWidth = penWidth;
  226. }
  227. public override void pointchange(PointF[] p)
  228. {
  229. pointlist = new PointF[p.Length];
  230. int i = 0;
  231. foreach (PointF tempPoint in p)
  232. {
  233. pointlist[i++] = tempPoint;
  234. }
  235. }
  236. public override void Draw(Graphics g)
  237. {
  238. using (Pen p = new Pen(penColor, penWidth))
  239. {
  240. g.DrawPolygon(p, pointlist);
  241. }
  242. }
  243. }
  244. //Beziers
  245. public class DBeziers : DPolygon
  246. {
  247. public DBeziers(PointF[] p, Color penColor, float penWidth) : base(p, penColor, penWidth)
  248. {
  249. }
  250. public override void Draw(Graphics g)
  251. {
  252. using (Pen p = new Pen(penColor, penWidth))
  253. {
  254. g.DrawBeziers(p, pointlist);
  255. }
  256. }
  257. }
  258. //Curve
  259. public class DCurve : DPolygon
  260. {
  261. public DCurve(PointF[] p, Color penColor, float penWidth) : base(p, penColor, penWidth)
  262. {
  263. }
  264. public override void Draw(Graphics g)
  265. {
  266. using (Pen p = new Pen(penColor, penWidth))
  267. {
  268. g.DrawCurve(p, pointlist);
  269. }
  270. }
  271. }
  272. //图像
  273. public class DImage : DShape
  274. {
  275. protected Image image;
  276. public DImage(PointF p, Image image)
  277. {
  278. bounding = new RectangleF(p, new SizeF(image.Width, image.Height));
  279. this.image = image;
  280. }
  281. public override void pointchange(PointF[] p)
  282. {
  283. }
  284. public override void Draw(Graphics g)
  285. {
  286. g.DrawImage(image, bounding);
  287. }
  288. }
  289. //图形集合
  290. public class DShapeList : CollectionBase
  291. {
  292. DrawCollection wholeList = new DrawCollection();
  293. public new int Count
  294. {
  295. get
  296. {
  297. return wholeList.Count;
  298. }
  299. }
  300. public void Add(DShape d)
  301. {
  302. wholeList.Add(d);
  303. }
  304. public new void RemoveAt(int i)
  305. {
  306. wholeList.RemoveAt(i);
  307. }
  308. public new void Clear()
  309. {
  310. wholeList.Clear();
  311. }
  312. //索引器
  313. public DShape this[int newshapeIndex]
  314. {
  315. get
  316. {
  317. return (DShape)wholeList[newshapeIndex];
  318. }
  319. set
  320. {
  321. wholeList[newshapeIndex] = value;
  322. }
  323. }
  324. public void DrawList(Graphics g)
  325. {
  326. if (wholeList.Count != 0)
  327. {
  328. foreach (DShape d in wholeList)
  329. d.Draw(g);
  330. }
  331. }
  332. //public IFillable[] GetFilledList()
  333. //{
  334. //return (IFillable[])filledList.ToArray(typeof(IFillable));
  335. //}
  336. }
  337. public class DrawCollection : CollectionBase
  338. {
  339. public void Add(DShape d)
  340. {
  341. List.Add(d);
  342. }
  343. public new void RemoveAt(int i)
  344. {
  345. List.RemoveAt(i);
  346. }
  347. public new void Clear()
  348. {
  349. List.Clear();
  350. }
  351. public DShape this[int shapeIndex]
  352. {
  353. get
  354. {
  355. return (DShape)List[shapeIndex];
  356. }
  357. set
  358. {
  359. List[shapeIndex] = value;
  360. }
  361. }
  362. }
  363. public class PointCollection : CollectionBase
  364. {
  365. public Point this[int pointIndex]
  366. {
  367. get
  368. {
  369. return (Point)List[pointIndex];
  370. }
  371. set
  372. {
  373. List[pointIndex] = value;
  374. }
  375. }
  376. public void Add(Point newPoint)
  377. {
  378. List.Add(newPoint);
  379. }
  380. public void RemoveRange(int x, int y)
  381. {
  382. for (int i = y - 1; i >= 0; i--)
  383. {
  384. List.RemoveAt(i);
  385. }
  386. }
  387. public void AddRange(Point[] point)
  388. {
  389. foreach (Point p in point)
  390. {
  391. List.Add(p);
  392. }
  393. }
  394. public PointCollection()
  395. {
  396. }
  397. }
  398. public class PointArrayCollection : CollectionBase
  399. {
  400. public Point[] this[int pointIndex]
  401. {
  402. get
  403. {
  404. return (Point[])List[pointIndex];
  405. }
  406. set
  407. {
  408. List[pointIndex] = value;
  409. }
  410. }
  411. public void Add(Point[] newPointArray)
  412. {
  413. List.Add(newPointArray);
  414. }
  415. public void RemoveRange(int x, int y)
  416. {
  417. for (int i = y - 1; i >= 0; i--)
  418. {
  419. List.RemoveAt(i);
  420. }
  421. }
  422. /*public void AddRange(Point[] point)
  423. {
  424. foreach(Point p in point)
  425. {
  426. List.Add(p);
  427. }
  428. } */
  429. public PointArrayCollection()
  430. {
  431. }
  432. }
  433. }