2

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

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

// 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

4 回答 4

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
}
于 2009-08-26T17:00:01.527 回答
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
}

注意:

OnClosePane 在 CBasePane::OnLButtonDown 处理程序中间调用,销毁窗口将使您的代码断言,因此您需要发布消息(WM_CLOSE)而不是发送它,这使 CBasePane::OnLButtonDown 处理程序有机会在窗格 hWnd 时完成执行还是有效 。并且对于同样的原因,我返回 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;
}

你应该像这样的 msg 映射条目:

ON_REGISTERED_MESSAGE(WM_RESETMEMBER,OnResetMember)

你应该像这样在全局范围内注册消息:

const UINT WM_RESETMEMBER = ::RegisterWindowMessage(_T("WM_RESETMEMBER"));
于 2012-11-04T16:53:22.960 回答
1

当您关闭窗格以完成工作时,我希望调用CDockingManager::RemovePaneFromDockManager 。

于 2009-08-26T12:29:06.697 回答
0

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

于 2020-03-17T02:27:57.453 回答