Click to See Complete Forum and Search --> : How can I bring a selected view window to the top?


Xiaojian Liu
May 14th, 1999, 05:51 AM
I got a treeview holding the path of all the opened files. I can selected a
file path by double clicking an item of the
treeview. My problem is: how can I bring the
view window associated with the selected file to the top like MS VC++ does? I wrote the following
code overrided when double clicking
on an item of the treeview but this seems not work:

void CMainFrame::SetActiveWindow()
{
CString strDocName,strDocPath;
CDocTemplate* pSelectedTemplate;
CDocument* pSelectedDoc;
CMyView* pView;
POSITION posd,posv;
POSITION pos=AfxGetApp()->GetFirstDocTemplatePosition();
CMyTreeView* TreeCtrl=GetTreeViewFile(); // get a pointer to my treeview
CString strfilepath=TreeCtrl->GetFilePath();
while(pos!=NULL) {
pSelectedTemplate=(CDocTemplate*)AfxGetApp()->GetNextDocTemplate(pos);
ASSERT(pSelectedTemplate!=NULL);
pSelectedTemplate->GetDocString(strDocName,CDocTemplate::docName);
posd=pSelectedTemplate->GetFirstDocPosition();
while(posd!=NULL) {
pSelectedDoc=(CDocument*)pSelectedTemplate->GetNextDoc(posd);
ASSERT(pSelectedDoc!=NULL);
strDocPath=pSelectedDoc->GetPathName();
if(strDocPath==strfilepath) {
// Iterating for views
posv=pSelectedDoc->GetFirstViewPosition();
while(posv!=NULL) {
pView=(CMyView*)pSelectedDoc->GetNextView(posv);
ASSERT(pView!=NULL);
pView->SetActiveWindow();
pView->BringWindowToTop();
pSelectedDoc->UpdateAllViews(NULL);
}
}
}
}
}



I appricate your suggestions.

Jason Teagle
May 14th, 1999, 06:26 AM
First, trace through the routine and verify that your document IS being matched correctly.

Second, I think the MFC framework doesn't like people trying to manipulate views directly - because they are embedded in frames. Once you have got the view pointer, call its GetParentFrame() method and then activate THAT window. I think that might work better.

The moral of this story is: if you want to manipulate a view, try to see if you can manipulate its frame in the same way instead, to achieve the same result. It stands more chance of success. For example, trying to size a view will probably not work correctly - resize its frame instead.

Does this help?

Xiaojian Liu
May 14th, 1999, 08:17 AM
Many thanks, Jason. It works!!