using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DllXqManager { public class MoveControl { #region Constructors public MoveControl(Control ctrl,ContextMenuStrip cms) { currMenu = cms; currentControl = ctrl; currentControl.Tag = this;//绑定当前实例 AddEvents(); CurrList.Add(this); } #endregion #region Fields public Control currentControl; //传入的控件 private Point pPoint; //上个鼠标坐标 private Point cPoint; //当前鼠标坐标 public FrameControl fc;//边框控件 private ContextMenuStrip currMenu = null; public bool IsSelected = false;//标记该控件是否选择 public static List CurrList = new List(); public static Control CurrParent =null;//父容器,不从单个实例取 #endregion #region Properties #endregion #region Methods /// /// 挂载事件 /// private void AddEvents() { currentControl.MouseClick += new MouseEventHandler(MouseClick); currentControl.MouseDown += new MouseEventHandler(MouseDown); currentControl.MouseMove += new MouseEventHandler(MouseMove); currentControl.MouseUp += new MouseEventHandler(MouseUp); } /// /// 绘制拖拉时的黑色边框 /// public static void DrawDragBound(Control ctrl) { ctrl.Refresh(); Graphics g = ctrl.CreateGraphics(); int width = ctrl.Width; int height = ctrl.Height; Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0), new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)}; g.DrawLines(new Pen(Color.Black), ps); } #endregion #region Events /// /// 鼠标单击事件:用来显示边框 /// /// /// protected void MouseClick(object sender, MouseEventArgs e) { if (IsSelected)//如果是已经选择的控件,则无需做操作 return; if ((Control.ModifierKeys & Keys.Control) == Keys.Control) { } else { //清空所有边框控件 foreach (var item in CurrList) item.IsSelected = false; ClearFrameControl(); } IsSelected = true; //this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框 //this.currentControl.BringToFront(); fc = new FrameControl(this.currentControl); this.currentControl.Parent.Controls.Add(fc); fc.Visible = true; fc.Draw(); } public static void ClearAllControl() { if (CurrParent == null) return; for(int i=CurrList.Count-1;i>=0;i--) { CurrList[i].ClearControl(); } CurrParent.Refresh(); } //从链表中移除控件和边框,并从父控件上移除 public void ClearControl() { //移除边框 if(fc!=null) { CurrParent.Controls.Remove(fc); fc.Dispose(); } CurrParent.Controls.Remove(currentControl); currentControl.Dispose(); CurrList.Remove(this); } /// /// 清空所以后边框控件 /// public static void ClearFrameControl() { //清空控件所有边框 foreach (var item in CurrList) { if (item.fc != null) { CurrParent.Controls.Remove(item.fc); item.fc.Dispose(); item.fc = null; } } CurrParent.Refresh(); } /// /// 给选中的控件添加边框 /// public static void AddFrameControl() { ClearFrameControl(); List sels = FindSelControl(); foreach(MoveControl item in sels) { item.fc = new FrameControl(item.currentControl); CurrParent.Controls.Add(item.fc); item.currentControl.BringToFront(); item.fc.Visible = true; item.fc.Draw(); } } public static List FindSelControl() { List sels = new List(); foreach (var item in CurrList) { if (item.IsSelected) sels.Add(item); } return sels; } /// /// 鼠标按下事件:记录当前鼠标相对窗体的坐标 /// void MouseDown(object sender, MouseEventArgs e) { pPoint = Cursor.Position; if(e.Button==MouseButtons.Right&&currMenu!=null) { currMenu.Tag = currentControl; System.Drawing.Point pointglobal = System.Windows.Forms.Control.MousePosition; currMenu.Show(pointglobal); } //if(e.Button==MouseButtons.Left) //{ // if (!IsSelected) // { // ClearFrameControl(); // IsSelected = true; // } //} } /// /// 鼠标移动事件:让控件跟着鼠标移动 /// void MouseMove(object sender, MouseEventArgs e) { Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll //当鼠标左键按下时才触发 if (e.Button == MouseButtons.Left) { ClearFrameControl(); if (!IsSelected) { foreach (var item in CurrList) item.IsSelected = false; IsSelected = true; } cPoint = Cursor.Position;//获得当前鼠标位置 int x = cPoint.X - pPoint.X; int y = cPoint.Y - pPoint.Y; List templist = FindSelControl(); foreach(var item in templist) { MoveControl.DrawDragBound(item.currentControl); if (item.fc != null) { //currentControl.Parent.Controls.Remove(item.fc); item.fc.Visible = false; } item.currentControl.Location = new Point(item.currentControl.Location.X + x, item.currentControl.Location.Y + y); } pPoint = cPoint; //MoveControl.DrawDragBound(this.currentControl); //if (fc != null) fc.Visible = false; //先隐藏 //cPoint = Cursor.Position;//获得当前鼠标位置 //int x = cPoint.X - pPoint.X; //int y = cPoint.Y - pPoint.Y; //currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y); //pPoint = cPoint; } } /// /// 鼠标弹起事件:让自定义的边框出现 /// void MouseUp(object sender, MouseEventArgs e) { AddFrameControl(); //currentControl.Parent.Refresh(); //this.currentControl.Refresh(); //if (fc != null) //{ // fc.Visible = true; // fc.Draw(); //} } public void Refresh() { this.currentControl.Refresh(); if (fc != null) { fc.Visible = true; fc.Draw(); } } #endregion } }