SHARE
TWEET

TabPageDragDrop

a guest Aug 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. public class MyToolWindow : Form
  5. {
  6.     public MyToolWindow()
  7.     {
  8.         FormBorderStyle = FormBorderStyle.SizableToolWindow;
  9.     }
  10. }
  11. public class MyTabControl : TabControl
  12. {
  13.     private static MyTabControl DropTarget { get; set; }
  14.     public MyTabControl()
  15.     {
  16.         this.AllowDrop = true;
  17.     }
  18.     Point p1;
  19.     int d = 16;
  20.     protected override void OnMouseDown(MouseEventArgs e)
  21.     {
  22.         base.OnMouseDown(e);
  23.         p1 = e.Location;
  24.     }
  25.     protected override void OnMouseMove(MouseEventArgs e)
  26.     {
  27.         base.OnMouseMove(e);
  28.         if (e.Button == MouseButtons.Left && this.SelectedTab != null)
  29.         {
  30.             var p2 = e.Location;
  31.             if ((p1.X - p2.X) * (p1.X - p2.X) + ((p1.Y - p2.Y)) * (p1.Y - p2.Y) > d * d)
  32.             {
  33.                 this.DoDragDrop(this.SelectedTab, DragDropEffects.All);
  34.             }
  35.         }
  36.     }
  37.     protected override void OnDragEnter(DragEventArgs e)
  38.     {
  39.         base.OnDragEnter(e);
  40.         if (e.Data.GetDataPresent(typeof(TabPage)))
  41.         {
  42.             e.Effect = DragDropEffects.Move;
  43.             DropTarget = this;
  44.         }
  45.     }
  46.     protected override void OnDragLeave(EventArgs e)
  47.     {
  48.         base.OnDragLeave(e);
  49.         DropTarget = null;
  50.     }
  51.     protected override void OnDragOver(DragEventArgs e)
  52.     {
  53.         base.OnDragOver(e);
  54.         if (e.Data.GetDataPresent(typeof(TabPage)))
  55.         {
  56.             e.Effect = DragDropEffects.Move;
  57.             DropTarget = this;
  58.         }
  59.     }
  60.     protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
  61.     {
  62.         base.OnGiveFeedback(e);
  63.         if (DropTarget == null)
  64.         {
  65.             e.UseDefaultCursors = false;
  66.             Cursor.Current = Cursors.SizeAll;
  67.         }
  68.     }
  69.     protected override void OnQueryContinueDrag(QueryContinueDragEventArgs e)
  70.     {
  71.         base.OnQueryContinueDrag(e);
  72.         if (e.Action == DragAction.Drop && DropTarget == null)
  73.         {
  74.             var tabPage = this.SelectedTab;
  75.             if (!this.RectangleToScreen(this.Bounds).Contains(Cursor.Position))
  76.             {
  77.                 var form = new MyToolWindow();
  78.                 form.Text = tabPage.Text;
  79.                 var tabControl = new MyTabControl();
  80.                 DropTarget = tabControl;
  81.                 tabControl.Dock = DockStyle.Fill;
  82.                 form.Controls.Add(tabControl);
  83.                 form.StartPosition = FormStartPosition.Manual;
  84.                 form.Location = new Point(Cursor.Position.X - 16,
  85.                     Cursor.Position.Y - SystemInformation.ToolWindowCaptionHeight - 16);
  86.                 form.Show();
  87.                 e.Action = DragAction.Continue;
  88.             }
  89.         }
  90.     }
  91.     protected override void OnDragDrop(DragEventArgs e)
  92.     {
  93.         base.OnDragDrop(e);
  94.         if (e.Data.GetDataPresent(typeof(TabPage)))
  95.         {
  96.             var tabPage = (TabPage)e.Data.GetData(typeof(TabPage));
  97.             var f1 = tabPage.FindForm();
  98.             var t1 = tabPage.Parent as MyTabControl;
  99.             this.TabPages.Remove(tabPage);
  100.             this.TabPages.Add(tabPage);
  101.             var f2 = tabPage.FindForm();
  102.             f2.Activate();
  103.             if (f1 is MyToolWindow && f1 != f2 && t1 != null && t1.TabCount == 0)
  104.                 f1.Close();
  105.         }
  106.     }
  107.     private const int WM_NCHITTEST = 0x84;
  108.     private const int HTTRANSPARENT = -1;
  109.     private const int HTCLIENT = 1;
  110.     protected override void WndProc(ref Message m)
  111.     {
  112.         base.WndProc(ref m);
  113.         if (m.Msg == WM_NCHITTEST)
  114.         {
  115.             if (m.Result.ToInt32() == HTTRANSPARENT)
  116.                 m.Result = new IntPtr(HTCLIENT);
  117.         }
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top