标签:
独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体,在无边框窗体及其友好的实现拖拽移动窗体
only原创首发,vonly.net
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Configuration; 5 using System.Diagnostics; 6 using System.Text; 7 using System.Threading; 8 using System.Windows.Forms; 9 10 namespace only.Controls 11 { 12 public partial class DragDrop : Component 13 { 14 15 private bool _enable = false; 16 17 [Browsable(true)] 18 [Description("打开拖拽功能"), Category("设置")] 19 public bool Enable 20 { 21 get 22 { 23 return _enable; 24 } 25 set 26 { 27 if (value != _enable) 28 { 29 _enable = value; 30 // Start(value); 31 new Thread(() => 32 { 33 Thread.Sleep(100); 34 Start(_enable); 35 }).Start(); 36 37 } 38 } 39 } 40 41 [Browsable(true)] 42 [Description("拖拽移动目标"), Category("设置")] 43 public Control TargetControl { get; set; } 44 [Browsable(true)] 45 [Description("拖拽事件源"), Category("设置")] 46 public Control SourceControl { get; set; } 47 48 49 public void Start(bool enable = true) 50 { 51 if (SourceControl != null & TargetControl != null) 52 { 53 if (enable) 54 { 55 SourceControl.MouseDown += SourceControlOnMouseDown; 56 SourceControl.MouseMove += SourceControlOnMouseMove; 57 SourceControl.MouseUp += SourceControlOnMouseUp; 58 } 59 else 60 { 61 SourceControl.MouseDown -= SourceControlOnMouseDown; 62 SourceControl.MouseMove -= SourceControlOnMouseMove; 63 SourceControl.MouseUp -= SourceControlOnMouseUp; 64 } 65 } 66 } 67 68 private bool _mouseDown = false; 69 private int _startX = 0; 70 private int _startY = 0; 71 72 private void SourceControlOnMouseUp(object sender, MouseEventArgs e) 73 { 74 _mouseDown = false; 75 } 76 77 private void SourceControlOnMouseMove(object sender, MouseEventArgs e) 78 { 79 if (_mouseDown & _enable) 80 { 81 TargetControl.Left += (e.X - _startX); 82 TargetControl.Top += (e.Y - _startY); 83 } 84 } 85 86 private void SourceControlOnMouseDown(object sender, MouseEventArgs e) 87 { 88 if (_mouseDown == false) 89 { 90 _mouseDown = true; 91 _startX = e.X; 92 _startY = e.Y; 93 } 94 } 95 } 96 }
标签:
原文地址:http://www.cnblogs.com/vonly/p/4597855.html