InsertPointPictureBox.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace SAGA.DotNetUtils.WinForms
  5. {
  6. public class InsertPointPictureBox : PictureBox
  7. {
  8. private int m_AllowMaxInsert;
  9. private Image m_Image = null;
  10. private int m_Index = -1;
  11. public InsertPointPictureBox()
  12. {
  13. this.AllowMaxInsert = 4;
  14. }
  15. protected override void Dispose(bool disposing)
  16. {
  17. base.Dispose(disposing);
  18. if (this.m_Image != null)
  19. {
  20. this.m_Image.Dispose();
  21. this.m_Image = null;
  22. }
  23. }
  24. public void FillInsertPoint(int intIndex)
  25. {
  26. if ((base.Image != null) && (this.m_Image == null))
  27. {
  28. this.m_Image = base.Image.Clone() as Image;
  29. }
  30. if (this.m_Image != null)
  31. {
  32. switch (this.AllowMaxInsert)
  33. {
  34. case 1:
  35. if (intIndex == 0)
  36. {
  37. break;
  38. }
  39. return;
  40. case 2:
  41. if (intIndex <= 1)
  42. {
  43. break;
  44. }
  45. return;
  46. case 3:
  47. if (intIndex <= 2)
  48. {
  49. break;
  50. }
  51. return;
  52. case 4:
  53. if (intIndex <= 3)
  54. {
  55. break;
  56. }
  57. return;
  58. }
  59. this.m_Index = intIndex;
  60. Image image = this.m_Image.Clone() as Image;
  61. if (base.Image != null)
  62. {
  63. base.Image.Dispose();
  64. }
  65. using (Graphics graphics = Graphics.FromImage(image))
  66. {
  67. int width = image.Width;
  68. int height = image.Height;
  69. int num3 = 8;
  70. Pen pen = new Pen(Color.Red, (float) num3);
  71. int num4 = (int) (Math.Min(width, height) * 0.15);
  72. switch (intIndex)
  73. {
  74. case 0:
  75. graphics.DrawEllipse(pen, 0, height - num4, num4, num4);
  76. break;
  77. case 1:
  78. graphics.DrawEllipse(pen, 0, 0, num4, num4);
  79. break;
  80. case 2:
  81. graphics.DrawEllipse(pen, width - num4, 0, num4, num4);
  82. break;
  83. case 3:
  84. graphics.DrawEllipse(pen, width - num4, height - num4, num4, num4);
  85. break;
  86. }
  87. graphics.Dispose();
  88. pen.Dispose();
  89. }
  90. base.Image = image;
  91. }
  92. }
  93. private double GetDistance(PointF pt1, PointF pt2)
  94. {
  95. double num = pt1.X - pt2.X;
  96. double num2 = pt1.Y - pt2.Y;
  97. return Math.Sqrt((num * num) + (num2 * num2));
  98. }
  99. private int GetNearPoint(PointF p, PointF[] points)
  100. {
  101. float maxValue = float.MaxValue;
  102. int num2 = 0;
  103. for (int i = 0; i < points.Length; i++)
  104. {
  105. float distance = (float) this.GetDistance(p, points[i]);
  106. if (distance < maxValue)
  107. {
  108. maxValue = distance;
  109. num2 = i;
  110. }
  111. }
  112. return num2;
  113. }
  114. protected override void OnMouseDoubleClick(MouseEventArgs e)
  115. {
  116. base.OnMouseDoubleClick(e);
  117. if (e.Button == MouseButtons.Left)
  118. {
  119. 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) };
  120. int nearPoint = this.GetNearPoint((PointF) e.Location, points);
  121. this.FillInsertPoint(nearPoint);
  122. }
  123. }
  124. public int AllowMaxInsert
  125. {
  126. get
  127. {
  128. return this.m_AllowMaxInsert;
  129. }
  130. set
  131. {
  132. if (value > 4)
  133. {
  134. this.m_AllowMaxInsert = 4;
  135. }
  136. if (value < 1)
  137. {
  138. this.m_AllowMaxInsert = 1;
  139. }
  140. this.m_AllowMaxInsert = value;
  141. }
  142. }
  143. public int InsertIndex
  144. {
  145. get
  146. {
  147. return this.m_Index;
  148. }
  149. set
  150. {
  151. this.FillInsertPoint(value);
  152. }
  153. }
  154. public InsertLocations InsertLocation
  155. {
  156. get
  157. {
  158. switch (this.m_Index)
  159. {
  160. case 0:
  161. return InsertLocations.LeftBottom;
  162. case 1:
  163. return InsertLocations.LeftTop;
  164. case 2:
  165. return InsertLocations.RightTop;
  166. case 3:
  167. return InsertLocations.RightBottom;
  168. }
  169. return InsertLocations.LeftBottom;
  170. }
  171. }
  172. }
  173. }