Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- public class MyToolWindow : Form
- {
- public MyToolWindow()
- {
- FormBorderStyle = FormBorderStyle.SizableToolWindow;
- }
- }
- public class MyTabControl : TabControl
- {
- private static MyTabControl DropTarget { get; set; }
- public MyTabControl()
- {
- this.AllowDrop = true;
- }
- Point p1;
- int d = 16;
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- p1 = e.Location;
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- if (e.Button == MouseButtons.Left && this.SelectedTab != null)
- {
- var p2 = e.Location;
- if ((p1.X - p2.X) * (p1.X - p2.X) + ((p1.Y - p2.Y)) * (p1.Y - p2.Y) > d * d)
- {
- this.DoDragDrop(this.SelectedTab, DragDropEffects.All);
- }
- }
- }
- protected override void OnDragEnter(DragEventArgs e)
- {
- base.OnDragEnter(e);
- if (e.Data.GetDataPresent(typeof(TabPage)))
- {
- e.Effect = DragDropEffects.Move;
- DropTarget = this;
- }
- }
- protected override void OnDragLeave(EventArgs e)
- {
- base.OnDragLeave(e);
- DropTarget = null;
- }
- protected override void OnDragOver(DragEventArgs e)
- {
- base.OnDragOver(e);
- if (e.Data.GetDataPresent(typeof(TabPage)))
- {
- e.Effect = DragDropEffects.Move;
- DropTarget = this;
- }
- }
- protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
- {
- base.OnGiveFeedback(e);
- if (DropTarget == null)
- {
- e.UseDefaultCursors = false;
- Cursor.Current = Cursors.SizeAll;
- }
- }
- protected override void OnQueryContinueDrag(QueryContinueDragEventArgs e)
- {
- base.OnQueryContinueDrag(e);
- if (e.Action == DragAction.Drop && DropTarget == null)
- {
- var tabPage = this.SelectedTab;
- if (!this.RectangleToScreen(this.Bounds).Contains(Cursor.Position))
- {
- var form = new MyToolWindow();
- form.Text = tabPage.Text;
- var tabControl = new MyTabControl();
- DropTarget = tabControl;
- tabControl.Dock = DockStyle.Fill;
- form.Controls.Add(tabControl);
- form.StartPosition = FormStartPosition.Manual;
- form.Location = new Point(Cursor.Position.X - 16,
- Cursor.Position.Y - SystemInformation.ToolWindowCaptionHeight - 16);
- form.Show();
- e.Action = DragAction.Continue;
- }
- }
- }
- protected override void OnDragDrop(DragEventArgs e)
- {
- base.OnDragDrop(e);
- if (e.Data.GetDataPresent(typeof(TabPage)))
- {
- var tabPage = (TabPage)e.Data.GetData(typeof(TabPage));
- var f1 = tabPage.FindForm();
- var t1 = tabPage.Parent as MyTabControl;
- this.TabPages.Remove(tabPage);
- this.TabPages.Add(tabPage);
- var f2 = tabPage.FindForm();
- f2.Activate();
- if (f1 is MyToolWindow && f1 != f2 && t1 != null && t1.TabCount == 0)
- f1.Close();
- }
- }
- private const int WM_NCHITTEST = 0x84;
- private const int HTTRANSPARENT = -1;
- private const int HTCLIENT = 1;
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m);
- if (m.Msg == WM_NCHITTEST)
- {
- if (m.Result.ToInt32() == HTTRANSPARENT)
- m.Result = new IntPtr(HTCLIENT);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement