Come chiudere le finestre CDockablePane creati in modo dinamico?
-
20-09-2019 - |
Domanda
Nel mio MFC (Feature Pack) si possono creare in modo dinamico di docking riquadri da visualizzare grafici / tabelle ecc
Comunque, io non voglio lasciare che l'utente apre la stessa cosa due volte.
creo un riquadro in questo modo:
// Create CMyDockablePane pPane
pPane->Create(...);
pPane->EnableDocking(CBRS_ALIGN_ANY);
// Create CRect rcPane
pPane->FloatPane(rcPane);
Questo sembra funzionare bene.
Questo è il modo ho cercato di controllare se un riquadro esiste già. Un riquadro è identificato dal suo tipo (classe) e un parametro.
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;
}
Il problema di questo è che quando chiudo un riquadro, questo non viene riconosciuto. L'oggetto CDockingManager restituisce comunque il riquadro nei GetPanes () chiamata.
Come faccio a dire al gestore di non tornare riquadri che sono chiuse?
o
Come posso rimuovere il pannello da un elenco riquadro, quando è chiuso?
Aggiornamento
Mi sono tuffato un po 'più a fondo e ha scoperto, che gli oggetti CWnd non sono effettivamente chiusi, quando si fa clic sul pulsante 'x' nella barra del titolo, ma solo i loro contenitori.
Quindi il vero problema sembra essere quello di chiudere davvero i vetri.
Ho cambiato anche la questione in modo da riflettere meglio il problema.
Soluzione
Come descritto nel mio aggiornamento, il problema per l'attracco direttore avermi chiuso riquadri, era che i vetri non sono stati effettivamente chiusi. Solo chiusi i contenitori; i vetri stessi erano solo nascosti.
Quindi, per chiudere davvero i vetri che escludeva i seguenti metodi della mia CMDIFrameWndEx
classe derivata telaio principale:
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
}
E la seconda:
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
}
Altri suggerimenti
aggiungere Yor CMainFram una voce msg simile al seguente:
ON_REGISTERED_MESSAGE(AFX_WM_ON_PRESS_CLOSE_BUTTON,OnClosePane)
OnClosePane simile a questa:
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 è chiamato nel bel mezzo di CBasePane :: gestore OnLButtonDown, distruggendo la finestra renderà il vostro codice di affermare, quindi è necessario inviare un messaggio (WM_CLOSE) invece di inviarlo, questo dare CBasePane :: gestore OnLButtonDown la possibilità di finire l'esecuzione mentre il riquadro hWnd ancora valido. e per lo stesso resone torno vero per impedire vicino perché abbiamo già vicino via WM_CLOSE che distruggerà finestra pure.
messaggio WM_RESETMEMBER è iscritto un messaggio finestra per ripristinare membro riquadro a null.
applicazione simile a questa:
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;
}
si dovrebbe msg voce della mappa come uno:
ON_REGISTERED_MESSAGE(WM_RESETMEMBER,OnResetMember)
e si dovrebbe registrare un messaggio a livello globale in questo modo:
const UINT WM_RESETMEMBER = ::RegisterWindowMessage(_T("WM_RESETMEMBER"));
mi aspetto una chiamata a CDockingManager :: RemovePaneFromDockManager quando si sta chiudendo il vostro pannello per fare il lavoro.