You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
9.5 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace Customize.Controls
  9. {
  10. public partial class ImageMask
  11. : System.Windows.Forms.Control
  12. {
  13. public string fileName;
  14. public ImageMask()
  15. {
  16. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  17. //this.BackColor = Color.Empty;
  18. this.BackColor = Color.Transparent;
  19. //this.BackColor = Color.FromArgb(50, Color.Red);
  20. //以下是设置双缓冲的代码 要不然在下面onPaint里的绘制半透明背景会让你很伤心,或者上面那句设置半透明背景用了过后拖动时也会让你跟蜗牛一样。
  21. this.SetStyle(ControlStyles.UserPaint, true);
  22. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  23. this.SetStyle(ControlStyles.DoubleBuffer, true);
  24. }
  25. public Size border;
  26. //public void showBorder() { }
  27. private Point m_MousePoint;
  28. private Size m_Size;
  29. private Point m_LastPoint;
  30. protected override void OnMouseDown(MouseEventArgs e)
  31. {
  32. base.OnMouseDown(e);
  33. this.m_LastPoint = this.Location;
  34. this.m_MousePoint = this.PointToScreen(e.Location);
  35. this.m_Size = this.Size;
  36. //this.BackColor = Color.Empty;
  37. }
  38. protected override void OnMouseMove(MouseEventArgs e)
  39. {
  40. base.OnMouseMove(e);
  41. if (arow != arrowType.none)
  42. {
  43. reSize(e);
  44. return;
  45. }
  46. if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
  47. Cursor = Cursors.SizeNWSE;
  48. else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
  49. Cursor = Cursors.SizeNESW;
  50. else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
  51. Cursor = Cursors.SizeNESW;
  52. else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
  53. Cursor = Cursors.SizeNWSE;
  54. else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
  55. Cursor = Cursors.SizeNS;
  56. else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
  57. Cursor = Cursors.SizeNS;
  58. else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
  59. Cursor = Cursors.SizeWE;
  60. else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
  61. Cursor = Cursors.SizeWE;
  62. else
  63. Cursor = Cursors.SizeAll;
  64. if (e.Button == MouseButtons.Left)
  65. {
  66. Point t = this.PointToScreen(e.Location);
  67. Point l = this.m_LastPoint;
  68. if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
  69. arow = arrowType.leftUp;
  70. else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
  71. arow = arrowType.leftDown;
  72. else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
  73. arow = arrowType.rightUp;
  74. else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
  75. arow = arrowType.rightDown;
  76. else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
  77. arow = arrowType.up;
  78. else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
  79. arow = arrowType.down;
  80. else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
  81. arow = arrowType.left;
  82. else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
  83. arow = arrowType.right;
  84. else
  85. arow = arrowType.none;
  86. l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
  87. if (arow != arrowType.none)
  88. reSize(e);
  89. else
  90. {
  91. this.Location = l;
  92. Refresh();//这句很重要立即重绘 不然拖动到时候会出现卡卡 的现象 ,找了半天原因
  93. }
  94. }
  95. }
  96. public void reSize(MouseEventArgs e)
  97. {
  98. Point t = this.PointToScreen(e.Location);
  99. Point l = this.m_LastPoint;
  100. l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
  101. switch (arow)
  102. {
  103. case arrowType.up:
  104. {
  105. this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
  106. this.Location = new Point(m_LastPoint.X, l.Y);
  107. break;
  108. }
  109. case arrowType.down:
  110. {
  111. this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
  112. break;
  113. }
  114. case arrowType.left:
  115. {
  116. this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
  117. this.Location = new Point(l.X, m_LastPoint.Y);
  118. break;
  119. }
  120. case arrowType.right:
  121. {
  122. this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
  123. break;
  124. }
  125. case arrowType.leftUp:
  126. {
  127. this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
  128. this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
  129. this.Location = new Point(l.X, l.Y);
  130. break;
  131. }
  132. case arrowType.leftDown:
  133. {
  134. this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
  135. this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
  136. this.Location = new Point(l.X, m_LastPoint.Y);
  137. break;
  138. }
  139. case arrowType.rightUp:
  140. {
  141. this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
  142. this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
  143. this.Location = new Point(m_LastPoint.X, l.Y);
  144. break;
  145. }
  146. case arrowType.rightDown:
  147. {
  148. this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
  149. this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
  150. break;
  151. }
  152. }
  153. this.Refresh();
  154. }
  155. public enum arrowType
  156. {
  157. up, down, left, right, leftUp, leftDown, rightUp, rightDown, none
  158. }
  159. public arrowType arow = arrowType.none;
  160. Rectangle[] recArrow = new Rectangle[8];//8个手柄
  161. public Rectangle area;//选择区域
  162. public readonly int blank = 8;//边距
  163. protected override void OnPaint(PaintEventArgs e)
  164. {
  165. int side = 6;//手柄矩形的边长
  166. recArrow[0] = new Rectangle(new Point(this.Width / 2 - side / 2, blank - side / 2), new Size(side, side));
  167. recArrow[1] = new Rectangle(new Point(this.Width / 2 - side / 2, this.Height - blank - side / 2), new Size(side, side));
  168. recArrow[2] = new Rectangle(new Point(blank - side / 2, this.Height / 2 - side / 2), new Size(side, side));
  169. recArrow[3] = new Rectangle(new Point(this.Width - blank - side / 2, this.Height / 2 - side / 2), new Size(side, side));
  170. recArrow[4] = new Rectangle(new Point(blank - side / 2, blank - side / 2), new Size(side, side));
  171. recArrow[5] = new Rectangle(new Point(blank - side / 2, this.Height - blank - side / 2), new Size(side, side));
  172. recArrow[6] = new Rectangle(new Point(this.Width - blank - side / 2, blank - side / 2), new Size(side, side));
  173. recArrow[7] = new Rectangle(new Point(this.Width - blank - side / 2, this.Height - blank - side / 2), new Size(side, side));
  174. foreach (Rectangle item in recArrow)
  175. e.Graphics.DrawRectangle(Pens.Red, item);
  176. area = new Rectangle(new Point(8, 8), new Size(this.Width - 8 * 2, this.Height - 8 * 2));
  177. ////加上半透明效果看倒是好看了 重绘的时候卡的凶 ,期待有解决方法
  178. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  179. System.Drawing.Color cor = System.Drawing.Color.FromArgb(50, Color.Red);
  180. System.Drawing.SolidBrush bsh = new System.Drawing.SolidBrush(cor);
  181. e.Graphics.FillRectangle(bsh, area);
  182. e.Graphics.DrawRectangle(Pens.Red, area);
  183. }
  184. protected override void OnMouseUp(MouseEventArgs e)
  185. {
  186. arow = arrowType.none;
  187. //this.BackColor = Color.FromArgb(0, Color.Red);
  188. }
  189. }
  190. }