123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #region Using directives
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- using System.Diagnostics;
- using System.Globalization;
- using System.Drawing.Drawing2D;
- using System.Collections.Generic;
- #endregion
- namespace DrawTools
- {
- using PointList = List<Point>;
- using PointEnumerator = IEnumerator<Point>;
-
-
-
- class DrawPolygon : DrawTools.DrawLine
- {
- private PointList pointArray;
- private static Cursor handleCursor = new Cursor(typeof(DrawPolygon), "PolyHandle.cur");
- private const string entryLength = "Length";
- private const string entryPoint = "Point";
- public DrawPolygon() : base()
- {
- pointArray = new PointList();
- Initialize();
- }
- public DrawPolygon(int x1, int y1, int x2, int y2) : base()
- {
- pointArray = new PointList();
- pointArray.Add(new Point(x1, y1));
- pointArray.Add(new Point(x2, y2));
- Initialize();
- }
-
-
-
- public override DrawObject Clone()
- {
- DrawPolygon drawPolygon = new DrawPolygon();
- foreach(Point p in this.pointArray)
- {
- drawPolygon.pointArray.Add(p);
- }
- FillDrawObjectFields(drawPolygon);
- return drawPolygon;
- }
- public override void Draw(Graphics g)
- {
- int x1 = 0, y1 = 0;
- int x2, y2;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- Pen pen = new Pen(Color, PenWidth);
- PointEnumerator enumerator = pointArray.GetEnumerator();
- if (enumerator.MoveNext())
- {
- x1 = ((Point)enumerator.Current).X;
- y1 = ((Point)enumerator.Current).Y;
- }
- while (enumerator.MoveNext())
- {
- x2 = ((Point)enumerator.Current).X;
- y2 = ((Point)enumerator.Current).Y;
- g.DrawLine(pen, x1, y1, x2, y2);
- x1 = x2;
- y1 = y2;
- }
- pen.Dispose();
- }
- public void AddPoint(Point point)
- {
- pointArray.Add(point);
- }
- public override int HandleCount
- {
- get
- {
- return pointArray.Count;
- }
- }
-
-
-
-
-
- public override Point GetHandle(int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- return ((Point)pointArray[handleNumber - 1]);
- }
- public override Cursor GetHandleCursor(int handleNumber)
- {
- return handleCursor;
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- pointArray[handleNumber - 1] = point;
- Invalidate();
- }
- public override void Move(int deltaX, int deltaY)
- {
- int n = pointArray.Count;
- Point point;
- for (int i = 0; i < n; i++)
- {
- point = new Point(((Point)pointArray[i]).X + deltaX, ((Point)pointArray[i]).Y + deltaY);
- pointArray[i] = point;
- }
- Invalidate();
- }
- public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
- {
- info.AddValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryLength, orderNumber),
- pointArray.Count);
- int i = 0;
- foreach (Point p in pointArray)
- {
- info.AddValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}-{2}",
- entryPoint, orderNumber, i++),
- p);
- }
- base.SaveToStream(info, orderNumber);
- }
- public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
- {
- Point point;
- int n = info.GetInt32(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}",
- entryLength, orderNumber));
- for (int i = 0; i < n; i++)
- {
- point = (Point)info.GetValue(
- String.Format(CultureInfo.InvariantCulture,
- "{0}{1}-{2}",
- entryPoint, orderNumber, i),
- typeof(Point));
- pointArray.Add(point);
- }
- base.LoadFromStream(info, orderNumber);
- }
-
-
-
- protected override void CreateObjects()
- {
- if (AreaPath != null)
- return;
-
- AreaPath = new GraphicsPath();
- int x1 = 0, y1 = 0;
- int x2, y2;
- PointEnumerator enumerator = pointArray.GetEnumerator();
- if (enumerator.MoveNext())
- {
- x1 = ((Point)enumerator.Current).X;
- y1 = ((Point)enumerator.Current).Y;
- }
- while (enumerator.MoveNext())
- {
- x2 = ((Point)enumerator.Current).X;
- y2 = ((Point)enumerator.Current).Y;
- AreaPath.AddLine(x1, y1, x2, y2);
- x1 = x2;
- y1 = y2;
- }
- AreaPath.CloseFigure();
-
- AreaRegion = new Region(AreaPath);
- }
- }
- }
|