DrawableObjectsCS.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace GraphicalCS
  7. {
  8. /// <summary>
  9. /// DrawableObjectsCS 的摘要说明。
  10. /// </summary>
  11. //基类
  12. public abstract class DShape
  13. {
  14. public abstract void Draw(Graphics g);
  15. protected Rectangle bounding;
  16. protected Point[] pointlist;
  17. public abstract void pointchange(Point[] p);
  18. public virtual GraphicsPath pathChange(GraphicsPath path, bool pick)
  19. {
  20. return null;
  21. }
  22. protected Color penColor;
  23. protected float penWidth;
  24. }
  25. //填充图形接口
  26. public interface IFillable
  27. {
  28. void Fill(Graphics g);
  29. Color FillBrushColor
  30. {
  31. get;
  32. set;
  33. }
  34. }
  35. //画点
  36. public class DPoint : DShape
  37. {
  38. public DPoint(Point[] p, Color penColor,float penWidth)
  39. {
  40. this.penColor = penColor;
  41. this.penWidth = penWidth;
  42. this.pointchange(p);
  43. }
  44. public override void pointchange(Point[] p)
  45. {
  46. pointlist = new Point[p.Length];
  47. int i = 0;
  48. foreach(Point tempPoint in p)
  49. {
  50. pointlist[i++] = tempPoint;
  51. }
  52. bounding = new Rectangle(pointlist[0], new Size((int)penWidth, (int)penWidth));
  53. }
  54. public override void Draw(Graphics g)
  55. {
  56. using (Pen p = new Pen(penColor,penWidth))
  57. {
  58. g.DrawRectangle(p, bounding);
  59. if(this.penWidth>1)
  60. {
  61. using(Brush b = new SolidBrush(penColor))
  62. {
  63. g.FillRectangle(b,bounding);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. //画线
  70. public class DLine : DShape
  71. {
  72. protected Point p1;
  73. protected Point p2;
  74. public DLine(Point[] p,Color penColor,float penWidth)
  75. {
  76. this.pointchange(p);
  77. this.penColor = penColor;
  78. this.penWidth = penWidth;
  79. }
  80. public override void pointchange(Point[] p)
  81. {
  82. pointlist = new Point[p.Length];
  83. int i = 0;
  84. foreach(Point tempPoint in p)
  85. {
  86. pointlist[i++] = tempPoint;
  87. }
  88. p1 = pointlist[0];
  89. p2 = pointlist[1];
  90. }
  91. public override void Draw(Graphics g)
  92. {
  93. using (Pen p = new Pen(penColor,penWidth))
  94. {
  95. g.DrawLine(p,p1,p2);
  96. }
  97. }
  98. }
  99. //路径
  100. public class DPLine : DShape
  101. {
  102. protected Point p1;
  103. protected Point p2;
  104. protected DashStyle style;
  105. protected System.Drawing.Drawing2D.LineCap scap;
  106. protected System.Drawing.Drawing2D.LineCap ecap;
  107. public DPLine(Point start, Point end, Color penColor,DashStyle style,System.Drawing.Drawing2D.LineCap scap,System.Drawing.Drawing2D.LineCap ecap)
  108. {
  109. p1 = start;
  110. p2 = end;
  111. this.penColor = penColor;
  112. this.style = style;
  113. this.scap = scap;
  114. this.ecap = ecap;
  115. }
  116. public override void pointchange(Point[] p)
  117. {
  118. }
  119. public override void Draw(Graphics g)
  120. {
  121. using(Pen p=new Pen(penColor))
  122. {
  123. p.DashStyle = style;
  124. p.StartCap = scap;
  125. p.EndCap = ecap;
  126. p.Width = 8;
  127. g.DrawLine(p,p1,p2);
  128. }
  129. }
  130. }
  131. //空心圆
  132. public class DHollowCircle : DShape
  133. {
  134. public DHollowCircle(Point[] p,Color penColor,float penWidth)
  135. {
  136. this.pointchange(p);
  137. this.penColor = penColor;
  138. this.penWidth = penWidth;
  139. }
  140. public override void pointchange(Point[] p)
  141. {
  142. this.pointlist = new Point[p.Length];
  143. int i = 0;
  144. foreach(Point tempPoint in p)
  145. {
  146. pointlist[i++] = tempPoint;
  147. }
  148. 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));
  149. pointlist[0].Offset(-Distance,-Distance);
  150. int diameter = Distance*2;
  151. bounding = new Rectangle(pointlist[0], new Size(diameter, diameter));
  152. }
  153. public override void Draw(Graphics g)
  154. {
  155. using (Pen p = new Pen(penColor,penWidth))
  156. {
  157. g.DrawEllipse(p,bounding);
  158. }
  159. }
  160. }
  161. //实心圆
  162. public class DFilledCircle : DHollowCircle, IFillable
  163. {
  164. protected Color brushColor;
  165. public DFilledCircle(Point[] p,Color penColor,float penWidth, Color brushColor) : base(p, penColor,penWidth)
  166. {
  167. this.brushColor = brushColor;
  168. }
  169. public void Fill(Graphics g)
  170. {
  171. using (Brush b = new SolidBrush(brushColor))
  172. {
  173. g.FillEllipse(b, bounding);
  174. }
  175. }
  176. public Color FillBrushColor
  177. {
  178. get
  179. {
  180. return brushColor;
  181. }
  182. set
  183. {
  184. brushColor = value;
  185. }
  186. }
  187. public override void Draw(Graphics g)
  188. {
  189. Fill(g);
  190. //base.Draw(g);
  191. }
  192. }
  193. //空心椭圆
  194. public class DHollowEllipse : DShape
  195. {
  196. public DHollowEllipse(Point[] p,Color penColor,float penWidth)
  197. {
  198. this.pointchange(p);
  199. this.penColor = penColor;
  200. this.penWidth = penWidth;
  201. }
  202. public override void pointchange(Point[] p)
  203. {
  204. pointlist = new Point[p.Length];
  205. int i = 0;
  206. foreach(Point tempPoint in p)
  207. {
  208. pointlist[i++] = tempPoint;
  209. }
  210. bounding = new Rectangle(pointlist[0], new Size(pointlist[1].X-pointlist[0].X,pointlist[1].Y-pointlist[0].Y));
  211. }
  212. public override void Draw(Graphics g)
  213. {
  214. using (Pen p = new Pen(penColor,penWidth))
  215. {
  216. g.DrawEllipse(p, bounding);
  217. }
  218. }
  219. }
  220. //实心椭圆
  221. public class DFilledEllipse : DHollowEllipse, IFillable
  222. {
  223. protected Color brushColor;
  224. public DFilledEllipse(Point[] p,Color penColor,float penWidth, Color brushColor) : base(p, penColor,penWidth)
  225. {
  226. this.brushColor = brushColor;
  227. }
  228. public void Fill(Graphics g)
  229. {
  230. using (Brush b = new SolidBrush(brushColor))
  231. {
  232. g.FillEllipse(b, bounding);
  233. }
  234. }
  235. public Color FillBrushColor
  236. {
  237. get
  238. {
  239. return brushColor;
  240. }
  241. set
  242. {
  243. brushColor = value;
  244. }
  245. }
  246. public override void Draw(Graphics g)
  247. {
  248. Fill(g);
  249. //base.Draw(g);
  250. }
  251. }
  252. //空心矩形
  253. public class DHollowRectangle : DHollowEllipse
  254. {
  255. public DHollowRectangle(Point[] p, Color penColor,float penWidth) :base(p,penColor,penWidth)
  256. {
  257. }
  258. public override void Draw(Graphics g)
  259. {
  260. using (Pen p = new Pen(penColor,penWidth))
  261. {
  262. g.DrawRectangle(p, bounding);
  263. }
  264. }
  265. }
  266. public class DPRectangle : DShape
  267. {
  268. protected DashStyle style;
  269. public DPRectangle(Rectangle rect, Color penColor,DashStyle style)
  270. {
  271. bounding = rect;
  272. this.penColor = penColor;
  273. this.style = style;
  274. }
  275. public override void pointchange(Point[] p)
  276. {
  277. }
  278. public override void Draw(Graphics g)
  279. {
  280. using(Pen p=new Pen(penColor))
  281. {
  282. p.DashStyle = style;
  283. g.DrawRectangle(p,bounding);
  284. }
  285. }
  286. }
  287. //实心矩形
  288. public class DFilledRectangle : DHollowRectangle, IFillable
  289. {
  290. protected Color brushColor;
  291. public DFilledRectangle(Point[] p, Color penColor, float penWidth,Color brushColor) : base(p, penColor,penWidth)
  292. {
  293. this.brushColor = brushColor;
  294. }
  295. public void Fill(Graphics g)
  296. {
  297. using (Brush b = new SolidBrush(brushColor))
  298. {
  299. g.FillRectangle(b, bounding);
  300. }
  301. }
  302. public Color FillBrushColor
  303. {
  304. get
  305. {
  306. return brushColor;
  307. }
  308. set
  309. {
  310. brushColor = value;
  311. }
  312. }
  313. public override void Draw(Graphics g)
  314. {
  315. Fill(g);
  316. //base.Draw(g);
  317. }
  318. }
  319. //画路径
  320. public class DPath : DShape
  321. {
  322. protected GraphicsPath Path;
  323. public DPath(GraphicsPath Path, Color penColor,float penWidth)
  324. {
  325. this.Path = Path;
  326. this.penColor = penColor;
  327. this.penWidth = penWidth;
  328. }
  329. public override void pointchange(Point[] p)
  330. {
  331. }
  332. public override GraphicsPath pathChange(GraphicsPath path, bool pick)
  333. {
  334. if(pick == true)
  335. return this.Path;
  336. else
  337. {
  338. this.Path = path;
  339. return null;
  340. }
  341. }
  342. public override void Draw(Graphics g)
  343. {
  344. using(Pen p = new Pen(penColor,penWidth))
  345. {
  346. g.DrawPath(p, Path);
  347. }
  348. }
  349. public GraphicsPath mousePath
  350. {
  351. get
  352. {
  353. return Path;
  354. }
  355. }
  356. }
  357. //橡皮擦
  358. public class DEraser : DShape
  359. {
  360. protected Region region;
  361. protected Color brushColor;
  362. public DEraser(Point p, Color brushColor, int Size)
  363. {
  364. p.Offset(-Size,-Size);
  365. bounding = new Rectangle(p, new Size(Size*2, Size*2));
  366. this.region = new Region(bounding);;
  367. this.brushColor = brushColor;
  368. }
  369. public override void pointchange(Point[] p)
  370. {
  371. }
  372. public override void Draw(Graphics g)
  373. {
  374. using(Brush b = new SolidBrush(brushColor))
  375. {
  376. g.FillRegion(b, region);
  377. }
  378. }
  379. }
  380. //文本
  381. public class DText : DShape
  382. {
  383. protected Color brushColor;
  384. protected string Text;
  385. protected Font font;
  386. public DText(Point[] p,Color brushColor,string Text,Size size, Font font)
  387. {
  388. bounding = new Rectangle(p[0],size);
  389. this.brushColor = brushColor;
  390. this.Text = Text;
  391. this.font = font;
  392. }
  393. public override void pointchange(Point[] p)
  394. {
  395. }
  396. public override void Draw(Graphics g)
  397. {
  398. using(Brush b = new SolidBrush(brushColor))
  399. {
  400. g.DrawString(Text,font,b,bounding);
  401. }
  402. }
  403. }
  404. //画弧
  405. public class DArc : DHollowCircle
  406. {
  407. protected float angle1;
  408. protected float angle2;
  409. public DArc(Point[] p, Color penColor, float penWidth,float angle1, float angle2) : base(p, penColor,penWidth)
  410. {
  411. this.angle1 = angle1;
  412. this.angle2 = angle2;
  413. }
  414. public override void Draw(Graphics g)
  415. {
  416. using(Pen p = new Pen(penColor,penWidth))
  417. {
  418. g.DrawArc(p, bounding, angle1, angle2);
  419. }
  420. }
  421. }
  422. //画多边形
  423. public class DPolygon : DShape
  424. {
  425. public DPolygon(Point[] p, Color penColor,float penWidth)
  426. {
  427. this.pointchange(p);
  428. this.penColor = penColor;
  429. this.penWidth = penWidth;
  430. }
  431. public override void pointchange(Point[] p)
  432. {
  433. pointlist = new Point[p.Length];
  434. int i = 0;
  435. foreach(Point tempPoint in p)
  436. {
  437. pointlist[i++] = tempPoint;
  438. }
  439. }
  440. public override void Draw(Graphics g)
  441. {
  442. using(Pen p = new Pen(penColor,penWidth))
  443. {
  444. g.DrawPolygon(p, pointlist);
  445. }
  446. }
  447. }
  448. //基数样条
  449. public class DCurve : DPolygon
  450. {
  451. public DCurve(Point[] p, Color penColor,float penWidth) : base(p, penColor,penWidth)
  452. {
  453. }
  454. public override void Draw(Graphics g)
  455. {
  456. using(Pen p = new Pen(penColor,penWidth))
  457. {
  458. g.DrawCurve(p,pointlist);
  459. }
  460. }
  461. }
  462. //闭合样条
  463. public class DClosedCurve : DCurve
  464. {
  465. public DClosedCurve(Point[] p, Color penColor,float penWidth) : base(p, penColor,penWidth)
  466. {
  467. }
  468. public override void Draw(Graphics g)
  469. {
  470. using(Pen p = new Pen(penColor,penWidth))
  471. {
  472. g.DrawClosedCurve(p,pointlist);
  473. }
  474. }
  475. }
  476. //Beziers
  477. public class DBeziers : DPolygon
  478. {
  479. public DBeziers(Point[] p, Color penColor,float penWidth) : base(p, penColor,penWidth)
  480. {
  481. }
  482. public override void Draw(Graphics g)
  483. {
  484. using(Pen p = new Pen(penColor,penWidth))
  485. {
  486. g.DrawBeziers(p,pointlist);
  487. }
  488. }
  489. }
  490. public class DRegion : DShape
  491. {
  492. protected Region newRegion;
  493. public DRegion(GraphicsPath path)
  494. {
  495. newRegion = new Region(path);
  496. }
  497. public override void pointchange(Point[] p)
  498. {
  499. }
  500. public override void Draw(Graphics g)
  501. {
  502. }
  503. public Region region
  504. {
  505. get
  506. {
  507. return newRegion;
  508. }
  509. }
  510. }
  511. //图像
  512. public class DImage : DShape
  513. {
  514. protected Image image;
  515. public DImage(Point p,Image image)
  516. {
  517. bounding = new Rectangle(p,new Size(image.Width,image.Height));
  518. this.image=image;
  519. }
  520. public override void pointchange(Point[] p)
  521. {
  522. }
  523. public override void Draw(Graphics g)
  524. {
  525. g.DrawImage(image,bounding);
  526. }
  527. }
  528. //图形集合
  529. public class DShapeList : CollectionBase
  530. {
  531. DrawCollection wholeList = new DrawCollection();
  532. public new int Count
  533. {
  534. get
  535. {
  536. return wholeList.Count;
  537. }
  538. }
  539. public void Add(DShape d)
  540. {
  541. wholeList.Add(d);
  542. }
  543. public new void RemoveAt(int i)
  544. {
  545. wholeList.RemoveAt(i);
  546. }
  547. //索引器
  548. public DShape this[int newshapeIndex]
  549. {
  550. get
  551. {
  552. return (DShape)wholeList[newshapeIndex];
  553. }
  554. set
  555. {
  556. wholeList[newshapeIndex] = value;
  557. }
  558. }
  559. public void DrawList(Graphics g)
  560. {
  561. if (wholeList.Count != 0)
  562. {
  563. foreach (DShape d in wholeList)
  564. d.Draw(g);
  565. }
  566. }
  567. //public IFillable[] GetFilledList()
  568. //{
  569. //return (IFillable[])filledList.ToArray(typeof(IFillable));
  570. //}
  571. }
  572. public class DrawCollection : CollectionBase
  573. {
  574. public void Add(DShape d)
  575. {
  576. List.Add(d);
  577. }
  578. public new void RemoveAt(int i)
  579. {
  580. List.RemoveAt(i);
  581. }
  582. public DShape this[int shapeIndex]
  583. {
  584. get
  585. {
  586. return (DShape)List[shapeIndex];
  587. }
  588. set
  589. {
  590. List[shapeIndex] = value;
  591. }
  592. }
  593. }
  594. public class NewRegion : CollectionBase
  595. {
  596. protected MainWindow.ImageType newType;
  597. public Region this[int regionIndex]
  598. {
  599. get
  600. {
  601. return (Region)List[regionIndex];
  602. }
  603. set
  604. {
  605. List[regionIndex] = value;
  606. }
  607. }
  608. public void Add(Region newRegion)
  609. {
  610. List.Add(newRegion);
  611. }
  612. public new void RemoveAt(int i)
  613. {
  614. List.RemoveAt(i);
  615. }
  616. public void Remove(Region oldRegion)
  617. {
  618. List.Remove(oldRegion);
  619. }
  620. public NewRegion()
  621. {
  622. }
  623. }
  624. public class NewRegionArray : CollectionBase
  625. {
  626. public Region[] this[int regionIndex]
  627. {
  628. get
  629. {
  630. return (Region[])List[regionIndex];
  631. }
  632. set
  633. {
  634. List[regionIndex] = value;
  635. }
  636. }
  637. public void Add(Region[] newRegion)
  638. {
  639. List.Add(newRegion);
  640. }
  641. public void Remove(Region[] oldRegion)
  642. {
  643. List.Remove(oldRegion);
  644. }
  645. public new void RemoveAt(int i)
  646. {
  647. List.RemoveAt(i);
  648. }
  649. public NewRegionArray()
  650. {
  651. }
  652. }
  653. public class PointCollection : CollectionBase
  654. {
  655. public Point this[int pointIndex]
  656. {
  657. get
  658. {
  659. return (Point)List[pointIndex];
  660. }
  661. set
  662. {
  663. List[pointIndex] = value;
  664. }
  665. }
  666. public void Add(Point newPoint)
  667. {
  668. List.Add(newPoint);
  669. }
  670. public void RemoveRange(int x,int y)
  671. {
  672. for(int i=y-1;i>=0;i--)
  673. {
  674. List.RemoveAt(i);
  675. }
  676. }
  677. public void AddRange(Point[] point)
  678. {
  679. foreach(Point p in point)
  680. {
  681. List.Add(p);
  682. }
  683. }
  684. public PointCollection()
  685. {
  686. }
  687. }
  688. public class PointArrayCollection : CollectionBase
  689. {
  690. public Point[] this[int pointIndex]
  691. {
  692. get
  693. {
  694. return (Point[])List[pointIndex];
  695. }
  696. set
  697. {
  698. List[pointIndex] = value;
  699. }
  700. }
  701. public void Add(Point[] newPointArray)
  702. {
  703. List.Add(newPointArray);
  704. }
  705. public void RemoveRange(int x,int y)
  706. {
  707. for(int i=y-1;i>=0;i--)
  708. {
  709. List.RemoveAt(i);
  710. }
  711. }
  712. /*public void AddRange(Point[] point)
  713. {
  714. foreach(Point p in point)
  715. {
  716. List.Add(p);
  717. }
  718. } */
  719. public PointArrayCollection()
  720. {
  721. }
  722. }
  723. public class Type : CollectionBase
  724. {
  725. public int this[int intIndex]
  726. {
  727. get
  728. {
  729. return (int)List[intIndex];
  730. }
  731. set
  732. {
  733. List[intIndex] = value;
  734. }
  735. }
  736. public void Add(MainWindow.ImageType type)
  737. {
  738. List.Add(type);
  739. }
  740. public new void RemoveAt(int i)
  741. {
  742. List.RemoveAt(i);
  743. }
  744. }
  745. public class Number : CollectionBase
  746. {
  747. public int this[int intIndex]
  748. {
  749. get
  750. {
  751. return (int)List[intIndex];
  752. }
  753. set
  754. {
  755. List[intIndex] = value;
  756. }
  757. }
  758. public void Add(int i)
  759. {
  760. List.Add(i);
  761. }
  762. }
  763. }