lu
2025-03-23 9096edb026d5d411fddeeb4281b24d67160567d2
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using DevExpress.XtraBars.Docking;
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Gs.DevApp.ToolBox
{
    public class DragResizeHelp
    {
        #region Field
        private bool isDragging = false;
        private const int Band = 5;
        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
        {
            MouseSizeNone = 0, //'无
            MouseSizeRight = 1, //'拉伸右边框
            MouseSizeLeft = 2, //'拉伸左边框
            MouseSizeBottom = 3, //'拉伸下边框
            MouseSizeTop = 4, //'拉伸上边框
            MouseSizeTopLeft = 5, //'拉伸左上角
            MouseSizeTopRight = 6, //'拉伸右上角
            MouseSizeBottomLeft = 7, //'拉伸左下角
            MouseSizeBottomRight = 8, //'拉伸右下角
            MouseDrag = 9   // '鼠标拖动
        }
        #endregion
        public DragResizeHelp(DevExpress.XtraEditors.XtraForm frm, Control regBox)
        {
          //  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
            // 
            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
            // 
            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
            // 
            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;
            // 
            // XtraForm1
            // 
            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);
            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;
                    }
                }
            };
        }
        public void Show()
        {
            if (dockPanel1.Visibility == DockVisibility.Visible)
                return;
            foreach (Control control in dragBox1.Controls)
            {
                if (control != null)
                {
                    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;
           
        }
        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;
        }
        #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;
            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) =>
                {
                    reloadLoaction((sender as Control).Parent);
                });
                menu.Show(control, e.Location);
            }
 
        }
        private void myPanel_Resize(object sender, EventArgs e)
        {
            Control panel = sender as Control;
            if (panel != null && panel.Controls.Count > 0)
            {
                foreach (Control childControl in panel.Controls)
                {
                    // childControl.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                    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 void control_MouseLeave(object sender, EventArgs e)
        {
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            Control control = sender as Control;
            control.Cursor = Cursors.Arrow;
            ctrlCurrentObj = control;
        }
        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)
                {
                    case EnumMousePointPosition.MouseDrag:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Width = lCtrl.Width + (e.X - p1.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        break;
                    default:
                        break;
                }
                if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
                if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
            }
            else
            {
                m_MousePointPosition = MousePointPosition(lCtrl.Size, e);   //'判断光标的位置状态
                Control control = sender as Control;
                switch (m_MousePointPosition)   //'改变光标
                {
                    case EnumMousePointPosition.MouseSizeNone:
                        control.Cursor = Cursors.Arrow;
                        //'箭头
                        break;
                    case EnumMousePointPosition.MouseDrag:
                        control.Cursor = Cursors.SizeAll;      //'四方向
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        control.Cursor = Cursors.SizeNS;       //'南北
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        control.Cursor = Cursors.SizeNS;       //'南北
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        control.Cursor = Cursors.SizeWE;       //'东西
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        control.Cursor = Cursors.SizeWE;       //'东西
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        control.Cursor = Cursors.SizeNESW;     //'东北到南西
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        control.Cursor = Cursors.SizeNWSE;     //'东南到西北
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        control.Cursor = Cursors.SizeNWSE;     //'东南到西北
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        control.Cursor = Cursors.SizeNESW;     //'东北到南西
                        break;
                    default:
                        break;
                }
 
                control.Refresh();
                Graphics g = control.CreateGraphics();
                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) };
                g.DrawLines(new Pen(Color.Pink), ps);
            }
            //  reloadLoaction();
            reloadMsg(lCtrl);
 
           
        }
        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))
            {
                if (e.X < Band)
                {
                    if (e.Y < Band)
                    {
                        return EnumMousePointPosition.MouseSizeTopLeft;
                    }
                    else
                    {
                        if (e.Y > -1 * Band + size.Height)
                        {
                            return EnumMousePointPosition.MouseSizeBottomLeft;
                        }
                        else
                        {
                            return EnumMousePointPosition.MouseSizeLeft;
                        }
                    }
                }
                else
                {
                    if (e.X > -1 * Band + size.Width)
                    {
                        if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTopRight;
                        }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottomRight;
                            }
                            else
                            {
                                return EnumMousePointPosition.MouseSizeRight;
                            }
                        }
                    }
                    else
                    {
                        if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTop;
                        }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottom;
                            }
                            else
                            {
                                return EnumMousePointPosition.MouseDrag;
                            }
                        }
                    }
                }
            }
            else
            {
                return EnumMousePointPosition.MouseSizeNone;
            }
        }
        public bool IsOutsideBounds(Control childControl, Control parentControl)
        {
            if (parentControl == null) return true;
            if (parentControl == null) return true;
            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;
        }
        #endregion
 
        private void reloadLoaction(Control cocccc)
        {
            int _top = 0;
            //如果是控件箱,不移Left
            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 = 2;
            int y = 2;
            foreach (Control btn in dragBox1.Controls)
            {
                if (cocccc.Name == btn.Name)
                    continue;
                if (cocccc.Visible ==false)
                    continue;
                btn.Left = x;
                btn.Top = y;
                x += btn.Width + 2;
                if (btn.Width > dragBox1.Width - x)
                {
                    x = 2;
                    y += btn.Height +2;
                }
            }
            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());
        }
    }
}