123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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<MoveControl> CurrList = new List<MoveControl>();
- public static Control CurrParent =null;//父容器,不从单个实例取
- #endregion
- #region Properties
- #endregion
- #region Methods
- /// <summary>
- /// 挂载事件
- /// </summary>
- private void AddEvents()
- {
- currentControl.MouseClick += new MouseEventHandler(MouseClick);
- currentControl.MouseDown += new MouseEventHandler(MouseDown);
- currentControl.MouseMove += new MouseEventHandler(MouseMove);
- currentControl.MouseUp += new MouseEventHandler(MouseUp);
- }
- /// <summary>
- /// 绘制拖拉时的黑色边框
- /// </summary>
- 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
- /// <summary>
- /// 鼠标单击事件:用来显示边框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- /// <summary>
- /// 清空所以后边框控件
- /// </summary>
- 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();
- }
- /// <summary>
- /// 给选中的控件添加边框
- /// </summary>
- public static void AddFrameControl()
- {
- ClearFrameControl();
- List<MoveControl> 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<MoveControl> FindSelControl()
- {
- List<MoveControl> sels = new List<MoveControl>();
- foreach (var item in CurrList)
- {
- if (item.IsSelected)
- sels.Add(item);
- }
- return sels;
- }
- /// <summary>
- /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
- /// </summary>
- 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;
- // }
- //}
- }
- /// <summary>
- /// 鼠标移动事件:让控件跟着鼠标移动
- /// </summary>
- 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<MoveControl> 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;
- }
- }
- /// <summary>
- /// 鼠标弹起事件:让自定义的边框出现
- /// </summary>
- 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
- }
- }
|