如何关闭动态创建的CDockablePane窗口?

时间:2009-08-26 10:40:10

标签: c++ docking mfc-feature-pack pane

在我的MFC(Feature Pack)应用程序中,可以动态创建停靠窗格以显示图表/表格等 但是,我不想让用户两次打开同一个东西。

我创建了一个这样的窗格:

// Create CMyDockablePane pPane
pPane->Create(...);
pPane->EnableDocking(CBRS_ALIGN_ANY);
// Create CRect rcPane
pPane->FloatPane(rcPane);

这似乎工作正常。

这是我尝试检查窗格是否已存在的方法。窗格由其类型(类)和参数标识。

BOOL CanOpenPane(const type_info & paneType, const CMyParameter & parameter) const
{
    CMainFrame* pFrm = GetMainFrame();
    CDockingManager* pDockMan = pFrm->GetDockingManager();


    // Check if there already is a pane of the same type which also has the same parameter.
    bool canOpen = true;
    CObList panes;
    pDockMan->GetPaneList(panes);
    POSITION pos = panes.GetHeadPosition();
    while (pos)
    {
        CMyDockablePane* pPane = dynamic_cast<CMyDockablePane*>(panes.GetNext(pos));
        if (NULL == pPane) { continue; }

        if (paneType == typeid(*pPane) &&
                pPane->GetParameter() == parameter)
        {
            canOpen = false;
            break;
        }
    }


    return canOpen;
}

问题在于,当我关闭窗格时,无法识别。 CDockingManager对象仍然返回GetPanes()调用中的窗格。

如何告诉经理不要退回已关闭的窗格?
当窗格关闭时,如何从窗格列表中删除窗格?


更新

我更深入地发现,当点击标题栏中的“x”按钮时,CWnd对象实际上并未关闭,只有他们的容器。 所以真正的问题似乎是真正关闭窗格 我也改变了问题以更好地反映问题。

4 个答案:

答案 0 :(得分:5)

如我的更新中所述,对接管理器给我关闭窗格的问题是,窗格实际上并未关闭。只有他们的容器被关闭;窗格本身就是隐藏的。

因此,为了真正关闭窗格,我在CMDIFrameWndEx派生的主框架类中覆盖了以下方法:

BOOL CMainFrame::OnCloseMiniFrame(CPaneFrameWnd* pWnd)
{
    if(0 == pWnd->GetPaneCount()) { return TRUE; } // No panes.. allow closing

    // Close all child panes of the miniframe that is about to be closed.
    //
    // Panes are placed inside a mini frame when they have the "floating" status.
    // Since I didn't find a way to iterate over the panes of a mini frame
    // (CMultiPaneFrameWnd can have several panes), we iterate over all panes
    // and close those whose parent frame is pWnd.

    CDockingManager* pDockMan = GetDockingManager();
    if(NULL != pDockMan)
    {
        CObList allPanes;
        pDockMan->GetPaneList(allPanes, TRUE, NULL, TRUE);

        for(POSITION pos = allPanes.GetHeadPosition(); pos != NULL;)
        {
            CDockablePane* pPane = dynamic_cast<CDockablePane*>(allPanes.GetNext(pos));
            if (NULL == pPane) { continue; }

            if(pWnd == pPane->GetParentMiniFrame())
            {
                pPane->PostMessage(WM_CLOSE); // Note: Post instead of Send
            }
        }

    }

    return TRUE; // Allow closing
}

第二个:

BOOL CMainFrame::OnCloseDockingPane(CDockablePane* pWnd)
{
    CObList paneList;

    // We can get CDockablePanes and CTabbedPanes here.
    // The tabbed panes contain dockable panes.
    CTabbedPane* pTabbed = dynamic_cast<CTabbedPane*>(pWnd);
    CDockablePane* pDockable = dynamic_cast<CDockablePane*>(pWnd);
    if(NULL != pTabbed)
    {
        pTabbed->GetPaneList(paneList);
    }
    else if(NULL != pDockable)
    {
        paneList.InsertAfter(paneList.GetHeadPosition(), pDockable);
    }

    // Whatever it was, we now have a list of dockable panes, which we will close.
    for(POSITION pos = paneList.GetHeadPosition(); NULL != pos;)
    {
        CDockablePane* pPane = dynamic_cast<CDockablePane*>(paneList.GetNext(pos));
        ASSERT(NULL != pPane);


        // Let the window disappear and then recalculate the layout.
        // Not doing this causes problems with panes grouped together in a tabbed pane.
        pPane->ShowWindow(SW_HIDE);
        RecalcLayout();

        // Really close the window so the docking manager also doesn't know of it anymore.
        pPane->Reset();
        pPane->PostMessage(WM_CLOSE); // Note: Post instead of Send
    }


    return TRUE; // Allow closing
}

答案 1 :(得分:2)

向CMainFram添加一个msg条目,如下所示:

ON_REGISTERED_MESSAGE(AFX_WM_ON_PRESS_CLOSE_BUTTON,OnClosePane)

OnClosePane看起来像这样:

LRESULT CMainFrame::OnClosePane(WPARAM,LPARAM lp)
{
    CBasePane* pane = (CBasePane*)lp;
    int id = pane->GetDlgCtrlID();
    pane->ShowPane(FALSE, FALSE, FALSE);
    RemovePaneFromDockManager(pane,TRUE,TRUE,TRUE,NULL);
    AdjustDockingLayout();
    pane->PostMessage(WM_CLOSE);
    PostMessage(WM_RESETMEMBER,id,0);
    return (LRESULT)TRUE;//prevent close , we already close it
}

COUTION:

在CBasePane :: OnLButtonDown处理程序中间调用OnClosePane,销毁窗口 将使您的代码断言,因此您需要发布消息(WM_CLOSE)而不是发送它,这使得CBasePane :: OnLButtonDown处理程序有机会在窗格hWnd仍然有效时完成执行。 并且对于相同的resone我返回True以防止关闭,因为我们已经通过关闭它 WM_CLOSE也会破坏窗口。

WM_RESETMEMBER消息是已注册的窗口消息,用于将窗格成员重置为null。

它的实现看起来像这样:

LRESULT CMainFrame::OnResetMember(WPARAM wp,LPARAM)
{
    int id = (int)wp;
    switch(id)
    {
        case IDC_BIDBOND_TREE_PANE:
            m_pBBTreePane.reset((BBTreePane*)NULL);
            break;
        case IDC_REFTREE_PANE :
            m_pRefTreePane.reset((RefTreePane*)NULL);
            break;
        default :
            return (LRESULT)FALSE;//id warent found

    }
    return (LRESULT)TRUE;
}

你应该像以下一样输入地图条目:

ON_REGISTERED_MESSAGE(WM_RESETMEMBER,OnResetMember)

你应该像这样在全球注册消息:

const UINT WM_RESETMEMBER = ::RegisterWindowMessage(_T("WM_RESETMEMBER"));

答案 2 :(得分:1)

当您关闭窗格以完成工作时,我希望能够拨打CDockingManager::RemovePaneFromDockManager

答案 3 :(得分:0)

在mfc文档中,它说不要使用showwindow,所以请使用showpane来显示窗格