lu
2025-02-22 363156238c2443d2759e541e65a0f1f148c654ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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;
            }
        }
 
 
        /// <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;
            }
        }
 
    }
 
}