c# – WinForms TabControl拖放问题

我有两个TabControls,我已经实现了在两个控件之间拖放tabpages的功能.它可以很好地工作,直到您从其中一个控件拖出最后一个tabpage.控件然后停止接受丢弃,我不能将任何tabpages重新放回该控件.

一个方向的拖放代码如下.反向控制名称的反向相同.

// Source TabControl
private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left) 
    this.tabControl1.DoDragDrop(this.tabControl1.SelectedTab, DragDropEffects.All);
}

//Target TabControl 
private void tabControl2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
  if (e.Data.GetDataPresent(typeof(TabPage))) 
    e.Effect = DragDropEffects.Move;
  else 
    e.Effect = DragDropEffects.None; 
} 

private void tabControl2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
{
  TabPage DropTab = (TabPage)(e.Data.GetData(typeof(TabPage))); 
  if (tabControl2.SelectedTab != DropTab)
    this.tabControl2.TabPages.Add (DropTab); 
}

解决方法:

您需要在TabControl中覆盖WndProc并在WM_NCHITTEST消息中将HTTRANSPARENT转换为HTCLIENT.

例如:

const int WM_NCHITTEST = 0x0084;
const int HTTRANSPARENT = -1;
const int HTCLIENT = 1;

//The default hit-test for a TabControl's
//background is HTTRANSPARENT, preventing
//me from receiving mouse and drag events
//over the background.  I catch this and 
//replace HTTRANSPARENT with HTCLIENT to 
//allow the user to drag over us when we 
//have no TabPages.
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);
    }
}
上一篇:mfc之tabcontrol的用法(非tabsheet)


下一篇:c# – 向TabControl容器动态添加选项卡