[C#] 一个类实现拖拽调整窗体或控件大小

最近闲来无事, 倒是借助WebAPI实现翻译器, 本想设计一个炫酷的界面(模仿VS), 却没想到, 难度大大超出我的想象, 拖拽, 调整大小, 如果要实现VS的边框, 还需要想办法做到过渡透明! 这对于WinForm来说实在是太难了, 如果不过渡透明, 就是全透明, 那鼠标就直接穿窗体了!不过还是有些成果的, 比如, 造了两个轮子(我真是一个热衷于造轮子的傻子)所说的轮子就是文章标题咯, 因为之前我还做了一个类来实现拖拽移动控件或窗体嘛, 所以我就直接把这个调整大小的跟之前的功能整合到了..

在这里插入图片描述

  1. 最近闲来无事, 倒是借助WebAPI实现翻译器, 本想设计一个炫酷的界面(模仿VS), 却没想到, 难度大大超出我的想象, 拖拽, 调整大小, 如果要实现VS的边框, 还需要想办法做到过渡透明! 这对于WinForm来说实在是太难了, 如果不过渡透明, 就是全透明, 那鼠标就直接穿窗体了!
  2. 不过还是有些成果的, 比如, 造了两个轮子

(我真是一个热衷于造轮子的傻子)

  1. 所说的轮子就是文章标题咯, 因为之前我还做了一个类来实现拖拽移动控件或窗体嘛, 所以我就直接把这个调整大小的跟之前的功能整合到了一起. (可以提到的是, 期间因为特殊需求, 还做了一个映射拖拽移动控件窗体, 就是拖拽控件1, 移动控件2)

  2. 下面的代码就是程序主体了, 它是应该被封装到一个类库里的, 所以, 不要直接把这些代码添加到你的文件中, 而是新建一个类库(不是新建项目, 而是添加到你的项目中), 然后把这些代码覆盖到新的文件中, 在你的程序中using CHO.DragOperation即可使用代码所包含的3个功能( DragMove,MapDragMove,DragResize )

  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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CHO
{
    namespace DragOperation
    {
        public class DragMove
        {
            public DragMove(MouseButtons MouseButton, DragMoveMode FollowMode = DragMoveMode.All, bool OutParent = false)
            {
                this.MouseButton = MouseButton;
                this.FollowMode = FollowMode;
                this.OutParent = OutParent;
            }
            public DragMove(DragMoveMode FollowMode = DragMoveMode.All, bool OutParent = false)
            {
                AllMouseButton = true;
                this.FollowMode = FollowMode;
                this.OutParent = OutParent;
            }

            object AboutControl;                                  // 表示当前移动控件操作是针对哪个控件的
            bool Moving = false;                                  // 表示是否正在移动控件
            bool AllMouseButton = false;                          // 表示是否处理所有按钮
            bool OutParent;                                       // 表示是否允许脱离父容器的显示区域
            Point ControlStartLocation;                           // 表示移动操作时, 控件的初始位置
            Point MouseFirstLocationAboutParent;                  // 表示移动操作时, 鼠标的初始位置
            DragMoveMode FollowMode;                              // 跟随模式
            MouseButtons MouseButton;                             // 判定按钮
            List<Control> AddedControls = new List<Control>();    // 已添加控件


            void MouseDownEvent(object sender, MouseEventArgs e)
            {
                if ((e.Button == MouseButton) || AllMouseButton)
                {
                    AboutControl = sender;
                    Moving = true;
                    ControlStartLocation = (sender as Control).Location;
                    if (sender is Form)
                    {
                        MouseFirstLocationAboutParent = Control.MousePosition;
                    }
                    else
                    {
                        MouseFirstLocationAboutParent = (sender as Control).Parent.PointToClient(Control.MousePosition);
                    }
                }
            }
            void MouseMoveEvent(object sender, MouseEventArgs e)
            {
                if (sender == AboutControl)
                {
                    if (Moving)
                    {
                        Point MouseLocationNow;
                        if (sender is Form)
                        {
                            MouseLocationNow = Control.MousePosition;
                        }
                        else
                        {
                            MouseLocationNow = (sender as Control).Parent.PointToClient(Control.MousePosition);
                        }
                        int NewX = MouseLocationNow.X - MouseFirstLocationAboutParent.X + ControlStartLocation.X;
                        int NewY = MouseLocationNow.Y - MouseFirstLocationAboutParent.Y + ControlStartLocation.Y;
                        if (!OutParent)
                        {
                            if (!(sender is Form))
                            {
                                if (NewX < 0)
                                {
                                    NewX = 0;
                                }
                                else if (NewX + (sender as Control).Width > (sender as Control).Parent.Width)
                                {
                                    NewX = (sender as Control).Parent.Width - (sender as Control).Width;
                                }
                                if (NewY < 0)
                                {
                                    NewY = 0;
                                }
                                else if (NewY + (sender as Control).Height > (sender as Control).Parent.Height)
                                {
                                    NewY = (sender as Control).Parent.Height - (sender as Control).Height;
                                }
                            }
                        }
                        if (FollowMode == DragMoveMode.X)
                        {
                            NewY = ControlStartLocation.Y;
                        }
                        if (FollowMode == DragMoveMode.Y)
                        {
                            NewX = ControlStartLocation.X;
                        }
                        (sender as Control).Location = new Point(NewX, NewY);
                    }
                }
                else
                {
                    if (Moving)
                    {
                        Moving = false;
                        (AboutControl as Control).Location = ControlStartLocation;
                    }
                }
            }
            void MouseUpEvent(object sender, MouseEventArgs e)
            {
                if (sender != AboutControl)
                {
                    if (Moving)
                    {
                        (AboutControl as Control).Location = ControlStartLocation;
                    }
                }
                Moving = false;
            }
            public bool AddControl(Control control)
            {
                if (AddedControls.Contains(control))
                {
                    return false;
                }
                else
                {
                    control.MouseDown += MouseDownEvent;
                    control.MouseMove += MouseMoveEvent;
                    control.MouseUp += MouseUpEvent;
                    AddedControls.Add(control);
                    return true;
                }
            }
            public bool RemoveControl(Control control)
            {
                if (AddedControls.Contains(control))
                {
                    control.MouseDown -= MouseDownEvent;
                    control.MouseMove -= MouseMoveEvent;
                    control.MouseUp -= MouseUpEvent;
                    AddedControls.Remove(control);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public class MapDragMove
        {
            public MapDragMove(MouseButtons MouseButton, DragMoveMode FollowMode = DragMoveMode.All)
            {
                this.MouseButton = MouseButton;
                this.FollowMode = FollowMode;
            }
            public MapDragMove(DragMoveMode FollowMode = DragMoveMode.All)
            {
                AllMouseButton = true;
                this.FollowMode = FollowMode;
            }

            object AboutControl;                                  // 表示当前移动控件操作是针对哪个控件的
            bool Moving = false;                                  // 表示是否正在移动控件
            bool AllMouseButton = false;                          // 表示是否处理所有按钮
            Point ControlStartLocation;                           // 表示移动操作时, 控件的初始位置
            Point MouseFirstLocation;                             // 表示移动操作时, 鼠标的初始位置
            DragMoveMode FollowMode;                              // 跟随模式
            MouseButtons MouseButton;                             // 判定按钮
            Dictionary<Control, Control> AddedControls = new Dictionary<Control, Control>();    // 已添加控件


            void MouseDownEvent(object sender, MouseEventArgs e)
            {
                if ((e.Button == MouseButton) || AllMouseButton)
                {
                    AboutControl = sender;
                    Moving = true;
                    ControlStartLocation = (sender as Control).Location;
                    MouseFirstLocation = Control.MousePosition;
                }
            }
            void MouseMoveEvent(object sender, MouseEventArgs e)
            {
                if (sender == AboutControl)
                {
                    if (Moving)
                    {
                        Point MouseLocationNow = Control.MousePosition;

                        if (FollowMode == DragMoveMode.X)
                        {
                            AddedControls[sender as Control].Left += MouseLocationNow.X - MouseFirstLocation.X;
                        }
                        else if (FollowMode == DragMoveMode.Y)
                        {
                            AddedControls[sender as Control].Top += MouseLocationNow.Y - MouseFirstLocation.Y;
                        }
                        else
                        {
                            AddedControls[sender as Control].Location = new Point(AddedControls[sender as Control].Location.X + (MouseLocationNow.X - MouseFirstLocation.X), AddedControls[sender as Control].Location.Y + (MouseLocationNow.Y - MouseFirstLocation.Y));
                        }

                        MouseFirstLocation = MouseLocationNow;
                    }
                }
                else
                {
                    if (Moving)
                    {
                        Moving = false;
                        (AboutControl as Control).Location = ControlStartLocation;
                    }
                }
            }
            void MouseUpEvent(object sender, MouseEventArgs e)
            {
                if (sender != AboutControl)
                {
                    if (Moving)
                    {
                        (AboutControl as Control).Location = ControlStartLocation;
                    }
                }
                Moving = false;
            }
            public bool AddControl(Control dragControl, Control moveControl)
            {
                if (AddedControls.ContainsKey(dragControl))
                {
                    return false;
                }
                else
                {
                    dragControl.MouseDown += MouseDownEvent;
                    dragControl.MouseMove += MouseMoveEvent;
                    dragControl.MouseUp += MouseUpEvent;
                    AddedControls.Add(dragControl, moveControl);
                    return true;
                }
            }
            public bool RemoveControl(Control control)
            {
                if (AddedControls.ContainsKey(control))
                {
                    control.MouseDown -= MouseDownEvent;
                    control.MouseMove -= MouseMoveEvent;
                    control.MouseUp -= MouseUpEvent;
                    AddedControls.Remove(control);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 控件跟随鼠标的移动模式
        /// </summary>
        public enum DragMoveMode
        {
            /// <summary>
            /// 表示只跟随横坐标
            /// </summary>
            X,
            /// <summary>
            /// 表示只跟随纵坐标
            /// </summary>
            Y,
            /// <summary>
            /// 跟随横坐标与纵坐标
            /// </summary>
            All
        }

        public class DragResize
        {
            public DragResize(MouseButtons MouseButton, int BorderSize = 5)
            {
                this.MouseButton = MouseButton;
                this.BorderSize = BorderSize;
            }
            public DragResize(int BorderSize = 5)
            {
                this.AllMouseButton = true;
                this.BorderSize = BorderSize;
            }

            int BorderSize = 10;                                  // 边框判断大小
            object AboutControl;                                  // 表示当前调整控件操作是针对哪个控件的
            bool Resizing = false;                                // 表示是否正在调整控件
            bool AllMouseButton = false;                          // 表示是否处理所有按钮

            bool MouseDownING = false;

            bool LeftResize;
            bool RightResize;
            bool TopResize;
            bool BottomResize;
            private Point MouseFirstLocation;                     // 表示调整操作时, 鼠标的初始位置
            MouseButtons MouseButton;                             // 判定按钮
            List<Control> AddedControls = new List<Control>();    // 已添加控件

            void CheckMouse(object sender)
            {
                Point mousePosition;
                if (sender is Form)
                {
                    mousePosition = Control.MousePosition;
                }
                else
                {
                    mousePosition = (sender as Control).Parent.PointToClient(Control.MousePosition);
                }

                if (mousePosition.X >= (sender as Form).Left && mousePosition.X <= (sender as Form).Left + BorderSize)
                {
                    LeftResize = true;
                }
                else if (mousePosition.X >= (sender as Form).Left + (sender as Form).Width - BorderSize && mousePosition.X <= (sender as Form).Left + (sender as Form).Width)
                {
                    RightResize = true;
                }
                else
                {
                    LeftResize = false;
                    RightResize = false;
                }
                if (mousePosition.Y <= (sender as Form).Top + BorderSize && mousePosition.Y >= (sender as Form).Top)
                {
                    TopResize = true;
                }
                else if (mousePosition.Y >= (sender as Form).Top + (sender as Form).Height - BorderSize && mousePosition.Y <= (sender as Form).Top + (sender as Form).Height)
                {
                    BottomResize = true;
                }
                else
                {
                    TopResize = false;
                    BottomResize = false;
                }

                if ((LeftResize || RightResize || TopResize || BottomResize) && MouseDownING)
                {
                    Resizing = true;
                    AboutControl = sender;
                }
                else
                {
                    Resizing = false;
                }
            }

            void MouseDownEvent(object sender, MouseEventArgs e)
            {
                if (AllMouseButton || e.Button == MouseButton)
                {
                    MouseDownING = true;
                    CheckMouse(sender);

                    if (Resizing)
                    {
                        MouseFirstLocation = Control.MousePosition;
                    }
                }
            }
            void MouseMoveEvent(object sender, MouseEventArgs e)
            {
                if (Resizing && sender == AboutControl)
                {
                    Point mousePosition = Control.MousePosition;
                    (sender as Control).Invalidate(false);
                    if (LeftResize)
                    {
                        (sender as Control).Width += MouseFirstLocation.X - mousePosition.X;
                        (sender as Control).Left -= MouseFirstLocation.X - mousePosition.X;
                    }
                    else if (RightResize)
                    {
                        (sender as Control).Width += mousePosition.X - MouseFirstLocation.X;
                    }
                    if (TopResize)
                    {
                        (sender as Control).Height += MouseFirstLocation.Y - mousePosition.Y;
                        (sender as Control).Top -= MouseFirstLocation.Y - mousePosition.Y;
                    }
                    else if (BottomResize)
                    {
                        (sender as Control).Height += mousePosition.Y - MouseFirstLocation.Y;
                    }
                    (sender as Control).Invalidate(true);
                    MouseFirstLocation = mousePosition;
                }
                else
                {
                    CheckMouse(sender);
                    if (TopResize)
                    {
                        if (LeftResize)
                        {
                            (sender as Control).Cursor = Cursors.SizeNWSE;
                        }
                        else if (RightResize)
                        {
                            (sender as Control).Cursor = Cursors.SizeNESW;
                        }
                        else
                        {
                            (sender as Control).Cursor = Cursors.SizeNS;
                        }
                    }
                    else if (BottomResize)
                    {
                        if (LeftResize)
                        {
                            (sender as Control).Cursor = Cursors.SizeNESW;
                        }
                        else if (RightResize)
                        {
                            (sender as Control).Cursor = Cursors.SizeNWSE;
                        }
                        else
                        {
                            (sender as Control).Cursor = Cursors.SizeNS;
                        }
                    }
                    else if (LeftResize)
                    {
                        (sender as Control).Cursor = Cursors.SizeWE;
                    }
                    else if (RightResize)
                    {
                        (sender as Control).Cursor = Cursors.SizeWE;
                    }
                    else
                    {
                        (sender as Control).Cursor = Cursors.Default;
                    }

                }
            }
            void MouseUpEvent(object sender, MouseEventArgs e)
            {
                MouseDownING = false;
                if (Resizing)
                {
                    Resizing = false;
                }
            }
            void MouseLeaveEvent(object sender, EventArgs e)
            {
                (sender as Control).Cursor = Cursors.Default;
            }

            public bool AddControl(Control control)
            {
                if (AddedControls.Contains(control))
                {
                    return false;
                }
                else
                {
                    control.MouseLeave += MouseLeaveEvent;
                    control.MouseDown += MouseDownEvent;
                    control.MouseMove += MouseMoveEvent;
                    control.MouseUp += MouseUpEvent;
                    AddedControls.Add(control);
                    return true;
                }
            }
            public bool RemoveControl(Control control)
            {
                if (AddedControls.Contains(control))
                {
                    control.MouseDown -= MouseDownEvent;
                    control.MouseMove -= MouseMoveEvent;
                    control.MouseUp -= MouseUpEvent;
                    AddedControls.Remove(control);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计