Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms选项卡控件拖放问题_C#_.net_Winforms_Tabcontrol - Fatal编程技术网

C# WinForms选项卡控件拖放问题

C# WinForms选项卡控件拖放问题,c#,.net,winforms,tabcontrol,C#,.net,Winforms,Tabcontrol,我有两个TabControls,并且实现了在两个控件之间拖放tabpages的功能。在将最后一个选项卡页从其中一个控件上拖动下来之前,它的效果非常好。然后该控件停止接受拖放,我无法将任何tabpages放回该控件 一个方向的拖放代码如下所示。相反方向与交换的控件名称相同 // Source TabControl private void tabControl1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == Mouse

我有两个TabControls,并且实现了在两个控件之间拖放tabpages的功能。在将最后一个选项卡页从其中一个控件上拖动下来之前,它的效果非常好。然后该控件停止接受拖放,我无法将任何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); 
}

您需要覆盖选项卡控件中的
WndProc
,并在
WM\u 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);
    }
}

这解决了问题,谢谢!你怎么知道的?@Kevin:两年前这咬了我一口。我不记得我是用谷歌搜索还是用Spy++。这就是stackoverflow很棒的原因!我用谷歌搜索了几个小时,并尝试了很多其他的东西,用了几个小时。我使用了与@KevinGale完全相同的代码,但我无法让你的修复程序为我工作。我只是将您的代码复制并粘贴到我的Form1类中,而不是关于如何实现它的任何实际线索。我做错了什么?@JonnyP:你需要把这个放到
TabControl
类中。