感谢 xiashengwang 提供。
原文地址:http://www.cnblogs.com/xiashengwang/p/4024505.html#3077216
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Windows.Forms; 5 using System.Drawing; 6 7 namespace WindowsFormsApplication1 8 { 9 public class DragableTabControl : TabControl 10 { 11 public DragableTabControl() 12 { 13 } 14 15 protected override void OnMouseDown(MouseEventArgs e) 16 { 17 base.OnMouseDown(e); 18 TabPage tabPage = GetTabPageByTab(new Point(e.X, e.Y)); 19 if (tabPage != null) 20 { 21 this.DoDragDrop(tabPage, DragDropEffects.All); 22 } 23 } 24 25 private TabPage GetTabPageByTab(Point point) 26 { 27 for (int i = 0; i < this.TabPages.Count; i++) 28 { 29 if (GetTabRect(i).Contains(point)) 30 { 31 return this.TabPages[i]; 32 } 33 } 34 return null; 35 } 36 37 protected override void OnDragOver(DragEventArgs e) 38 { 39 base.OnDragOver(e); 40 TabPage source = (TabPage)e.Data.GetData(typeof(TabPage)); 41 if (source != null) 42 { 43 TabPage target = GetTabPageByTab(PointToClient(new Point(e.X, e.Y))); 44 if (target != null) 45 { 46 e.Effect = DragDropEffects.Move; 47 MoveTabPage(source, target); 48 } 49 else 50 { 51 e.Effect = DragDropEffects.None; 52 } 53 } 54 else 55 { 56 e.Effect = DragDropEffects.None; 57 } 58 } 59 60 private void MoveTabPage(TabPage source, TabPage target) 61 { 62 if (source == target) 63 return; 64 65 int targetIndex = -1; 66 ListlstPages = new List (); 67 for (int i = 0; i < this.TabPages.Count; i++) 68 { 69 if (this.TabPages[i] == target) 70 { 71 targetIndex = i; 72 } 73 if (this.TabPages[i] != source) 74 { 75 lstPages.Add(this.TabPages[i]); 76 } 77 } 78 this.TabPages.Clear(); 79 this.TabPages.AddRange(lstPages.ToArray()); 80 this.TabPages.Insert(targetIndex, source); 81 this.SelectedTab = source; 82 } 83 } 84 }