using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using NewMyFormDesigner; using MyFormDesinger; namespace NewMyFormDesigner { public partial class Overlayer : UserControl { HostFrame _themainhost; //被遮罩的控件容器,通过Overlayer操作该容器(以及其中的子控件) Recter _recter = new Recter(); //被操作控件(容器)周围的方框 Point _firstPoint = new Point();//第一点位置 bool _mouseDown = false;//鼠标是否点下 DragType _dragType = DragType.None; bool isMove = false; public Control _currentCtrl = null; //当前被操作控件 //选中多个控件的集合 List Contrlist = null; public Overlayer(HostFrame themainhost) { _themainhost = themainhost; //默认被操作的是控件容器 Rectangle r = _themainhost.Bounds; r = _themainhost.Parent.RectangleToScreen(r); r = this.RectangleToClient(r); _recter.Rect = r; _recter.IsForm = true; InitializeComponent(); } protected override CreateParams CreateParams { get { CreateParams para = base.CreateParams; para.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 透明支持 return para; } } protected override void OnPaintBackground(PaintEventArgs e) //不画背景 { //base.OnPaintBackground(e); } protected override void OnPaint(PaintEventArgs e) { if (_recter != null) //绘制被操作控件周围的方框 { _recter.Draw(e.Graphics); } base.OnPaint(e); } public void UserMouseDown(MouseEventArgs e) { OnMouseDown(e); } public void UserMouseUp(MouseEventArgs e) { OnMouseUp(e); } Point mouseDownPoint = Point.Empty;//记录鼠标按下坐标 #region 代理所有用户操作 protected override void OnMouseMove(MouseEventArgs e) { //if (MouseIsDown) //{ // ResizeToRectangle(e.Location); //} //画线 //if (!MouseIsDown && isDrag) //{ // var ly = e.Location.Y - mouseDownPoint.Y; // var lx = e.Location.X - mouseDownPoint.X; // foreach (var item in Contrlist) // { // //rect.Location = getPointToForm(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)); // item.control.Location = new Point(item.control.Location.X+lx, item.control.Location.Y + ly); // item.rects = new Rectangle(item.rects.X+lx, item.rects.Y + ly, item.rects.Width, item.rects.Height); // _recter.Rect = item.rects; // _firstPoint = item.control.Location; // } //} //_themainhost.Text = "X:" + e.Location.X.ToString() + "Y:" + e.Location.Y.ToString(); if (!_mouseDown) //鼠标形状 { DragType dt = _recter.GetMouseDragType(e.Location); switch (dt) { case DragType.Top: { Cursor = Cursors.SizeNS; break; } case DragType.RightTop: { Cursor = Cursors.SizeNESW; break; } case DragType.RightBottom: { Cursor = Cursors.SizeNWSE; break; } case DragType.Right: { Cursor = Cursors.SizeWE; break; } case DragType.LeftTop: { Cursor = Cursors.SizeNWSE; break; } case DragType.LeftBottom: { Cursor = Cursors.SizeNESW; break; } case DragType.Left: { Cursor = Cursors.SizeWE; break; } case DragType.Center: { Cursor = Cursors.SizeAll; break; } case DragType.Bottom: { Cursor = Cursors.SizeNS; break; } default: { Cursor = Cursors.Default; break; } } } else { string cid = "0"; if (_currentCtrl != null) { object[] col = _currentCtrl.Tag as object[]; if (col.Length >= 8) { cid = col[2].ToString();//当前控件的类型 } if (Contrlist.Count == 1 && _currentCtrl is LabelGroupBox) { this.改变颜色ToolStripMenuItem.Enabled = true; this.标题位置ToolStripMenuItem.Enabled = true; } else { this.改变颜色ToolStripMenuItem.Enabled = false; this.标题位置ToolStripMenuItem.Enabled = false; } } switch (_dragType) //改变方框位置大小 { case DragType.Top: { if (cid.Equals("8890")) break;//8890为分割线,不能改变高度 ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y + delta.Y, _recter.Rect.Width, _recter.Rect.Height + delta.Y * (-1)); _firstPoint = e.Location; break; } case DragType.RightTop: { if (cid.Equals("8890")) break; ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y + delta.Y, _recter.Rect.Width + delta.X, _recter.Rect.Height + delta.Y * (-1)); _firstPoint = e.Location; break; } case DragType.RightBottom: { if (cid.Equals("8890")) break; ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y, _recter.Rect.Width + delta.X, _recter.Rect.Height + delta.Y); _firstPoint = e.Location; break; } case DragType.Right: { ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y, _recter.Rect.Width + delta.X, _recter.Rect.Height); _firstPoint = e.Location; break; } case DragType.LeftTop: { if (cid.Equals("8890")) break; ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X + delta.X, _recter.Rect.Y + delta.Y, _recter.Rect.Width + delta.X * (-1), _recter.Rect.Height + delta.Y * (-1)); _firstPoint = e.Location; break; } case DragType.LeftBottom: { if (cid.Equals("8890")) break; ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X + delta.X, _recter.Rect.Y, _recter.Rect.Width + delta.X * (-1), _recter.Rect.Height + delta.Y); _firstPoint = e.Location; break; } case DragType.Left: { ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X + delta.X, _recter.Rect.Y, _recter.Rect.Width + delta.X * (-1), _recter.Rect.Height); _firstPoint = e.Location; break; } case DragType.Center: { //Invalidate(); //Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); //_recter.Rect = new Rectangle(_recter.Rect.X + delta.X, _recter.Rect.Y + delta.Y, _recter.Rect.Width, _recter.Rect.Height); //object[] obj = (object[])_currentCtrl.Tag; //ControlProperty cp = obj[obj.Length - 1] as ControlProperty; //if (cp != null) //{ // cp.Width = _currentCtrl.Width; // cp.Height = _currentCtrl.Height; // cp.Top = Convert.ToInt16(_recter.Rect.Y - 41); // cp.Left = Convert.ToInt16(_recter.Rect.X - 18); // cp.TabIndex = _currentCtrl.TabIndex; //} //_firstPoint = e.Location; //break; Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); for (int i = 0; i < Contrlist.Count; i++) { ControlRect cp = Contrlist[i] as ControlRect; if (cp != null) { cp.control.Width = cp.control.Width; cp.control.Height = cp.control.Height; cp.control.Left = Convert.ToInt16(cp.control.Left + delta.X); ; cp.control.Top = Convert.ToInt16(cp.control.Top + delta.Y); cp.control.TabIndex = cp.control.TabIndex; } } if (!isMove && (delta.Y != 0 || delta.X != 0)) { _themainhost.Invalidate(); isMove = true; } _recter.Rect = new Rectangle(_recter.Rect.X + delta.X, _recter.Rect.Y + delta.Y, _recter.Rect.Width, _recter.Rect.Height); object[] obj2 = (object[])_currentCtrl.Tag; ControlProperty cp2 = obj2[obj2.Length - 1] as ControlProperty; if (cp2 != null) { cp2.Width = _currentCtrl.Width; cp2.Height = _currentCtrl.Height; cp2.Top = Convert.ToInt16(_recter.Rect.Y - 41); cp2.Left = Convert.ToInt16(_recter.Rect.X - 18); cp2.TabIndex = _currentCtrl.TabIndex; } _firstPoint = e.Location; break; } case DragType.Bottom: { if (cid.Equals("8890")) break; ParentControlRefresh(); Point delta = new Point(e.Location.X - _firstPoint.X, e.Location.Y - _firstPoint.Y); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y, _recter.Rect.Width, _recter.Rect.Height + delta.Y); _firstPoint = e.Location; break; } default: { break; } } } if (_mouseDown) { Invalidate2(false); } base.OnMouseMove(e); } private void ResizeToRectangle(Point p) { DrawRectangle(); MouseRect.Width = p.X - MouseRect.Left; MouseRect.Height = p.Y - MouseRect.Top; DrawRectangle(); } private void DrawStart(Point StartPoint) { this.Capture = true; Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height)); MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0); } private void DrawRectangle() { Rectangle rect = this.RectangleToScreen(MouseRect); ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed); } // List lists = new List(); //List Contrlist = null;//控件集合 private class ControlRect { public Control control { get; set; } public Rectangle rects { get; set; } } //List rects = null; //bool isDrag = false; /// /// 鼠标单击 /// /// protected override void OnMouseDown(MouseEventArgs e) { mouseDownPoint = e.Location;//记录初始位置 if (!ControlState)//ctrl状态 { Contrlist = new List(); } //当ctrl建按下时,否则new下集合 if (e.Button == MouseButtons.Left) //左键 { _themainhost.Text = "X:" + e.Location.X.ToString() + "Y:" + e.Location.Y.ToString(); bool flag = false;//是否选中控件 foreach (Control c in _themainhost.Controls) //遍历控件容器 看是否选中其中某几个控件 { Rectangle r = c.Bounds; r = _themainhost.RectangleToScreen(r); r = this.RectangleToClient(r); Rectangle rr = r; rr.Inflate(10, 10);//放大指定量 if (rr.Contains(e.Location)) { _recter.Rect = r; _currentCtrl = c; ControlProperty cp = new ControlProperty(c); object[] obj = (object[])c.Tag;//拿到选中的控件信息 if (obj.Length <= 8) { List list = obj.ToList(); if (obj.Length == 7)//Length=7是新增的控件 { //没有id属性就赋值为空 string id = null; list.Add(id); } if (cp != null) { cp.Width = _currentCtrl.Width; cp.Height = _currentCtrl.Height; cp.Top = Convert.ToInt16(_recter.Rect.Y - 41); cp.Left = Convert.ToInt16(_recter.Rect.X - 18); cp.TabIndex = _currentCtrl.TabIndex; } list.Add(cp); c.Tag = list.ToArray(); cp = list[8] as ControlProperty; if (c is LabelGroupBox || c is Panel || c is LabelDivider) { cp.GroupName = obj[1] + ""; DataTable group = SqlHelper.ExecuteDataTable(string.Format("select * from p_systemAddGroup where id='{0}'", list[7])); if (group != null && group.Rows.Count > 0&& group.Columns.Count>=10) { string name = group.Rows[0][8].ToString(); string order = group.Rows[0][9].ToString(); cp.PageName = name; cp.PageOrder = order; } } } else { List list = obj.ToList(); cp = list[8] as ControlProperty; if (cp != null) { cp.Width = _currentCtrl.Width; cp.Height = _currentCtrl.Height; cp.Top = Convert.ToInt16(_recter.Rect.Y - 41); cp.Left = Convert.ToInt16(_recter.Rect.X - 18); cp.TabIndex = _currentCtrl.TabIndex; } } MyFormDesigner myForm = (GetForm(this) as MyFormDesigner); myForm.propertyGrid1.SelectedObject = cp; //选中控件 _recter.IsForm = false; flag = true; ControlRect cr = new ControlRect(); cr.rects = r; cr.control = c; if (Contrlist.Cast().FirstOrDefault(x => x.control == c) == null) Contrlist.Add(cr);//选中的控件 //rects.Add(r); Invalidate2(false); //dic.Add(_currentCtrl, _recter);//将控件存起来 break; } } if (!flag) //没有控件被选中,判断是否选中控件容器 { Invalidate(); Contrlist.Clear(); Rectangle r = _themainhost.Bounds; r = Parent.RectangleToScreen(r); r = this.RectangleToClient(r); if (r.Contains(e.Location)) { _recter.Rect = r; _recter.IsForm = true; _currentCtrl = null; ControlParentProperty cp = null; if (_themainhost.Tag == null) { cp = new ControlParentProperty(_themainhost); _themainhost.Tag = cp; } cp = _themainhost.Tag as ControlParentProperty; MyFormDesigner myForm = (GetForm(this) as MyFormDesigner); myForm.propertyGrid1.SelectedObject = cp; //选中控件容器 Invalidate2(false); } } DragType dt = _recter.GetMouseDragType(e.Location); //判断是否可以进行鼠标操作 if (dt != DragType.None) { _mouseDown = true; _firstPoint = e.Location; _dragType = dt; } base.OnMouseDown(e); } //MouseIsDown = true; //DrawStart(e.Location); } Rectangle MouseRect = Rectangle.Empty; protected override void OnMouseUp(MouseEventArgs e) { if (e.Button == MouseButtons.Left) //左键弹起 { #region 画线 //this.Capture = false; //Cursor.Clip = Rectangle.Empty; //MouseIsDown = false; //DrawRectangle(); //if (MouseRect.Width > 0) //{ // foreach (Control ct in _themainhost.Controls) // { // Rectangle r = ct.Bounds; // r = _themainhost.RectangleToScreen(r); // r = this.RectangleToClient(r); // Rectangle rr = r; // rr.Inflate(10, 10); // if (MouseRect.Contains(ct.Location)) // { // isDrag = true; // _recter.Rect = r; // ControlRect cr = new ControlRect(); // cr.control = ct; // cr.rects = _recter.Rect; // Contrlist.Add(cr); // } // } //} #endregion foreach (Control c in _themainhost.Controls) //遍历控件容器 看是否选中其中某几个控件 { int sl = 0; for (int i = 0; i < Contrlist.Count; i++) { if (c.Name == Contrlist[i].control.Name) { sl++; break; } } if (sl != 1) { Invalidate(); } else { } } if (isMove) { isMove = false; } MouseRect = Rectangle.Empty; _mouseDown = false; _firstPoint = new Point(); _dragType = DragType.None; Invalidate2(true); if (_currentCtrl != null) { //鼠标放开时,刷新当前选中控件的数据(下方显示) object[] obj = (object[])_currentCtrl.Tag;//拿到选中的控件信息 List list = obj.ToList(); if (obj.Length >= 8&&list[8] is ControlProperty) { ControlProperty cp = list[8] as ControlProperty; if (cp != null) { cp.Width = _currentCtrl.Width; cp.Height = _currentCtrl.Height; cp.Top = Convert.ToInt16(_recter.Rect.Y - 41); cp.Left = Convert.ToInt16(_recter.Rect.X - 18); cp.TabIndex = _currentCtrl.TabIndex; MyFormDesigner myForm = (GetForm(this) as MyFormDesigner); myForm.propertyGrid1.SelectedObject = cp; //选中控件 } } } } base.OnMouseUp(e); } protected override void OnDragEnter(DragEventArgs drgevent) { drgevent.Effect = DragDropEffects.Copy; base.OnDragEnter(drgevent); } protected override void OnDragDrop(DragEventArgs drgevent) { try { object[] strs = (object[])drgevent.Data.GetData(typeof(object[])); //获取拖拽数据 string fieldid = strs[0].ToString(); string username = strs[1].ToString(); string fieldtypeid = strs[2].ToString(); string fieldName = strs[3].ToString(); string defaultValue = strs[4].ToString(); string column_Id = strs[5].ToString(); string orderId = strs[6].ToString(); Control ctrl = ControlHelper.CreateControl(username, fieldName, Convert.ToInt32(fieldtypeid)); //实例化控件 ctrl.Location = _themainhost.PointToClient(new Point(drgevent.X, drgevent.Y)); //屏幕坐标转换成控件容器坐标 ctrl.Tag = new object[] { fieldid, username, fieldtypeid, fieldName, defaultValue, column_Id, orderId }; ControlProperty cp = null; if (ctrl is LabelGroupBox) { cp = new ControlProperty(ctrl); _themainhost.Controls.Add(ctrl); cp.Name = username; ctrl.SendToBack(); } else if (ctrl is Panel) { cp = new ControlProperty(ctrl); _themainhost.Controls.Add(ctrl); cp.Name = username; ctrl.SendToBack(); } else if (ctrl is LabelDivider) { cp = new ControlProperty(ctrl); _themainhost.Controls.Add(ctrl); cp.Name = username; ctrl.SendToBack(); } else { cp = new ControlProperty(ctrl); _themainhost.Controls.Add(ctrl); ctrl.BringToFront(); } _currentCtrl = ctrl; (GetForm(this) as MyFormDesigner).propertyGrid1.SelectedObject = cp; Rectangle r = _themainhost.RectangleToScreen(ctrl.Bounds); r = this.RectangleToClient(r); _recter.Rect = r; _recter.IsForm = false; Invalidate2(false); } catch (Exception ex) { MessageBox.Show(ex.Message); } base.OnDragDrop(drgevent); } bool state = false;//鼠标弹起 bool ControlState = false;//ctrl状态 List recters = new List();//装控件和框的值 /// /// 键盘处理(取消方向键对控件的焦点的控件,用自己自定义的函数处理各个方向键的处理函数) /// /// /// /// protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.LButton | Keys.ShiftKey | Keys.Control: ControlState = true; Invalidate2(state); return base.ProcessCmdKey(ref msg, keyData); case Keys.Up: if (Contrlist != null) { for (int i = 0; i < Contrlist.Count; i++) { Contrlist[i].control.Location = new Point(Contrlist[i].control.Location.X, Contrlist[i].control.Location.Y - 1); Contrlist[i].rects = new Rectangle(Contrlist[i].rects.X, Contrlist[i].rects.Y - 1, Contrlist[i].rects.Width, Contrlist[i].rects.Height); _recter.Rect = Contrlist[i].rects; _firstPoint = Contrlist[i].control.Location; } } break; case Keys.Down: if (Contrlist != null) { for (int i = 0; i < Contrlist.Count; i++) { Contrlist[i].control.Location = new Point(Contrlist[i].control.Location.X, Contrlist[i].control.Location.Y + 1); Contrlist[i].rects = new Rectangle(Contrlist[i].rects.X, Contrlist[i].rects.Y + 1, Contrlist[i].rects.Width, Contrlist[i].rects.Height); _recter.Rect = Contrlist[i].rects; _firstPoint = Contrlist[i].control.Location; } state = false; } break; case Keys.Left: if (Contrlist != null) { for (int i = 0; i < Contrlist.Count; i++) { Contrlist[i].control.Location = new Point(Contrlist[i].control.Location.X - 1, Contrlist[i].control.Location.Y); Contrlist[i].rects = new Rectangle(Contrlist[i].rects.X - 1, Contrlist[i].rects.Y, Contrlist[i].rects.Width, Contrlist[i].rects.Height); _recter.Rect = Contrlist[i].rects; _firstPoint = Contrlist[i].control.Location; } state = false; } break; case Keys.Right: if (Contrlist != null) { for (int i = 0; i < Contrlist.Count; i++) { Contrlist[i].control.Location = new Point(Contrlist[i].control.Location.X + 1, Contrlist[i].control.Location.Y); Contrlist[i].rects = new Rectangle(Contrlist[i].rects.X + 1, Contrlist[i].rects.Y, Contrlist[i].rects.Width, Contrlist[i].rects.Height); _recter.Rect = Contrlist[i].rects; _firstPoint = Contrlist[i].control.Location; } state = false; } break; case Keys.Left | Keys.Shift: if (Contrlist != null) { _currentCtrl.Location = new Point(_currentCtrl.Location.X - 1, _currentCtrl.Location.Y); _recter.Rect = new Rectangle(_recter.Rect.X - 1, _recter.Rect.Y, _recter.Rect.Width + 1, _recter.Rect.Height); _firstPoint = _currentCtrl.Location; state = true; } break; case Keys.Right | Keys.Shift: if (Contrlist != null) { _currentCtrl.Location = new Point(_currentCtrl.Location.X + 1, _currentCtrl.Location.Y); _recter.Rect = new Rectangle(_recter.Rect.X + 1, _recter.Rect.Y, _recter.Rect.Width - 1, _recter.Rect.Height); _firstPoint = _currentCtrl.Location; state = true; } break; case Keys.Up | Keys.Shift: if (Contrlist != null) { _currentCtrl.Location = new Point(_currentCtrl.Location.X, _currentCtrl.Location.Y - 1); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y - 1, _recter.Rect.Width, _recter.Rect.Height + 1); _firstPoint = _currentCtrl.Location; state = true; } break; case Keys.Down | Keys.Shift: if (Contrlist != null) { _currentCtrl.Location = new Point(_currentCtrl.Location.X, _currentCtrl.Location.Y + 1); _recter.Rect = new Rectangle(_recter.Rect.X, _recter.Rect.Y + 1, _recter.Rect.Width, _recter.Rect.Height - 1); _firstPoint = _currentCtrl.Location; state = true; } break; } Invalidate2(state); return true; } protected override void OnKeyUp(KeyEventArgs e) { switch (e.KeyCode) { case Keys.LButton | Keys.ShiftKey: ControlState = false; //mouse_event(0x0002 | 0x0004, 0, 0, 0, 0); break; } } private Form GetForm(Control ctr) { if (ctr is Form) { return ctr as Form; } else { return GetForm(ctr.Parent);//获取或设置控件的父容器 } } private void Invalidate2(bool mouseUp) { if (mouseUp) //鼠标弹起 更新底层控件 { //this.Invalidate(); if (Parent != null && ControlState == false) //更新父控件 { Rectangle rc = new Rectangle(this.Location, this.Size); Parent.Invalidate(rc, true); } if (Contrlist.Count > 0) //更新底层控件的位置、大小 { Rectangle r = _recter.Rect; r = this.RectangleToScreen(r); r = _themainhost.RectangleToClient(r); if (_currentCtrl == null) return; _currentCtrl.SetBounds(r.Left, r.Top, r.Width, r.Height); } else //更新控件容器大小 { Rectangle r = _recter.Rect; r = this.RectangleToScreen(r); r = Parent.RectangleToClient(r); _themainhost.SetBounds(r.Left, r.Top, r.Width, r.Height); } this.Invalidate(); } //SetProp(); } public void SetProp() { if (_currentCtrl != null)//当前是否有被操作控件 { object[] obj = (object[])_currentCtrl.Tag; ControlProperty cp = obj[obj.Length - 1] as ControlProperty; if (cp != null) { cp.Width = _currentCtrl.Width; cp.Height = _currentCtrl.Height; cp.Top = _currentCtrl.Top; cp.Left = _currentCtrl.Left; cp.TabIndex = _currentCtrl.TabIndex; } } else { if (_themainhost.Tag != null) { ControlParentProperty cpp = _themainhost.Tag as ControlParentProperty; cpp.Width = _themainhost.Width; cpp.Height = _themainhost.Height; } } } //靠边 private void PullOver(object sender, EventArgs e) { int Distance = 0;//移动距离 List ControlList = new List();//移动路线上的控件 bool overlap = false;//是否重叠 string TheCommand = sender.ToString();//点击的命令名称 if (Contrlist.Count == 0) { if (_currentCtrl != null)//当前是否有被操作控件 { object[] obj = (object[])_currentCtrl.Tag; ControlProperty cp = obj[obj.Length - 1] as ControlProperty; if (cp != null) { switch (TheCommand) { case "居左": { cp.Left = 5; break; } case "左靠": { Distance = 0; //int MinLeftLoaction = 0; foreach (Control c in _themainhost.Controls) { if ((c.Top + c.Height >= cp.Top && c.Top <= cp.Top) || (cp.Top <= c.Top && cp.Top + cp.Height >= c.Top)) { if (c.Name != _currentCtrl.Name) { ControlList.Add(c); //出现重叠情况 if ((c.Left <= cp.Left && cp.Left <= c.Left + c.Width) || (cp.Left <= c.Left && c.Left <= cp.Left + cp.Width)) { overlap = true; } } if (cp.Left > c.Left) { if (c.Left + c.Width > Distance) Distance = c.Left + c.Width; } } } if (overlap) { ControlList = ControlList.OrderBy(t => t.Left).ToList(); for (int i = 0; i < ControlList.Count - 1; i++) { if (ControlList[i + 1].Left - ControlList[i].Left - ControlList[i].Width > cp.Width)//可以插入 { Distance = ControlList[i].Left + ControlList[i].Width; break; } else { Distance = ControlList[i + 1].Left + ControlList[i + 1].Width; if (Distance + ControlList[i].Width > _themainhost.Width) Distance = 0; } } } cp.Left = Distance + 5; break; } case "居右": { cp.Left = _currentCtrl.Parent.Width - cp.Width - 25; break; } case "右靠": { Distance = _currentCtrl.Parent.Width - cp.Width - 25; foreach (Control c in _themainhost.Controls) { int i = 0; if ((c.Top + c.Height >= cp.Top && c.Top <= cp.Top) || (cp.Top <= c.Top && cp.Top + cp.Height >= c.Top)) { if (c.Name != _currentCtrl.Name) { ControlList.Add(c); //出现重叠情况 if ((c.Left <= cp.Left && cp.Left <= c.Left + c.Width) || (cp.Left <= c.Left && c.Left <= cp.Left + cp.Width)) { overlap = true; } } if (cp.Left < c.Left) { i++; if (i == 1) { Distance = c.Left - cp.Width; } else if (cp.Left + cp.Width < Distance) { Distance = c.Left - cp.Width; } } } } if (overlap) { ControlList = ControlList.OrderByDescending(t => t.Left).ToList(); for (int i = 0; i < ControlList.Count - 1; i++) { if (ControlList[i].Left - ControlList[i + 1].Left - ControlList[i + 1].Width > cp.Width) { Distance = ControlList[i].Left - cp.Width; break; } else { Distance = ControlList[i + 1].Left - cp.Width; //Distance = ControlList[i ].Left - cp.Width; if (Distance <= 0) Distance = _themainhost.Width - cp.Width - 25; } } } cp.Left = Distance - 5; break; } case "居上": { cp.Top = 5; break; } case "上靠": { Distance = 0; foreach (Control c in _themainhost.Controls) { if ((c.Left + c.Width >= cp.Left && c.Left <= cp.Left) || (cp.Left <= c.Left && cp.Left + cp.Width >= c.Left)) { if (c.Name != _currentCtrl.Name) { ControlList.Add(c); //出现重叠情况 if ((c.Top <= cp.Top && cp.Top <= c.Top + c.Height) || (cp.Top <= c.Top && c.Top <= cp.Top + cp.Height)) { overlap = true; } } if (cp.Top > c.Top) { if (c.Top + c.Height > Distance) Distance = c.Top + c.Height; } } } if (overlap) { ControlList = ControlList.OrderBy(t => t.Top).ToList(); for (int i = 0; i < ControlList.Count - 1; i++) { if (ControlList[i + 1].Top - ControlList[i].Top - ControlList[i].Height > cp.Height)//可以插入 { Distance = ControlList[i].Top + ControlList[i].Height; break; } else { Distance = ControlList[i + 1].Top + ControlList[i + 1].Height; if (Distance + ControlList[i].Height > _themainhost.Height) Distance = _themainhost.Height - cp.Height; } } } cp.Top = Distance + 5; break; } } } } } else { //循环选中的对象 for (int i = 0; i < Contrlist.Count; i++) { ControlRect cp = Contrlist[i] as ControlRect; if (cp != null) { switch (TheCommand) { case "居左": { cp.control.Left = 5; break; } case "居右": { cp.control.Left = _currentCtrl.Parent.Width - cp.control.Width - 25; ; break; } case "居上": { cp.control.Top = 5; break; } case "左靠": { Contrlist = Contrlist.OrderBy(t => t.control.Left).ToList(); cp = Contrlist[i] as ControlRect; Distance = 0; foreach (Control c in _themainhost.Controls) { if ((c.Top + c.Height >= cp.control.Top && c.Top <= cp.control.Top) || (cp.control.Top <= c.Top && cp.control.Top + cp.control.Height >= c.Top)) { if (cp.control.Left > c.Left) { if (c.Left + c.Width > Distance) Distance = c.Left + c.Width; } } } cp.control.Left = Distance + 5; break; } case "右靠": { Contrlist = Contrlist.OrderByDescending(t => t.control.Left).ToList(); cp = Contrlist[i] as ControlRect; Distance = _currentCtrl.Parent.Width - cp.control.Width - 25; foreach (Control c in _themainhost.Controls) { int number = 0; if ((c.Top + c.Height >= cp.control.Top && c.Top <= cp.control.Top) || (cp.control.Top <= c.Top && cp.control.Top + cp.control.Height >= c.Top)) { if (cp.control.Left < c.Left) { number++; if (number == 1) { Distance = c.Left - cp.control.Width; } else if (cp.control.Left + cp.control.Width < Distance) { Distance = c.Left - cp.control.Width; } } } } cp.control.Left = Distance - 5; break; } case "上靠": { Contrlist = Contrlist.OrderBy(t => t.control.Top).ToList(); cp = Contrlist[i] as ControlRect; Distance = 0; foreach (Control c in _themainhost.Controls) { if ((c.Left + c.Width >= cp.control.Left && c.Left <= cp.control.Left) || (cp.control.Left <= c.Left && cp.control.Left + cp.control.Width >= c.Left)) { if (cp.control.Top > c.Top) { if (c.Top + c.Height > Distance) Distance = c.Top + c.Height; } } } cp.control.Top = Distance + 5; break; } } } } } _themainhost.Invalidate(); } //改变颜色 private void ChangeTheColor(object sender, EventArgs e) { if (colorDialog.ShowDialog() == DialogResult.OK) { //获取所选择的颜色 Color colorChoosed = this.colorDialog.Color; string colorHtml = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(colorChoosed.R, colorChoosed.G, colorChoosed.B)); if (_currentCtrl is LabelGroupBox) { LabelGroupBox lgb = _currentCtrl as LabelGroupBox; lgb.BorderColor = ColorTranslator.FromHtml(colorHtml); _themainhost.Invalidate(); } } } //改变标题位置 private void ChangeTitlePosition(object sender, EventArgs e) { string TheCommand = sender.ToString();//点击的命令名称 if (_currentCtrl is LabelGroupBox) { LabelGroupBox lgb = _currentCtrl as LabelGroupBox; switch (TheCommand) { case "靠左": { lgb.TitlePosition = 1; break; } case "居中": { lgb.TitlePosition = 2; break; } case "靠右": { lgb.TitlePosition = 3; break; } } } } [System.Runtime.InteropServices.DllImport("user32")] private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); private void ParentControlRefresh() { if (Parent != null && ControlState == false) //更新父控件 { Rectangle rc = new Rectangle(this.Location, this.Size); Parent.Invalidate(rc, true); } } } } #endregion