因为业务需要,百度了个可移动可改变大小的控件,然后自己修改了下,功能类似vs的设计面板中的功能差不多,可拖拽,改变大小
拖动的
public class MoveControl { #region 自定义事件 /// <summary> /// 控件移动时触发事件 /// </summary> public event EventHandler ControlMoving; /// <summary> /// 控件移动完成触发事件 /// </summary> public event EventHandler ControlMoved; /// <summary> /// 控件改变大小时触发事件 /// </summary> public event EventHandler ControlResizing; /// <summary> /// 控件改变大小完成触发事件 /// </summary> public event EventHandler ControlResized; /// <summary> /// 选中控件时发生 /// </summary> public event EventHandler ControlSelected; #endregion /// <summary> /// 是否可调整尺寸 /// </summary> bool m_blnResize = true; MoveControlType m_moveType; #region Constructors /// <summary> /// 功能描述:构造函数 /// 作 者:beck.huang /// 创建日期:2018-07-07 09:20:41 /// 任务编号:中餐 /// </summary> /// <param name="ctrl">ctrl</param> /// <param name="blnResize">是否可调整尺寸</param> /// <param name="moveType">移动模式</param> public MoveControl(Control ctrl, bool blnResize = true, MoveControlType moveType = MoveControlType.ANY) { m_blnResize = blnResize; m_moveType = moveType; currentControl = ctrl; AddEvents(); } #endregion #region Fields private Control currentControl; //传入的控件 private Point pPoint; //上个鼠标坐标 private Point cPoint; //当前鼠标坐标 FrameControl fc;//边框控件 #endregion #region Properties #endregion #region Methods /// <summary> /// 挂载事件 /// </summary> private void AddEvents() { currentControl.MouseClick -= new MouseEventHandler(MouseClick); currentControl.MouseClick += new MouseEventHandler(MouseClick); currentControl.MouseDown -= new MouseEventHandler(MouseDown); currentControl.MouseDown += new MouseEventHandler(MouseDown); currentControl.MouseMove -= new MouseEventHandler(MouseMove); currentControl.MouseMove += new MouseEventHandler(MouseMove); currentControl.MouseUp -= new MouseEventHandler(MouseUp); 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 (m_blnResize) { this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框 for (int i = currentControl.Parent.Controls.Count-1; i >=0; i--) { Control item = currentControl.Parent.Controls[i]; if (item is FrameControl) { currentControl.Parent.Controls.RemoveAt(i); } } this.currentControl.BringToFront(); fc = new FrameControl(this.currentControl); fc.ControlResized += fc_ControlResized; fc.ControlResizing += fc_ControlResizing; this.currentControl.Parent.Controls.Add(fc); fc.Visible = true; fc.Draw(); } if (ControlSelected != null) { ControlSelected(this.currentControl, e); } } /// <summary> /// 功能描述:控件改变大小时事件 /// 作 者:beck.huang /// 创建日期:2018-07-07 09:51:27 /// 任务编号:中餐 /// </summary> /// <param name="sender">sender</param> /// <param name="e">e</param> void fc_ControlResizing(object sender, EventArgs e) { if (ControlResizing != null) { ControlResizing(sender, e); } } /// <summary> /// 功能描述:控件改变大小完成事件 /// 作 者:beck.huang /// 创建日期:2018-07-07 09:51:12 /// 任务编号:中餐 /// </summary> /// <param name="sender">sender</param> /// <param name="e">e</param> void fc_ControlResized(object sender, EventArgs e) { if (ControlResized != null) { ControlResized(sender, e); } } /// <summary> /// 鼠标按下事件:记录当前鼠标相对窗体的坐标 /// </summary> void MouseDown(object sender, MouseEventArgs e) { pPoint = Cursor.Position; } /// <summary> /// 鼠标移动事件:让控件跟着鼠标移动 /// </summary> void MouseMove(object sender, MouseEventArgs e) { if (m_moveType != MoveControlType.NONE) { if (m_moveType == MoveControlType.ANY) { Cursor.Current = Cursors.SizeAll; } else if (m_moveType == MoveControlType.VERTICAL) { Cursor.Current = Cursors.HSplit; } else { Cursor.Current = Cursors.VSplit; } // Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll //当鼠标左键按下时才触发 if (e.Button == MouseButtons.Left) { 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; if (m_moveType == MoveControlType.ANY) { currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y); } else if (m_moveType == MoveControlType.VERTICAL) { currentControl.Location = new Point(currentControl.Location.X, currentControl.Location.Y + y); } else { currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y); } pPoint = cPoint; if (ControlMoving != null) { ControlMoving(currentControl, e); } } } } /// <summary> /// 鼠标弹起事件:让自定义的边框出现 /// </summary> void MouseUp(object sender, MouseEventArgs e) { this.currentControl.Refresh(); if (fc != null) { fc.Visible = true; fc.Draw(); } if (ControlMoved != null) { ControlMoved(currentControl, e); } } #endregion } /// <summary> /// 移动控件模式 /// </summary> public enum MoveControlType { /// <summary> /// 任意 /// </summary> ANY = 0, /// <summary> /// 垂直 /// </summary> VERTICAL, /// <summary> /// 水平 /// </summary> HORIZONTAL, /// <summary> /// 不可移动 /// </summary> NONE }