9

是否可以在运行时重新排序 WinForms 中的选项卡,TabControl如 IE 或 Firefox?

像这样的链接不会给我太多希望。

4

4 回答 4

10

当然,这是可能的!您很可能试图使解决方案过于复杂。本质上,您所要做的就是对标准进行子类化TabControl并向鼠标事件处理程序添加一些逻辑。您只需要检查用户当前正在拖动哪个表单并在TabPages集合中重新排序。

网上有几个完整的解决方案:

于 2010-12-04T09:42:22.090 回答
7

我发现 @Cody Gray 最初发布的解决方案主要是我想要的,但我没有看到它需要如此复杂。

这是我的简化,通过从 TabControl 派生实现:

public class DraggableTabControl : TabControl
{
    private TabPage m_DraggedTab;

    public DraggableTabControl()
    {
        MouseDown += OnMouseDown;
        MouseMove += OnMouseMove;
    }

    private void OnMouseDown(object sender, MouseEventArgs e)
    {
        m_DraggedTab = TabAt(e.Location);
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left || m_DraggedTab == null)
        {
            return;
        }

        TabPage tab = TabAt(e.Location);

        if (tab == null || tab == m_DraggedTab)
        {
            return;
        }

        Swap(m_DraggedTab, tab);
        SelectedTab = m_DraggedTab;
    }

    private TabPage TabAt(Point position)
    {
        int count = TabCount;

        for (int i = 0; i < count; i++)
        {
            if (GetTabRect(i).Contains(position))
            {
                return TabPages[i];
            }
        }

        return null;
    }

    private void Swap(TabPage a, TabPage b)
    {
        int i = TabPages.IndexOf(a);
        int j = TabPages.IndexOf(b);
        TabPages[i] = b;
        TabPages[j] = a;
    }
}

拖放 API 的真正目的是在单独的应用程序之间或至少在单独的控件之间拖动东西。在这种情况下使用它们是多余的。

如果您支持我的回答,请确保您也支持 Cody 的回答,因为它是基于他的。

于 2012-07-06T11:22:47.267 回答
1

通过拖放重新排序 TabPages - Ludwig B.
灵感来自http://dotnetrix.co.uk/tabcontrol.htm#tip7

        private void tc_MouseDown(object sender, MouseEventArgs e)
        {
            // store clicked tab
            TabControl tc = (TabControl)sender;
            int hover_index = this.getHoverTabIndex(tc);
            if (hover_index >= 0) { tc.Tag = tc.TabPages[hover_index]; }
        }
        private void tc_MouseUp(object sender, MouseEventArgs e)
        {
            // clear stored tab
            TabControl tc = (TabControl)sender;
            tc.Tag = null;
        }
        private void tc_MouseMove(object sender, MouseEventArgs e)
        {           
            // mouse button down? tab was clicked?
            TabControl tc = (TabControl)sender;
            if ((e.Button != MouseButtons.Left) || (tc.Tag == null)) return;
            TabPage clickedTab = (TabPage)tc.Tag;
            int clicked_index = tc.TabPages.IndexOf(clickedTab);

            // start drag n drop
            tc.DoDragDrop(clickedTab, DragDropEffects.All);
        }
        private void tc_DragOver(object sender, DragEventArgs e)
        {
            TabControl tc = (TabControl)sender;

            // a tab is draged?
            if (e.Data.GetData(typeof(TabPage)) == null) return;
            TabPage dragTab = (TabPage)e.Data.GetData(typeof(TabPage));
            int dragTab_index = tc.TabPages.IndexOf(dragTab);

            // hover over a tab?
            int hoverTab_index = this.getHoverTabIndex(tc);
            if (hoverTab_index < 0) { e.Effect = DragDropEffects.None; return; }
            TabPage hoverTab = tc.TabPages[hoverTab_index];
            e.Effect = DragDropEffects.Move;

            // start of drag?
            if (dragTab == hoverTab) return;

            // swap dragTab & hoverTab - avoids toggeling
            Rectangle dragTabRect = tc.GetTabRect(dragTab_index);
            Rectangle hoverTabRect = tc.GetTabRect(hoverTab_index);

            if (dragTabRect.Width < hoverTabRect.Width)
            {
                Point tcLocation = tc.PointToScreen(tc.Location);

                if (dragTab_index < hoverTab_index)
                {
                    if ((e.X - tcLocation.X) > ((hoverTabRect.X + hoverTabRect.Width) - dragTabRect.Width))
                        this.swapTabPages(tc, dragTab, hoverTab);
                }
                else if (dragTab_index > hoverTab_index)
                {
                    if ((e.X - tcLocation.X) < (hoverTabRect.X + dragTabRect.Width))
                        this.swapTabPages(tc, dragTab, hoverTab);
                }
            }
            else this.swapTabPages(tc, dragTab, hoverTab);

            // select new pos of dragTab
            tc.SelectedIndex = tc.TabPages.IndexOf(dragTab);
        }

        private int getHoverTabIndex(TabControl tc)
        {
            for (int i = 0; i < tc.TabPages.Count; i++)
            {
                if (tc.GetTabRect(i).Contains(tc.PointToClient(Cursor.Position)))
                    return i;
            }

            return -1;
        }

        private void swapTabPages(TabControl tc, TabPage src, TabPage dst)
        {
            int index_src = tc.TabPages.IndexOf(src);
            int index_dst = tc.TabPages.IndexOf(dst);
            tc.TabPages[index_dst] = src;
            tc.TabPages[index_src] = dst;
            tc.Refresh();
        }
