using System; using System.Drawing; using System.Windows.Forms; namespace SAGA.DotNetUtils.WinForms { public class InsertPointPictureBox : PictureBox { private int m_AllowMaxInsert; private Image m_Image = null; private int m_Index = -1; public InsertPointPictureBox() { this.AllowMaxInsert = 4; } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (this.m_Image != null) { this.m_Image.Dispose(); this.m_Image = null; } } public void FillInsertPoint(int intIndex) { if ((base.Image != null) && (this.m_Image == null)) { this.m_Image = base.Image.Clone() as Image; } if (this.m_Image != null) { switch (this.AllowMaxInsert) { case 1: if (intIndex == 0) { break; } return; case 2: if (intIndex <= 1) { break; } return; case 3: if (intIndex <= 2) { break; } return; case 4: if (intIndex <= 3) { break; } return; } this.m_Index = intIndex; Image image = this.m_Image.Clone() as Image; if (base.Image != null) { base.Image.Dispose(); } using (Graphics graphics = Graphics.FromImage(image)) { int width = image.Width; int height = image.Height; int num3 = 8; Pen pen = new Pen(Color.Red, (float) num3); int num4 = (int) (Math.Min(width, height) * 0.15); switch (intIndex) { case 0: graphics.DrawEllipse(pen, 0, height - num4, num4, num4); break; case 1: graphics.DrawEllipse(pen, 0, 0, num4, num4); break; case 2: graphics.DrawEllipse(pen, width - num4, 0, num4, num4); break; case 3: graphics.DrawEllipse(pen, width - num4, height - num4, num4, num4); break; } graphics.Dispose(); pen.Dispose(); } base.Image = image; } } private double GetDistance(PointF pt1, PointF pt2) { double num = pt1.X - pt2.X; double num2 = pt1.Y - pt2.Y; return Math.Sqrt((num * num) + (num2 * num2)); } private int GetNearPoint(PointF p, PointF[] points) { float maxValue = float.MaxValue; int num2 = 0; for (int i = 0; i < points.Length; i++) { float distance = (float) this.GetDistance(p, points[i]); if (distance < maxValue) { maxValue = distance; num2 = i; } } return num2; } protected override void OnMouseDoubleClick(MouseEventArgs e) { base.OnMouseDoubleClick(e); if (e.Button == MouseButtons.Left) { PointF[] points = new PointF[] { new PointF(0f, (float) base.Height), new PointF(0f, 0f), new PointF((float) base.Width, 0f), new PointF((float) base.Width, (float) base.Height) }; int nearPoint = this.GetNearPoint((PointF) e.Location, points); this.FillInsertPoint(nearPoint); } } public int AllowMaxInsert { get { return this.m_AllowMaxInsert; } set { if (value > 4) { this.m_AllowMaxInsert = 4; } if (value < 1) { this.m_AllowMaxInsert = 1; } this.m_AllowMaxInsert = value; } } public int InsertIndex { get { return this.m_Index; } set { this.FillInsertPoint(value); } } public InsertLocations InsertLocation { get { switch (this.m_Index) { case 0: return InsertLocations.LeftBottom; case 1: return InsertLocations.LeftTop; case 2: return InsertLocations.RightTop; case 3: return InsertLocations.RightBottom; } return InsertLocations.LeftBottom; } } } }