MoveControl.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 DllXqManager
  9. {
  10. public class MoveControl
  11. {
  12. #region Constructors
  13. public MoveControl(Control ctrl,ContextMenuStrip cms)
  14. {
  15. currMenu = cms;
  16. currentControl = ctrl;
  17. currentControl.Tag = this;//绑定当前实例
  18. AddEvents();
  19. CurrList.Add(this);
  20. }
  21. #endregion
  22. #region Fields
  23. public Control currentControl; //传入的控件
  24. private Point pPoint; //上个鼠标坐标
  25. private Point cPoint; //当前鼠标坐标
  26. public FrameControl fc;//边框控件
  27. private ContextMenuStrip currMenu = null;
  28. public bool IsSelected = false;//标记该控件是否选择
  29. public static List<MoveControl> CurrList = new List<MoveControl>();
  30. public static Control CurrParent =null;//父容器,不从单个实例取
  31. #endregion
  32. #region Properties
  33. #endregion
  34. #region Methods
  35. /// <summary>
  36. /// 挂载事件
  37. /// </summary>
  38. private void AddEvents()
  39. {
  40. currentControl.MouseClick += new MouseEventHandler(MouseClick);
  41. currentControl.MouseDown += new MouseEventHandler(MouseDown);
  42. currentControl.MouseMove += new MouseEventHandler(MouseMove);
  43. currentControl.MouseUp += new MouseEventHandler(MouseUp);
  44. }
  45. /// <summary>
  46. /// 绘制拖拉时的黑色边框
  47. /// </summary>
  48. public static void DrawDragBound(Control ctrl)
  49. {
  50. ctrl.Refresh();
  51. Graphics g = ctrl.CreateGraphics();
  52. int width = ctrl.Width;
  53. int height = ctrl.Height;
  54. Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0),
  55. new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
  56. g.DrawLines(new Pen(Color.Black), ps);
  57. }
  58. #endregion
  59. #region Events
  60. /// <summary>
  61. /// 鼠标单击事件:用来显示边框
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. protected void MouseClick(object sender, MouseEventArgs e)
  66. {
  67. if (IsSelected)//如果是已经选择的控件,则无需做操作
  68. return;
  69. if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
  70. {
  71. }
  72. else
  73. {
  74. //清空所有边框控件
  75. foreach (var item in CurrList)
  76. item.IsSelected = false;
  77. ClearFrameControl();
  78. }
  79. IsSelected = true;
  80. //this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
  81. //this.currentControl.BringToFront();
  82. fc = new FrameControl(this.currentControl);
  83. this.currentControl.Parent.Controls.Add(fc);
  84. fc.Visible = true;
  85. fc.Draw();
  86. }
  87. public static void ClearAllControl()
  88. {
  89. if (CurrParent == null)
  90. return;
  91. for(int i=CurrList.Count-1;i>=0;i--)
  92. {
  93. CurrList[i].ClearControl();
  94. }
  95. CurrParent.Refresh();
  96. }
  97. //从链表中移除控件和边框,并从父控件上移除
  98. public void ClearControl()
  99. {
  100. //移除边框
  101. if(fc!=null)
  102. {
  103. CurrParent.Controls.Remove(fc);
  104. fc.Dispose();
  105. }
  106. CurrParent.Controls.Remove(currentControl);
  107. currentControl.Dispose();
  108. CurrList.Remove(this);
  109. }
  110. /// <summary>
  111. /// 清空所以后边框控件
  112. /// </summary>
  113. public static void ClearFrameControl()
  114. {
  115. //清空控件所有边框
  116. foreach (var item in CurrList)
  117. {
  118. if (item.fc != null)
  119. {
  120. CurrParent.Controls.Remove(item.fc);
  121. item.fc.Dispose();
  122. item.fc = null;
  123. }
  124. }
  125. CurrParent.Refresh();
  126. }
  127. /// <summary>
  128. /// 给选中的控件添加边框
  129. /// </summary>
  130. public static void AddFrameControl()
  131. {
  132. ClearFrameControl();
  133. List<MoveControl> sels = FindSelControl();
  134. foreach(MoveControl item in sels)
  135. {
  136. item.fc = new FrameControl(item.currentControl);
  137. CurrParent.Controls.Add(item.fc);
  138. item.currentControl.BringToFront();
  139. item.fc.Visible = true;
  140. item.fc.Draw();
  141. }
  142. }
  143. public static List<MoveControl> FindSelControl()
  144. {
  145. List<MoveControl> sels = new List<MoveControl>();
  146. foreach (var item in CurrList)
  147. {
  148. if (item.IsSelected)
  149. sels.Add(item);
  150. }
  151. return sels;
  152. }
  153. /// <summary>
  154. /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
  155. /// </summary>
  156. void MouseDown(object sender, MouseEventArgs e)
  157. {
  158. pPoint = Cursor.Position;
  159. if(e.Button==MouseButtons.Right&&currMenu!=null)
  160. {
  161. currMenu.Tag = currentControl;
  162. System.Drawing.Point pointglobal = System.Windows.Forms.Control.MousePosition;
  163. currMenu.Show(pointglobal);
  164. }
  165. //if(e.Button==MouseButtons.Left)
  166. //{
  167. // if (!IsSelected)
  168. // {
  169. // ClearFrameControl();
  170. // IsSelected = true;
  171. // }
  172. //}
  173. }
  174. /// <summary>
  175. /// 鼠标移动事件:让控件跟着鼠标移动
  176. /// </summary>
  177. void MouseMove(object sender, MouseEventArgs e)
  178. {
  179. Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
  180. //当鼠标左键按下时才触发
  181. if (e.Button == MouseButtons.Left)
  182. {
  183. ClearFrameControl();
  184. if (!IsSelected)
  185. {
  186. foreach (var item in CurrList)
  187. item.IsSelected = false;
  188. IsSelected = true;
  189. }
  190. cPoint = Cursor.Position;//获得当前鼠标位置
  191. int x = cPoint.X - pPoint.X;
  192. int y = cPoint.Y - pPoint.Y;
  193. List<MoveControl> templist = FindSelControl();
  194. foreach(var item in templist)
  195. {
  196. MoveControl.DrawDragBound(item.currentControl);
  197. if (item.fc != null)
  198. {
  199. //currentControl.Parent.Controls.Remove(item.fc);
  200. item.fc.Visible = false;
  201. }
  202. item.currentControl.Location = new Point(item.currentControl.Location.X + x, item.currentControl.Location.Y + y);
  203. }
  204. pPoint = cPoint;
  205. //MoveControl.DrawDragBound(this.currentControl);
  206. //if (fc != null) fc.Visible = false; //先隐藏
  207. //cPoint = Cursor.Position;//获得当前鼠标位置
  208. //int x = cPoint.X - pPoint.X;
  209. //int y = cPoint.Y - pPoint.Y;
  210. //currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
  211. //pPoint = cPoint;
  212. }
  213. }
  214. /// <summary>
  215. /// 鼠标弹起事件:让自定义的边框出现
  216. /// </summary>
  217. void MouseUp(object sender, MouseEventArgs e)
  218. {
  219. AddFrameControl();
  220. //currentControl.Parent.Refresh();
  221. //this.currentControl.Refresh();
  222. //if (fc != null)
  223. //{
  224. // fc.Visible = true;
  225. // fc.Draw();
  226. //}
  227. }
  228. public void Refresh()
  229. {
  230. this.currentControl.Refresh();
  231. if (fc != null)
  232. {
  233. fc.Visible = true;
  234. fc.Draw();
  235. }
  236. }
  237. #endregion
  238. }
  239. }