none
怎么做出像VS那样的TabControl可以拖动标签改变位置,标签右键菜单? RRS feed

答案

  • 你好 12vd7

    欢迎来到MSDN中文论坛。

    对于tabControl拖动标签改变位置,我找到一个例子供你参考:

                    Drag and Drop Tab Control - CodeProject

    基于上面的例子,我做了一点修改,是为了实现右键标签菜单。 代码如下:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    
    namespace DraggableTabControl
    {
        /// <summary>
        /// 这是基于自定义控件的一个例子
        /// </summary>
    	[ToolboxBitmap(typeof(DraggableTabControl))]
    	public class DraggableTabControl : System.Windows.Forms.TabControl
    	{
    		private System.ComponentModel.Container components = null;
            private ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();//创建一个右键菜单
    		public DraggableTabControl()
    		{
    			InitializeComponent();
    			AllowDrop = true;
                contextMenuStrip1.Items.Add("name");//添加一些item。
                contextMenuStrip1.Items.Add("age");
                contextMenuStrip1.Items.Add("sex");
                
    		}
    
           
    
    		protected override void Dispose( bool disposing )//重载Dispose方法,释放对象
    		{
    			if( disposing )
    			{
    				if(components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    
    		private void InitializeComponent()//初始化
    		{
                this.SuspendLayout();
                this.ResumeLayout(false);
    
    		}
    
    
            protected override void OnDragOver(System.Windows.Forms.DragEventArgs e)//重载OnDragOver方法
    		{
    			base.OnDragOver(e);
    		
    			Point pt = new Point(e.X, e.Y);
    			//首先活得鼠标位置
    			pt = PointToClient(pt);
    			
    			//得到鼠标选定点tag
    			TabPage hover_tab = GetTabPageByTab(pt);
    
    			//确定鼠标选择的是tag
    			if(hover_tab != null)
    			{
    				//确定鼠标在拖拽tag
    				if(e.Data.GetDataPresent(typeof(TabPage)))
    				{
    					e.Effect = DragDropEffects.Move;
    					TabPage drag_tab = (TabPage)e.Data.GetData(typeof(TabPage));
    
    					int item_drag_index = FindIndex(drag_tab);
    					int drop_location_index = FindIndex(hover_tab);
    
    					//如果鼠标未移动tag
    					if(item_drag_index != drop_location_index)
    					{
    						ArrayList pages = new ArrayList();
    
                            //把所有的tag加到一个ArrayList.
    						for(int i = 0; i < TabPages.Count; i++)
    						{
    							//除了需要移动的tag.
    							if(i != item_drag_index)
    								pages.Add(TabPages[i]);
    						}
    
    						//把要移动的tag放到正确的位置
    						pages.Insert(drop_location_index, drag_tab);
    						TabPages.Clear();
    						TabPages.AddRange((TabPage[])pages.ToArray(typeof(TabPage)));
    						SelectedTab = drag_tab;
    					}
    				}
    			}
    			else
    			{
    				e.Effect = DragDropEffects.None;
    			}
    		}
    
    		protected override void OnMouseDown(MouseEventArgs e)
    		{
    			base.OnMouseDown(e);
    
    			Point pt = new Point(e.X, e.Y);
    			TabPage tp = GetTabPageByTab(pt);
    			
    			if(tp != null)
    			{
    				DoDragDrop(tp, DragDropEffects.All);
    			}
                if (e.Button == MouseButtons.Right)//添加右键菜单
                {
                    this.contextMenuStrip1.Show(this, e.Location);
                } 
    		}
    		private TabPage GetTabPageByTab(Point pt)
    		{
    			TabPage tp = null;
    
    			for(int i = 0; i < TabPages.Count; i++)
    			{
    				if(GetTabRect(i).Contains(pt))
    				{
    					tp = TabPages[i];
    					break;
    				}
    			}
    			
    			return tp;
    		}
    		private int FindIndex(TabPage page)
    		{
    			for(int i = 0; i < TabPages.Count; i++)
    			{
    				if(TabPages[i] == page)
    					return i;
    			}
    
    			return -1;
    		}
    	}
    }
    
    

    希望能帮得到您。


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年11月7日 8:52
    版主