lu
2025-02-21 db9189bc9225f858949c5ff098282cb136009303
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
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;
 
        public XtraForm1()
        {
            InitializeComponent();
            // 初始化表单
            this.Text = "Draggable Panel Example";
            this.Size = new Size(800, 600);
 
            // 添加鼠标事件处理程序
            panTuo.MouseDown += new MouseEventHandler(DraggablePanel_MouseDown);
            panTuo.MouseMove += new MouseEventHandler(DraggablePanel_MouseMove);
            panTuo.MouseUp += new MouseEventHandler(DraggablePanel_MouseUp);
        }
        private void DraggablePanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                dragCursorPoint = Cursor.Position;
                dragPanelPoint = panTuo.Location;
                panTuo.Cursor = Cursors.SizeAll;
 
            }
        }
 
        private void DraggablePanel_MouseMove(object sender, MouseEventArgs e)
        {
            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))
                {
                    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)));
                }
                else
                {
                    lbMsg.Text = panTuo.Location.ToString() + "-" + bounds.ToString();
                }
            }
 
 
        }
        public static bool IsOutsideBounds(Control child, Control parent)
        {
            // 获取控件相对于其父容器的位置和大小
            Rectangle childBounds = new Rectangle(child.Location, child.Size);
            Rectangle parentBounds = new Rectangle(Point.Empty, parent.ClientSize); // 使用ClientSize获取容器的工作区大小
 
            // 注意:这里我们假设child是parent的直接子控件,因此child.Location是相对于parent的。
            // 如果child不是parent的直接子控件,你需要先找到child相对于parent的相对位置。
 
            // 检查childBounds是否超出了parentBounds的任何一个边界
            return !parentBounds.Contains(childBounds);
        }
 
        private void DraggablePanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
            }
        }
 
 
    }
 
}