using DevExpress.XtraBars.Docking; using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Gs.DevApp.DevFrm { public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm { private bool isDragging = false; private Point dragCursorPoint; private Point dragPanelPoint; private Control dragSourceContainer; public XtraForm1() { InitializeComponent(); // 初始化表单 this.Text = "Draggable Panel Example"; this.Size = new Size(800, 600); // 添加鼠标事件处理程序 // 遍历 parentPanel 中的所有子控件 foreach (Control control in panelControl1.Controls) { // 检查当前控件是否是一个 Panel if (control is PanelControl panel) { // 在这里对子 Panel 执行操作 // 例如,打印出子 Panel 的名字(如果有设置 Name 属性) Console.WriteLine(panel.Name ?? "无名Panel"); // 或者你可以调用其他方法,传递这个 panel 作为参数 // SomeMethod(panel); control.MouseDown += new MouseEventHandler(DraggablePanel_MouseDown); control.MouseMove += new MouseEventHandler(DraggablePanel_MouseMove); control.MouseUp += new MouseEventHandler(DraggablePanel_MouseUp); } } } private void DraggablePanel_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // 从事件参数 DragEventArgs 中获取被拖动的元素 PanelControl _soucePanel = (PanelControl)sender; isDragging = true; dragCursorPoint = Cursor.Position; dragPanelPoint = _soucePanel.Location; _soucePanel.Cursor = Cursors.SizeAll; dragSourceContainer = _soucePanel.Parent; } } private void DraggablePanel_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { PanelControl _soucePanel = (PanelControl)sender; string _mmm = dragSourceContainer.Name.ToString(); if (_mmm == "panelControl1") { _soucePanel.Location = new Point( dragPanelPoint.X + Cursor.Position.X - dragCursorPoint.X, dragPanelPoint.Y + Cursor.Position.Y - dragCursorPoint.Y); Point newLocation = _soucePanel.Location; Rectangle bounds = panelControl1.Bounds; if (IsOutsideBounds(_soucePanel, panelControl1)) { lbMsg.Text = _soucePanel.Location.ToString() + "-" + bounds.ToString() + ":pp" + _mmm; panelControl1.Controls.Remove(_soucePanel); dockPanel1.Controls.Add(_soucePanel); RefreshControls(); _soucePanel.Location = new Point( Math.Max(0, Math.Min(dockPanel1.Width - _soucePanel.Width, newLocation.X - dockPanel1.Location.X)), Math.Max(0, Math.Min(dockPanel1.Height - _soucePanel.Height, newLocation.Y - dockPanel1.Location.Y))); } else { lbMsg.Text = _soucePanel.Location.ToString() + "-" + bounds.ToString() + ":pp" + _mmm; } return; } if (_mmm == "dockPanel1_Container") { _soucePanel.Location = new Point( dragPanelPoint.X + Cursor.Position.X - dragCursorPoint.X, dragPanelPoint.Y + Cursor.Position.Y - dragCursorPoint.Y); Point newLocation = _soucePanel.Location; Rectangle bounds = dockPanel1.Bounds; if (IsOutsideBounds(_soucePanel, dockPanel1)) { lbMsg.Text = _soucePanel.Location.ToString() + "-" + bounds.ToString() + ":pp" + _mmm; dockPanel1.Controls.Remove(_soucePanel); panelControl1.Controls.Add(_soucePanel); RefreshControls(); _soucePanel.Location = new Point( Math.Max(0, Math.Min(panelControl1.Width - _soucePanel.Width, newLocation.X - panelControl1.Location.X)), Math.Max(0, Math.Min(panelControl1.Height - _soucePanel.Height, newLocation.Y - panelControl1.Location.Y))); } else { lbMsg.Text = _soucePanel.Location.ToString() + "-" + bounds.ToString() + ":pp" + _mmm; } return; } } } public static bool IsOutsideBounds(Control childControl, Control parentControl) { 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; } private void DraggablePanel_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isDragging = false; dragSourceContainer = panTuo.Parent; } } /// /// 对控件中的项进行排列 /// private void RefreshControls() { int y = 0; foreach (PanelControl btn in dockPanel1_Container.Controls) { btn.Left = 0; btn.Top = y; y += btn.Height + 15; } } } }