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
| public partial class MyTabControl : TabControl
{
public override bool AllowDrop
{
get { return true; }
set { }
}
private bool dragging = false;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (!dragging)
{
if (e.Button == MouseButtons.Left)
{
int idx = GetTabIndexAtPosition(e.X, e.Y);
if (idx >= 0)
{
dragging = true;
this.DoDragDrop(idx, DragDropEffects.Move);
return;
}
}
}
dragging = false;
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (e.Data.GetDataPresent(typeof(int)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);
int idxOld = -1;
if (e.Data.GetDataPresent(typeof(int)))
idxOld = (int)e.Data.GetData(typeof(int));
else
return;
Point pos = this.PointToClient(new Point(e.X, e.Y));
int idxNew = GetNewTabIndexForPosition(pos.X, pos.Y);
if (idxNew < 0)
idxNew = this.TabCount;
if (idxNew == idxOld)
return;
TabPage tp = this.TabPages[idxOld];
this.TabPages.Remove(tp);
if (idxOld < idxNew)
idxNew--;
this.TabPages.Insert(idxNew, tp);
this.SelectedTab = tp;
}
private int GetTabIndexAtPosition(int x, int y)
{
for (int i = 0; i < this.TabCount; i++)
{
Rectangle rect = this.GetTabRect(i);
if (rect.Contains(x, y))
{
return i;
}
}
return -1;
}
private int GetNewTabIndexForPosition(int x, int y)
{
int idx = GetTabIndexAtPosition(x, y);
Rectangle rect = this.GetTabRect(idx);
if (x < rect.Left + rect.Width / 2)
return idx;
else
return idx + 1;
}
} |
Partager