lu
2025-02-22 363156238c2443d2759e541e65a0f1f148c654ec
DevApp/Gs.DevApp/DevFrm/XtraForm1.cs
@@ -14,11 +14,10 @@
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        private bool isDragging = false;
        private Point dragCursorPoint;
        private Point dragPanelPoint;
        private Control dragSourceContainer;
        public XtraForm1()
        {
            InitializeComponent();
@@ -27,19 +26,40 @@
            this.Size = new Size(800, 600);
            // 添加鼠标事件处理程序
            panTuo.MouseDown += new MouseEventHandler(DraggablePanel_MouseDown);
            panTuo.MouseMove += new MouseEventHandler(DraggablePanel_MouseMove);
            panTuo.MouseUp += new MouseEventHandler(DraggablePanel_MouseUp);
            // 遍历 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 = panTuo.Location;
                panTuo.Cursor = Cursors.SizeAll;
                dragPanelPoint = _soucePanel.Location;
                _soucePanel.Cursor = Cursors.SizeAll;
                dragSourceContainer = _soucePanel.Parent;
            }
        }
@@ -47,43 +67,85 @@
        {
            if (isDragging)
            {
                panTuo.Location = new Point(
                    dragPanelPoint.X + Cursor.Position.X - dragCursorPoint.X,
                    dragPanelPoint.Y + Cursor.Position.Y - dragCursorPoint.Y);
                Point newLocation = panTuo.Location;
                Rectangle bounds = panelControl1.Bounds;
                lbMsg.Text = panTuo.Location.ToString() + "-" + bounds.ToString();
                if (IsOutsideBounds(panTuo, panelControl1))
                PanelControl _soucePanel = (PanelControl)sender;
                string _mmm = dragSourceContainer.Name.ToString();
                if (_mmm == "panelControl1")
                {
                    lbMsg.Text = "超了";
                     panelControl1.Controls.Remove(panTuo);
                    dockPanel1.Controls.Add(panTuo);
                    panTuo.Location = new Point(
                       Math.Max(0, Math.Min(dockPanel1.Width - panTuo.Width, newLocation.X - dockPanel1.Location.X)),
                       Math.Max(0, Math.Min(dockPanel1.Height - panTuo.Height, newLocation.Y - dockPanel1.Location.Y)));
                    _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;
                }
                else
                if (_mmm == "dockPanel1_Container")
                {
                    lbMsg.Text = panTuo.Location.ToString() + "-" + bounds.ToString();
                    _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 child, Control parent)
        public static bool IsOutsideBounds(Control childControl, Control parentControl)
        {
            // 获取控件相对于其父容器的位置和大小
            Rectangle childBounds = new Rectangle(child.Location, child.Size);
            Rectangle parentBounds = new Rectangle(Point.Empty, parent.ClientSize); // 使用ClientSize获取容器的工作区大小
            int _w = childControl.Width / 2;
            int _h = childControl.Height / 2;
            // 获取父控件的客户区矩形(相对于父控件自己)
            Rectangle parentClientRect = parentControl.ClientRectangle;
            // 注意:这里我们假设child是parent的直接子控件,因此child.Location是相对于parent的。
            // 如果child不是parent的直接子控件,你需要先找到child相对于parent的相对位置。
            // 获取子控件相对于父控件的位置和尺寸
            Rectangle childRectRelativeToParent = childControl.Bounds;
            // 检查childBounds是否超出了parentBounds的任何一个边界
            return !parentBounds.Contains(childBounds);
            // 检查子控件的左边界是否超出了父控件的左边界
            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)
@@ -91,10 +153,25 @@
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                dragSourceContainer = panTuo.Parent;
            }
        }
        /// <summary>
        /// 对控件中的项进行排列
        /// </summary>
        private void RefreshControls()
        {
            int y = 0;
            foreach (PanelControl btn in dockPanel1_Container.Controls)
            {
                btn.Left = 0;
                btn.Top = y;
                y += btn.Height + 15;
            }
        }
    }
}