| | |
| | | using DevExpress.XtraCharts; |
| | | using DevExpress.XtraBars.Docking; |
| | | using System; |
| | | using System.Drawing; |
| | | using System.Windows.Forms; |
| | |
| | | public class DragResizeHelp |
| | | { |
| | | #region Field |
| | | private static bool isDragging = false; |
| | | private bool isDragging = false; |
| | | private const int Band = 5; |
| | | private const int MinWidth = 10; |
| | | private const int MinHeight = 10; |
| | | private static EnumMousePointPosition m_MousePointPosition; |
| | | private static Point p, p1; |
| | | private static Label sbMsg; |
| | | private static Control dragBox1; |
| | | private static string dragCurrent_Name = ""; |
| | | private static DevExpress.XtraBars.Docking.ControlContainer dockPanel1_Container; |
| | | private const int MinWidth = 50; |
| | | private const int MinHeight = 30; |
| | | private EnumMousePointPosition m_MousePointPosition; |
| | | private Point p, p1; |
| | | private Control dragBox1;//容器1 |
| | | private string dragBoxCurrentName = "";//当前容器的名称 |
| | | private Control ctrlCurrentObj;//当前拖拖动的对像 |
| | | private DevExpress.XtraBars.Docking.DockManager dockManager1; |
| | | private DevExpress.XtraBars.Docking.DockPanel dockPanel1; |
| | | private DevExpress.XtraBars.Docking.ControlContainer dockPanel1_Container; |
| | | private System.ComponentModel.IContainer components = null; |
| | | private ToolTip toolTip; |
| | | #endregion |
| | | |
| | | #region Inner Object |
| | | private enum EnumMousePointPosition |
| | | { |
| | |
| | | MouseDrag = 9 // '鼠标拖动 |
| | | } |
| | | #endregion |
| | | |
| | | #region Constructor |
| | | public DragResizeHelp() |
| | | public DragResizeHelp(DevExpress.XtraEditors.XtraForm frm, Control regBox) |
| | | { |
| | | // Nothing to do. |
| | | } |
| | | #endregion |
| | | private static void InitializeComponent(DevExpress.XtraEditors.XtraForm frm) |
| | | { |
| | | DevExpress.XtraBars.Docking.DockManager dockManager1; |
| | | DevExpress.XtraBars.Docking.DockPanel dragBox2_Container; |
| | | dockManager1 = new DevExpress.XtraBars.Docking.DockManager(); |
| | | dragBox2_Container = new DevExpress.XtraBars.Docking.DockPanel(); |
| | | dockPanel1_Container = new DevExpress.XtraBars.Docking.ControlContainer(); |
| | | ((System.ComponentModel.ISupportInitialize)(dockManager1)).BeginInit(); |
| | | toolTip = new ToolTip(); |
| | | this.components = new System.ComponentModel.Container(); |
| | | dragBox1 = regBox; |
| | | this.components = new System.ComponentModel.Container(); |
| | | this.dockManager1 = new DevExpress.XtraBars.Docking.DockManager(this.components); |
| | | dockManager1.BeginUpdate(); |
| | | this.dockPanel1 = new DevExpress.XtraBars.Docking.DockPanel(); |
| | | this.dockPanel1_Container = new DevExpress.XtraBars.Docking.ControlContainer(); |
| | | ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).BeginInit(); |
| | | this.dockPanel1.SuspendLayout(); |
| | | frm.SuspendLayout(); |
| | | // |
| | | // dockManager1 |
| | | // |
| | | dockManager1.Form = frm; |
| | | dockManager1.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] { |
| | | dragBox2_Container}); |
| | | this.dockManager1.Form = frm; |
| | | this.dockManager1.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] { |
| | | this.dockPanel1}); |
| | | this.dockManager1.TopZIndexControls.AddRange(new string[] { |
| | | "DevExpress.XtraBars.BarDockControl", |
| | | "DevExpress.XtraBars.StandaloneBarDockControl", |
| | | "System.Windows.Forms.MenuStrip", |
| | | "System.Windows.Forms.StatusStrip", |
| | | "System.Windows.Forms.StatusBar", |
| | | "DevExpress.XtraBars.Ribbon.RibbonStatusBar", |
| | | "DevExpress.XtraBars.Ribbon.RibbonControl", |
| | | "DevExpress.XtraBars.Navigation.OfficeNavigationBar", |
| | | "DevExpress.XtraBars.Navigation.TileNavPane", |
| | | "DevExpress.XtraBars.TabFormControl", |
| | | "DevExpress.XtraBars.FluentDesignSystem.FluentDesignFormControl", |
| | | "DevExpress.XtraBars.ToolbarForm.ToolbarFormControl"}); |
| | | // |
| | | // dockPanel1 |
| | | // |
| | | dragBox2_Container.Controls.Add(dockPanel1_Container); |
| | | dragBox2_Container.Dock = DevExpress.XtraBars.Docking.DockingStyle.Float; |
| | | dragBox2_Container.FloatLocation = new System.Drawing.Point(691, 455); |
| | | dragBox2_Container.ID = new System.Guid("319a7cf1-f26d-4049-8a96-8f9fbbd13cd2"); |
| | | dragBox2_Container.Location = new System.Drawing.Point(0, 0); |
| | | dragBox2_Container.Name = "dockPanel1"; |
| | | dragBox2_Container.OriginalSize = new System.Drawing.Size(200, 200); |
| | | dragBox2_Container.Size = new System.Drawing.Size(200, 200); |
| | | dragBox2_Container.Text = "dockPanel1"; |
| | | dragBox2_Container.Options.AllowDockBottom = false; |
| | | dragBox2_Container.Options.AllowDockFill = false; |
| | | dragBox2_Container.Options.AllowDockLeft = false; |
| | | dragBox2_Container.Options.AllowDockRight = false; |
| | | dragBox2_Container.Options.AllowDockTop = false; |
| | | dragBox2_Container.Options.ShowMaximizeButton = false; |
| | | dragBox2_Container.Options.ShowMaximizeButton = false; |
| | | this.dockPanel1.Controls.Add(this.dockPanel1_Container); |
| | | this.dockPanel1.Dock = DevExpress.XtraBars.Docking.DockingStyle.Float; |
| | | this.dockPanel1.FloatLocation = new System.Drawing.Point(1617, 547); |
| | | this.dockPanel1.FloatSize = new System.Drawing.Size(306, 459); |
| | | this.dockPanel1.ID = new System.Guid("7148938b-5fd8-4833-9a9f-445a728a11cf"); |
| | | this.dockPanel1.Location = new System.Drawing.Point(0, 0); |
| | | this.dockPanel1.Name = "dockPanel1"; |
| | | this.dockPanel1.OriginalSize = new System.Drawing.Size(200, 200); |
| | | this.dockPanel1.Size = new System.Drawing.Size(306, 459); |
| | | this.dockPanel1.Text = "组件箱"; |
| | | dockPanel1.Options.AllowDockBottom = false; |
| | | dockPanel1.Options.AllowDockFill = false; |
| | | dockPanel1.Options.AllowDockLeft = false; |
| | | dockPanel1.Options.AllowDockRight = false; |
| | | dockPanel1.Options.AllowDockTop = false; |
| | | dockPanel1.Options.ShowMaximizeButton = false; |
| | | dockPanel1.Options.ShowMaximizeButton = false; |
| | | // |
| | | // dockPanel1_Container |
| | | // |
| | | dockPanel1_Container.Location = new System.Drawing.Point(5, 32); |
| | | dockPanel1_Container.Name = "dockPanel1_Container"; |
| | | dockPanel1_Container.Size = new System.Drawing.Size(190, 164); |
| | | dockPanel1_Container.TabIndex = 0; |
| | | this.dockPanel1_Container.Location = new System.Drawing.Point(5, 32); |
| | | this.dockPanel1_Container.Name = "dockPanel1_Container"; |
| | | this.dockPanel1_Container.Size = new System.Drawing.Size(296, 423); |
| | | this.dockPanel1_Container.TabIndex = 0; |
| | | this.dockPanel1_Container.BorderStyle = System.Windows.Forms.BorderStyle.None; |
| | | // |
| | | // XtraForm3 |
| | | // XtraForm1 |
| | | // |
| | | ((System.ComponentModel.ISupportInitialize)(dockManager1)).EndInit(); |
| | | dragBox2_Container.ResumeLayout(false); |
| | | frm.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F); |
| | | frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; |
| | | frm.ClientSize = new System.Drawing.Size(1057, 660); |
| | | |
| | | ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).EndInit(); |
| | | this.dockPanel1.ResumeLayout(false); |
| | | frm.ResumeLayout(false); |
| | | dragBox2_Container.Show(); |
| | | dockManager1.EndUpdate(); |
| | | |
| | | this.dockPanel1.Hide(); |
| | | |
| | | dockPanel1.ClosedPanel += (s, e) => |
| | | { |
| | | foreach (Control control in dragBox1.Controls) |
| | | { |
| | | if (control != null) |
| | | { |
| | | control.MouseDown -= new MouseEventHandler(control_MouseDown); |
| | | control.MouseLeave -= new EventHandler(control_MouseLeave); |
| | | control.MouseMove -= new MouseEventHandler(control_MouseMove); |
| | | control.MouseUp -= Control_MouseUp; |
| | | control.Resize -= myPanel_Resize; |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | #region Public Method |
| | | public static void RegisterControl(DevExpress.XtraEditors.XtraForm frm, Control box1, Label lbMsg) |
| | | public void Show() |
| | | { |
| | | InitializeComponent(frm); |
| | | foreach (Control control in box1.Controls) |
| | | if (dockPanel1.Visibility == DockVisibility.Visible) |
| | | return; |
| | | foreach (Control control in dragBox1.Controls) |
| | | { |
| | | if (control != null) |
| | | { |
| | | sbMsg = lbMsg; |
| | | dragBox1 = box1; |
| | | dragCurrent_Name = box1.Name; |
| | | dragBoxCurrentName = dragBox1.Name; |
| | | control.MouseDown += new MouseEventHandler(control_MouseDown); |
| | | control.MouseLeave += new EventHandler(control_MouseLeave); |
| | | control.MouseMove += new MouseEventHandler(control_MouseMove); |
| | | control.MouseUp += Control_MouseUp; |
| | | |
| | | control.Resize += myPanel_Resize; |
| | | control.ContextMenuStrip = new ContextMenuStrip(); |
| | | |
| | | } |
| | | } |
| | | dockPanel1.Show(); |
| | | isDragging = true; |
| | | |
| | | } |
| | | |
| | | private static void Control_MouseUp(object sender, MouseEventArgs e) |
| | | { |
| | | (sender as Control).Refresh(); |
| | | } |
| | | |
| | | public static void UnRegisterControl(Control control) |
| | | public void Hide(Control control) |
| | | { |
| | | if (control != null) |
| | | { |
| | | control.MouseDown -= new MouseEventHandler(control_MouseDown); |
| | | control.MouseLeave -= new EventHandler(control_MouseLeave); |
| | | control.MouseMove -= new MouseEventHandler(control_MouseMove); |
| | | control.MouseUp -= Control_MouseUp; |
| | | control.Resize -= myPanel_Resize; |
| | | } |
| | | dockPanel1.Hide(); |
| | | isDragging = false; |
| | | } |
| | | #endregion |
| | | private static void control_MouseDown(object sender, MouseEventArgs e) |
| | | #region Public Method |
| | | private void Control_MouseUp(object sender, MouseEventArgs e) |
| | | { |
| | | Control lCtrl = (sender as Control); |
| | | ctrlCurrentObj = lCtrl; |
| | | (sender as Control).Refresh(); |
| | | dragBoxCurrentName = (sender as Control).Parent.Name; |
| | | if (dragBox1.Name == dragBoxCurrentName) |
| | | { |
| | | if (IsOutsideBounds(lCtrl, dragBox1)) |
| | | { |
| | | dragBox1.Controls.Remove(lCtrl); |
| | | dockPanel1_Container.Controls.Add(lCtrl); |
| | | lCtrl.Left = 0; |
| | | lCtrl.Top = 10; |
| | | // isDragging = false; |
| | | dragBoxCurrentName = dockPanel1_Container.Name; |
| | | } |
| | | reloadLoaction(null); |
| | | } |
| | | else if (dockPanel1_Container.Name == dragBoxCurrentName) |
| | | { |
| | | if (IsOutsideBounds(lCtrl, dockPanel1_Container)) |
| | | { |
| | | dockPanel1_Container.Controls.Remove(lCtrl); |
| | | dragBox1.Controls.Add(lCtrl); |
| | | lCtrl.Left = 0; |
| | | lCtrl.Top = 10; |
| | | // isDragging = false; |
| | | dragBoxCurrentName = dragBox1.Name; |
| | | } |
| | | reloadLoaction(lCtrl); |
| | | } |
| | | |
| | | } |
| | | private void control_MouseDown(object sender, MouseEventArgs e) |
| | | { |
| | | p.X = e.X; |
| | | p.Y = e.Y; |
| | | p1.X = e.X; |
| | | p1.Y = e.Y; |
| | | isDragging = true; |
| | | dragCurrent_Name = (sender as Control).Parent.Name; |
| | | dragBoxCurrentName = (sender as Control).Parent.Name; |
| | | ctrlCurrentObj = (sender as Control); |
| | | |
| | | |
| | | if (e.Button == MouseButtons.Right) |
| | | { |
| | | Control control = (sender as Control); |
| | | var menu = control.ContextMenuStrip; |
| | | menu.Items.Clear(); |
| | | SetDrag frm = new SetDrag(); |
| | | menu.Items.Add($"设置坐标", null, (ss, ee) => |
| | | { |
| | | frm.ShowDialog(); |
| | | }); |
| | | menu.Items.Add($"设置大小", null, (ss, ee) => |
| | | { |
| | | frm.ShowDialog(); |
| | | }); |
| | | menu.Items.Add($"高级设置", null, (ss, ee) => |
| | | { |
| | | frm.ShowDialog(); |
| | | }); |
| | | menu.Items.Add($"一键重排", null, (ss, ee) => |
| | | { |
| | | frm.ShowDialog(); |
| | | }); |
| | | menu.Show(control, e.Location); |
| | | } |
| | | |
| | | } |
| | | |
| | | private static void myPanel_Resize(object sender, EventArgs e) |
| | | private void myPanel_Resize(object sender, EventArgs e) |
| | | { |
| | | Control panel = sender as Control; |
| | | if (panel != null && panel.Controls.Count > 0) |
| | |
| | | childControl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); |
| | | int newY = (panel.ClientSize.Height - childControl.Height) / 2; |
| | | childControl.Location = new Point(childControl.Location.X, newY); |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | | reloadMsg(panel); |
| | | } |
| | | private static void control_MouseLeave(object sender, EventArgs e) |
| | | private void control_MouseLeave(object sender, EventArgs e) |
| | | { |
| | | m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; |
| | | Control control = sender as Control; |
| | | control.Cursor = Cursors.Arrow; |
| | | ctrlCurrentObj = control; |
| | | } |
| | | private static void control_MouseMove(object sender, MouseEventArgs e) |
| | | private void control_MouseMove(object sender, MouseEventArgs e) |
| | | { |
| | | if (!isDragging) { return; } |
| | | Control lCtrl = (sender as Control); |
| | | ctrlCurrentObj = lCtrl; |
| | | if (e.Button == MouseButtons.Left) |
| | | { |
| | | switch (m_MousePointPosition) |
| | |
| | | p1.Y = e.Y; //'记录光标拖动的当前点 |
| | | break; |
| | | case EnumMousePointPosition.MouseSizeRight: |
| | | lCtrl.Width = lCtrl.Width + e.X - p1.X; // |
| | | lCtrl.Width = lCtrl.Width + e.X - p1.X; |
| | | lCtrl.Height = lCtrl.Height + e.Y - p1.Y; |
| | | p1.X = e.X; |
| | | p1.Y = e.Y; //'记录光标拖动的当前点 |
| | |
| | | int width = control.Width; |
| | | int height = control.Height; |
| | | Point[] ps = { new Point(0, 0),new Point(width - 1, 0), |
| | | new Point(width - 1, height - 1),new Point(0, height - 1),new Point(0, 0) }; |
| | | new Point(width - 1, height - 1),new Point(0, height - 1),new Point(0, 0) }; |
| | | g.DrawLines(new Pen(Color.LightCoral), ps); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | if (dragBox1.Name == dragCurrent_Name) |
| | | { |
| | | if (IsOutsideBounds(lCtrl, dragBox1)) |
| | | { |
| | | dragBox1.Controls.Remove(lCtrl); |
| | | dockPanel1_Container.Controls.Add(lCtrl); |
| | | lCtrl.Left = 0; |
| | | lCtrl.Top = 10; |
| | | isDragging = false; |
| | | dragCurrent_Name = dockPanel1_Container.Name; |
| | | } |
| | | } |
| | | else if (dockPanel1_Container.Name == dragCurrent_Name) |
| | | { |
| | | if (IsOutsideBounds(lCtrl, dockPanel1_Container)) |
| | | { |
| | | dockPanel1_Container.Controls.Remove(lCtrl); |
| | | dragBox1.Controls.Add(lCtrl); |
| | | lCtrl.Left = 0; |
| | | lCtrl.Top = 10; |
| | | isDragging = false; |
| | | dragCurrent_Name = dragBox1.Name; |
| | | } |
| | | } |
| | | sbMsg.Text = lCtrl.Location.ToString() + "容器:" + dragCurrent_Name; |
| | | |
| | | // reloadLoaction(); |
| | | reloadMsg(lCtrl); |
| | | |
| | | |
| | | } |
| | | private static EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) |
| | | private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) |
| | | { |
| | | if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) |
| | | { |
| | |
| | | return EnumMousePointPosition.MouseSizeNone; |
| | | } |
| | | } |
| | | |
| | | public static bool IsOutsideBounds(Control childControl, Control parentControl) |
| | | public bool IsOutsideBounds(Control childControl, Control parentControl) |
| | | { |
| | | if (parentControl == null) return true; |
| | | if (parentControl == null) return true; |
| | |
| | | // 如果子控件的所有边界都在父控件的边界内,则返回false |
| | | return false; |
| | | } |
| | | #endregion |
| | | |
| | | private void reloadLoaction(Control cocccc) |
| | | { |
| | | int _top = 0; |
| | | if (dockPanel1_Container.Name == dragBoxCurrentName) |
| | | { |
| | | foreach (Control col in dockPanel1_Container.Controls) |
| | | { |
| | | col.Left = 0; |
| | | col.Top = _top; |
| | | _top += (col.Height + 2); |
| | | } |
| | | return; |
| | | } |
| | | if (cocccc == null) return; |
| | | int x = 15; |
| | | int y = 15; |
| | | foreach (Control btn in dragBox1.Controls) |
| | | { |
| | | if (cocccc.Name == btn.Name) |
| | | continue; |
| | | //btn.Left = x; |
| | | //btn.Top = y; |
| | | x += btn.Width + 15; |
| | | if (btn.Width > dragBox1.Width - x) |
| | | { |
| | | x = 15; |
| | | y += btn.Height + 15; |
| | | } |
| | | } |
| | | cocccc.Left = x; |
| | | cocccc.Top = y; |
| | | return; |
| | | } |
| | | |
| | | private void reloadMsg(Control cocccc) |
| | | { |
| | | System.Text.StringBuilder sb = new System.Text.StringBuilder(); |
| | | if (ctrlCurrentObj != null) |
| | | sb.Append(ctrlCurrentObj.Name + "在"); |
| | | sb.Append(string.IsNullOrEmpty(dragBoxCurrentName) ? "表单中" : "组件箱中"); |
| | | sb.Append(",坐标:" + cocccc.Location.ToString()); |
| | | sb.Append(",大小:" + cocccc.Size.ToString()); |
| | | toolTip.SetToolTip(ctrlCurrentObj, sb.ToString()); |
| | | } |
| | | } |
| | | } |