using System; using System.Drawing; using System.Windows.Forms; namespace Gs.DevApp.ToolBox { public class DragResizeHelp { #region Field private static bool isDragging = false; private const int Band = 5; private const int MinWidth = 10; private const int MinHeight = 10; private static EnumMousePointPosition m_MousePointPosition; private static Point p, p1; private static Label sbMsg; private static Control dragBox1; private static string dragCurrent_Name = ""; private static DevExpress.XtraBars.Docking.ControlContainer dockPanel1_Container; #endregion #region Inner Object private enum EnumMousePointPosition { MouseSizeNone = 0, //'无 MouseSizeRight = 1, //'拉伸右边框 MouseSizeLeft = 2, //'拉伸左边框 MouseSizeBottom = 3, //'拉伸下边框 MouseSizeTop = 4, //'拉伸上边框 MouseSizeTopLeft = 5, //'拉伸左上角 MouseSizeTopRight = 6, //'拉伸右上角 MouseSizeBottomLeft = 7, //'拉伸左下角 MouseSizeBottomRight = 8, //'拉伸右下角 MouseDrag = 9 // '鼠标拖动 } #endregion #region Constructor public DragResizeHelp() { // Nothing to do. } #endregion private static void InitializeComponent(DevExpress.XtraEditors.XtraForm frm) { DevExpress.XtraBars.Docking.DockManager dockManager1; DevExpress.XtraBars.Docking.DockPanel dragBox2_Container; dockManager1 = new DevExpress.XtraBars.Docking.DockManager(); dragBox2_Container = new DevExpress.XtraBars.Docking.DockPanel(); dockPanel1_Container = new DevExpress.XtraBars.Docking.ControlContainer(); ((System.ComponentModel.ISupportInitialize)(dockManager1)).BeginInit(); // // dockManager1 // dockManager1.Form = frm; dockManager1.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] { dragBox2_Container}); // // dockPanel1 // dragBox2_Container.Controls.Add(dockPanel1_Container); dragBox2_Container.Dock = DevExpress.XtraBars.Docking.DockingStyle.Float; dragBox2_Container.FloatLocation = new System.Drawing.Point(691, 455); dragBox2_Container.ID = new System.Guid("319a7cf1-f26d-4049-8a96-8f9fbbd13cd2"); dragBox2_Container.Location = new System.Drawing.Point(0, 0); dragBox2_Container.Name = "dockPanel1"; dragBox2_Container.OriginalSize = new System.Drawing.Size(200, 200); dragBox2_Container.Size = new System.Drawing.Size(200, 200); dragBox2_Container.Text = "dockPanel1"; dragBox2_Container.Options.AllowDockBottom = false; dragBox2_Container.Options.AllowDockFill = false; dragBox2_Container.Options.AllowDockLeft = false; dragBox2_Container.Options.AllowDockRight = false; dragBox2_Container.Options.AllowDockTop = false; dragBox2_Container.Options.ShowMaximizeButton = false; dragBox2_Container.Options.ShowMaximizeButton = false; // // dockPanel1_Container // dockPanel1_Container.Location = new System.Drawing.Point(5, 32); dockPanel1_Container.Name = "dockPanel1_Container"; dockPanel1_Container.Size = new System.Drawing.Size(190, 164); dockPanel1_Container.TabIndex = 0; // // XtraForm3 // ((System.ComponentModel.ISupportInitialize)(dockManager1)).EndInit(); dragBox2_Container.ResumeLayout(false); frm.ResumeLayout(false); dragBox2_Container.Show(); } #region Public Method public static void RegisterControl(DevExpress.XtraEditors.XtraForm frm, Control control, Control box1, Label lbMsg) { InitializeComponent(frm); if (control != null) { sbMsg = lbMsg; dragBox1 = box1; dragCurrent_Name = box1.Name; control.MouseDown += new MouseEventHandler(control_MouseDown); control.MouseLeave += new EventHandler(control_MouseLeave); control.MouseMove += new MouseEventHandler(control_MouseMove); control.Resize += myPanel_Resize; } } public static void UnRegisterControl(Control control) { if (control != null) { control.MouseDown -= new MouseEventHandler(control_MouseDown); control.MouseLeave -= new EventHandler(control_MouseLeave); control.MouseMove -= new MouseEventHandler(control_MouseMove); } } #endregion private static void control_MouseDown(object sender, MouseEventArgs e) { p.X = e.X; p.Y = e.Y; p1.X = e.X; p1.Y = e.Y; isDragging = true; dragCurrent_Name = (sender as Control).Parent.Name; } private static void myPanel_Resize(object sender, EventArgs e) { Control panel = sender as Control; if (panel != null && panel.Controls.Count > 0) { foreach (Control childControl in panel.Controls) { // childControl.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; childControl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); int newY = (panel.ClientSize.Height - childControl.Height) / 2; childControl.Location = new Point(childControl.Location.X, newY); } } } private static void control_MouseLeave(object sender, EventArgs e) { m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; Control control = sender as Control; control.Cursor = Cursors.Arrow; } private static void control_MouseMove(object sender, MouseEventArgs e) { if (!isDragging) { return; } Control lCtrl = (sender as Control); if (e.Button == MouseButtons.Left) { switch (m_MousePointPosition) { case EnumMousePointPosition.MouseDrag: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Top = lCtrl.Top + e.Y - p.Y; break; case EnumMousePointPosition.MouseSizeBottom: lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeBottomRight: lCtrl.Width = lCtrl.Width + e.X - p1.X; lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeRight: lCtrl.Width = lCtrl.Width + e.X - p1.X; // lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTop: lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); break; case EnumMousePointPosition.MouseSizeLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Width = lCtrl.Width - (e.X - p.X); break; case EnumMousePointPosition.MouseSizeBottomLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Width = lCtrl.Width - (e.X - p.X); lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTopRight: lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Width = lCtrl.Width + (e.X - p1.X); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTopLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Width = lCtrl.Width - (e.X - p.X); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); break; default: break; } if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth; if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight; } else { m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态 Control control = sender as Control; switch (m_MousePointPosition) //'改变光标 { case EnumMousePointPosition.MouseSizeNone: control.Cursor = Cursors.Arrow; //'箭头 break; case EnumMousePointPosition.MouseDrag: control.Cursor = Cursors.SizeAll; //'四方向 break; case EnumMousePointPosition.MouseSizeBottom: control.Cursor = Cursors.SizeNS; //'南北 break; case EnumMousePointPosition.MouseSizeTop: control.Cursor = Cursors.SizeNS; //'南北 break; case EnumMousePointPosition.MouseSizeLeft: control.Cursor = Cursors.SizeWE; //'东西 break; case EnumMousePointPosition.MouseSizeRight: control.Cursor = Cursors.SizeWE; //'东西 break; case EnumMousePointPosition.MouseSizeBottomLeft: control.Cursor = Cursors.SizeNESW; //'东北到南西 break; case EnumMousePointPosition.MouseSizeBottomRight: control.Cursor = Cursors.SizeNWSE; //'东南到西北 break; case EnumMousePointPosition.MouseSizeTopLeft: control.Cursor = Cursors.SizeNWSE; //'东南到西北 break; case EnumMousePointPosition.MouseSizeTopRight: control.Cursor = Cursors.SizeNESW; //'东北到南西 break; default: break; } } if (dragBox1.Name == dragCurrent_Name) { if (IsOutsideBounds(lCtrl, dragBox1)) { dragBox1.Controls.Remove(lCtrl); dockPanel1_Container.Controls.Add(lCtrl); lCtrl.Left = 0; lCtrl.Top = 10; isDragging = false; dragCurrent_Name = dockPanel1_Container.Name; } } else if (dockPanel1_Container.Name == dragCurrent_Name) { if (IsOutsideBounds(lCtrl, dockPanel1_Container)) { dockPanel1_Container.Controls.Remove(lCtrl); dragBox1.Controls.Add(lCtrl); lCtrl.Left = 0; lCtrl.Top = 10; isDragging = false; dragCurrent_Name = dragBox1.Name; } } sbMsg.Text = lCtrl.Location.ToString() + "容器:" + dragCurrent_Name; } private static EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) { if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) { if (e.X < Band) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottomLeft; } else { return EnumMousePointPosition.MouseSizeLeft; } } } else { if (e.X > -1 * Band + size.Width) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopRight; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottomRight; } else { return EnumMousePointPosition.MouseSizeRight; } } } else { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTop; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottom; } else { return EnumMousePointPosition.MouseDrag; } } } } } else { return EnumMousePointPosition.MouseSizeNone; } } public static bool IsOutsideBounds(Control childControl, Control parentControl) { if (parentControl == null) return true; if (parentControl == null) return true; int _w = childControl.Width / 2; int _h = childControl.Height / 2; // 获取父控件的客户区矩形(相对于父控件自己) Rectangle parentClientRect = parentControl.ClientRectangle; // 获取子控件相对于父控件的位置和尺寸 Rectangle childRectRelativeToParent = childControl.Bounds; // 检查子控件的左边界是否超出了父控件的左边界 if (childRectRelativeToParent.Left < parentClientRect.Left - _w) return true; // 检查子控件的右边界是否超出了父控件的右边界 if (childRectRelativeToParent.Right > parentClientRect.Right + _w) return true; // 检查子控件的顶边界是否超出了父控件的顶边界 if (childRectRelativeToParent.Top < parentClientRect.Top - _h) return true; // 检查子控件的底边界是否超出了父控件的底边界 if (childRectRelativeToParent.Bottom > parentClientRect.Bottom + _h) return true; // 如果子控件的所有边界都在父控件的边界内,则返回false return false; } } }