Re: How to access tree control from win explorer type app
MK wrote:
Hi,
I've been coding C++ for quite a number of years, but am new to MFC
(I've used Borland Builder 3 to do something similar but the interface
and my design is differnt in what I'm doing now). I'm building a
Windows Explorer type application (MFC), yes another one of those,
similar looking interface and functionality. I used the wizard in VC++
to create the basic Windows Explorer shell and I have example code for
the shell interface, which I'll add later on. At the moment, I'm trying
to add items to the tree control from various dialogues. My question is
how can I access the CTreeCtrl from a dialogue opened from a menu
(something simliar to what "Map Network Drive" does in Windows
Explorer), so I can update it with a new item/node?
For example, in the main frame (main form) I can access the tree
control as follows
CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
CLeftView* pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
CTreeCtrl & objTreeCtrl = pView->GetTreeCtrl();
I have a dialogue that is opened from one the application menus
void CmkedmApp::OnToolsMapImagePath()
{
NewTreeItemDlg objNewTreeItemDlg;
objNewTreeItemDlg.DoModal();
}
where the user can select a file that is to be added to the tree
control.
I've seen a few example programs, which have helped a great deal, but
haven't found anything yet of how to access the tree control so I can
add items to it from a different dialogue?
Regards,
Michael
Michael:
First of all the dialog should not be updating the tree control itself.
The dialog's job is to get the file name, not to say what to do with it.
So, an improvement from an OOP viewpoint would be
void CmkedmApp::OnToolsMapImagePath()
{
NewTreeItemDlg objNewTreeItemDlg;
if(objNewTreeItemDlg.DoModal() == IDOK)
{
CString fileName = objNewTreeItemDlg.FileName();
CMainFrame* pMainFrame = (CMainFrame*)m_pMainWnd;
CLeftView* pView = pMainFrame->GetLeftView();
pView->AddFile(fileName);
}
}
However I would question whether the application object is the right
place for this handler. If your CLeftView is always the active view then
you could put the handler in the view. Otherwise, the document might be
a better place, because it can call UpdateAllViews() directly, as
Scott suggests.
David Wilkinson