Board index » Visual Studio » How to access tree control from win explorer type app

How to access tree control from win explorer type app

Visual Studio123
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


-
 

Re:How to access tree control from win explorer type app

MK wrote:

Quote
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





Accessing a control from a different dialog would be poor practice,

especially from a modal dialog. (Because a modal dialog should also

support the Cancel button.)



MFC provides a built in mechanism for updating views. Call

UpdateAllViews, which produces calls to OnUpdate for each view. In

turn, each view would update its own controls using the passed

parameters and doc data.



To access the CDocument::UpdateAllViews, note that your CWinApp class

contains a list of all doc templates, and they contain a list of all

docs. See GetFirstDocTemplatePosition, GetFirstDocPosition, etc.



--

Scott McPhillips [VC++ MVP]



-

Re:How to access tree control from win explorer type app

MK wrote:



Quote
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

-

Re:How to access tree control from win explorer type app

Some informative suggestions there and it's starting to make sense to

use the document interface. The problem is that I don't know how to use

it, so I'll need to read up on it and look at some practical examples.

In the meantime, it would be great to clarify a few things.



Scott, once I have the document template, how do I send data to it (eg.

a file name), so it will update the tree control? I've written some

code to get the document template, but I don't know if it's ok... How

can I use pobjDocTemplate to do what I need to?



POSITION pos = theApp.GetFirstDocTemplatePosition();

CDocTemplate * pobjDocTemplate = theApp.GetNextDocTemplate(pos);



So, the app document class has an interface to the tree control. Once I

have a document template, I still don't know what to do with it. How do

I pass a file name to it so it updates the tree control?



David, thanks for the starter and I agree with your comments about the

design. Not knowing much about the framework, I'm trying to hack my way

through it which isn't a good thing. Since this program is only a

prototype, I have some time to sort things out.



Regards,



Michael



-

Re:How to access tree control from win explorer type app

MK wrote:

Quote
Some informative suggestions there and it's starting to make sense to

use the document interface. The problem is that I don't know how to use

it, so I'll need to read up on it and look at some practical examples.

In the meantime, it would be great to clarify a few things.



Scott, once I have the document template, how do I send data to it (eg.

a file name), so it will update the tree control? I've written some

code to get the document template, but I don't know if it's ok... How

can I use pobjDocTemplate to do what I need to?



POSITION pos = theApp.GetFirstDocTemplatePosition();

CDocTemplate * pobjDocTemplate = theApp.GetNextDocTemplate(pos);



You should go through the SCRIBBLE tutorial that introduces examples of

most fundamental MFC concepts.



I don't think you've said whether your app is SDI or MDI. If SDI there

is only one doc object. Continue the above code with...

pos = GetFirstDocPosition();

CMyDoc* pDoc = (CMyDoc*)GetNextDoc(pos);



Now you can call your document. Suppose you add a doc function like...

pDoc->AddFile(fileName);



void CMyDoc::AddFile(LPCTSTR czFilename)

{

// You may wish to add czFilename to some internal data such as

// a list of files. Or, at minimum you could do this:

UpdateAllViews(NULL, HINT_NEW_FILE, (CObject*)czFilename);

}



where HINT_NEW_FILE is a #define in doc.h.



Having done all this, override OnUpdate in your view(s) to receive the

parameters passed to UpdateAllViews.



And why did we do all this? To improve maintainablility by reducing

dependencies. The dialog has no dependencies on anything. The view

depends only on the doc, no matter where its data may actually come

from. And the views have no dependencies on each other - a big feature

if future evolution brings more or different views.



--

Scott McPhillips [VC++ MVP]



-

Re:How to access tree control from win explorer type app

Hi Scott,



Thanks for clarifying that. I started reading up on this stuff last

night and I'm beginning to understand the relationship between the

classes better. I'll be working through the SCRIBBLE tutorial today,

and will work on a better design for the app. The suggestions and

examples by yourself and David are really helpful and have cleared a

few things up. BTW, the app is SDI, so your example is clearly

understood.



Regards,



Michael



Scott McPhillips [MVP] wrote:

Quote
MK wrote:

>Some informative suggestions there and it's starting to make sense to

>use the document interface. The problem is that I don't know how to use

>it, so I'll need to read up on it and look at some practical examples.

>In the meantime, it would be great to clarify a few things.

>

>Scott, once I have the document template, how do I send data to it (eg.

>a file name), so it will update the tree control? I've written some

>code to get the document template, but I don't know if it's ok... How

>can I use pobjDocTemplate to do what I need to?

>

>POSITION pos = theApp.GetFirstDocTemplatePosition();

>CDocTemplate * pobjDocTemplate = theApp.GetNextDocTemplate(pos);



You should go through the SCRIBBLE tutorial that introduces examples of

most fundamental MFC concepts.



I don't think you've said whether your app is SDI or MDI. If SDI there

is only one doc object. Continue the above code with...

pos = GetFirstDocPosition();

CMyDoc* pDoc = (CMyDoc*)GetNextDoc(pos);



Now you can call your document. Suppose you add a doc function like...

pDoc->AddFile(fileName);



void CMyDoc::AddFile(LPCTSTR czFilename)

{

// You may wish to add czFilename to some internal data such as

// a list of files. Or, at minimum you could do this:

UpdateAllViews(NULL, HINT_NEW_FILE, (CObject*)czFilename);

}



where HINT_NEW_FILE is a #define in doc.h.



Having done all this, override OnUpdate in your view(s) to receive the

parameters passed to UpdateAllViews.



And why did we do all this? To improve maintainablility by reducing

dependencies. The dialog has no dependencies on anything. The view

depends only on the doc, no matter where its data may actually come

from. And the views have no dependencies on each other - a big feature

if future evolution brings more or different views.



--

Scott McPhillips [VC++ MVP]



-