如何关闭动态创建CDockablePane窗口?
-
20-09-2019 - |
题
在我的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()调用。
<击>我怎么能告诉经理来无回被关闭的窗格?点击 或强>结果 我如何从一个窗格列表,在关闭时删除窗格?击>
更新
我潜深一点,并发现,CWnd的对象实际上并没有关闭,点击标题栏的“X”按钮时,但只有它们的容器。点击 所以,真正的问题似乎是真的关闭窗格。结果 我也改变的问题以更好地反映该问题。
解决方案
正如我在更新中描述,在对接经理给我的问题关闭的窗格,是窗格实际上没有关闭。只有自己的集装箱被关闭;窗格自己只是隐藏。
因此,为了确实关闭窗格我推翻在我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
}
其他提示
添加到产油虽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:强>
OnClosePane被称为在CBasePane的中间:: OnLButtonDown中的处理程序,破坏窗口 将会使你的代码断言,所以你需要张贴消息(WM_CLOSE),而不是将其发送,这给CBasePane :: OnLButtonDown中的处理程序有机会完成执行,而面板的hWnd仍然有效。 出于同样的resone我返回TRUE以防止接近,因为我们已经通过关闭 WM_CLOSE这将破坏窗口也是如此。
WM_RESETMEMBER消息被注册的窗口消息来重置窗格构件为空。
它实施看起来像这样:
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"));
我期望调用 CDockingManager :: RemovePaneFromDockManager 当你关闭你的窗格来完成这项工作。