From 363156238c2443d2759e541e65a0f1f148c654ec Mon Sep 17 00:00:00 2001 From: lu <99954486@qq.com> Date: 星期六, 22 二月 2025 09:04:17 +0800 Subject: [PATCH] 调拨 --- DevApp/Gs.DevApp/DevFrm/XtraForm1.cs | 149 +++++++++++++++++++++++++++++++++++++------------ 1 files changed, 113 insertions(+), 36 deletions(-) diff --git a/DevApp/Gs.DevApp/DevFrm/XtraForm1.cs b/DevApp/Gs.DevApp/DevFrm/XtraForm1.cs index 5157073..3ca0776 100644 --- a/DevApp/Gs.DevApp/DevFrm/XtraForm1.cs +++ b/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; - // 娉ㄦ剰锛氳繖閲屾垜浠亣璁綾hild鏄痯arent鐨勭洿鎺ュ瓙鎺т欢锛屽洜姝hild.Location鏄浉瀵逛簬parent鐨勩�� - // 濡傛灉child涓嶆槸parent鐨勭洿鎺ュ瓙鎺т欢锛屼綘闇�瑕佸厛鎵惧埌child鐩稿浜巔arent鐨勭浉瀵逛綅缃�� + // 鑾峰彇瀛愭帶浠剁浉瀵逛簬鐖舵帶浠剁殑浣嶇疆鍜屽昂瀵� + Rectangle childRectRelativeToParent = childControl.Bounds; - // 妫�鏌hildBounds鏄惁瓒呭嚭浜唒arentBounds鐨勪换浣曚竴涓竟鐣� - 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; + } + } + } } \ No newline at end of file -- Gitblit v1.9.3