于 2011-05-19T22:52:04.677 回答
1

我稍微扩展了 Jacob Stanley 的答案。这样交换就不会经常发生。这对于不同大小的选项卡特别有用,在这种情况下,以前的解决方案在拖动时会经常交换。

用户体验的不同之处在于您必须进一步拖动才能实际移动选项卡。但这类似于浏览器中的标签重新排序。

我还在拖动时添加了一个手形光标并启用了双缓冲。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Controls
{
    public class DraggableTabControl : TabControl
    {
        private TabPage draggedTab;

        public DraggableTabControl()
        {
            this.MouseDown += OnMouseDown;
            this.MouseMove += OnMouseMove;
            this.Leave += new System.EventHandler(this.DraggableTabControl_Leave);

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        }

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            draggedTab = TabAt(e.Location);
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left || draggedTab == null)
            {
                this.Cursor = this.DefaultCursor;
                draggedTab = null;
                return;
            }

            int index = TabPages.IndexOf(draggedTab);          
            int nextIndex = index + 1;
            int prevIndex = index - 1;

            int minXForNext = int.MaxValue;
            int maxXForPrev = int.MinValue;

            var tabRect = GetTabRect(index);

            if (nextIndex < TabPages.Count)
            {
                var nextTabRect = GetTabRect(nextIndex);
                minXForNext = tabRect.Left + nextTabRect.Width;
            }

            if (prevIndex >= 0)
            {
                var prevTabRect = GetTabRect(prevIndex);
                maxXForPrev = prevTabRect.Left + tabRect.Width;
            }

            this.Cursor = Cursors.Hand;

            if (e.Location.X > maxXForPrev && e.Location.X < minXForNext)
            {
                return;
            }

            TabPage tab = TabAt(e.Location);

            if (tab == null || tab == draggedTab)
            {
                return;
            }

            Swap(draggedTab, tab);
            SelectedTab = draggedTab;
        }

        private TabPage TabAt(Point position)
        {
            int count = TabCount;

            for (int i = 0; i < count; i++)
            {
                if (GetTabRect(i).Contains(position))
                {
                    return TabPages[i];
                }
            }

            return null;
        }

        private void Swap(TabPage a, TabPage b)
        {
            int i = TabPages.IndexOf(a);
            int j = TabPages.IndexOf(b);

            TabPages[i] = b;
            TabPages[j] = a;
        }

        private void DraggableTabControl_Leave(object sender, EventArgs e)
        {
            this.Cursor = this.DefaultCursor;
            draggedTab = null;
        }
    }
}
于 2016-07-01T08:40:49.617 回答