How To Acquire a List of All CDocument Objects (106455)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++ for Windows, 16-bit edition 1.0
- Microsoft Visual C++ for Windows, 16-bit edition 1.5
- Microsoft Visual C++ for Windows, 16-bit edition 1.51
- Microsoft Visual C++ for Windows, 16-bit edition 1.52
- Microsoft Visual C++, 32-bit Editions 1.0
- Microsoft Visual C++, 32-bit Editions 2.0
- Microsoft Visual C++, 32-bit Editions 2.1
- Microsoft Visual C++, 32-bit Editions 2.2
- Microsoft Visual C++, 32-bit Editions 4.0
This article was previously published under Q106455 SUMMARY
The code samples below demonstrate how to retrieve a list of pointers to
all open CDocument (and CDocument-derived objects) created by a
CWinApp-derived object. For MFC versions 3.x and below, Sample Code 1 shows
the use of CWinApp::m_templateList, CDocTemplate::GetFirstDocPosition(),
and CDocTemplate::GetNextDoc(). For MFC version 4.0, Sample Code 2 provides
a similar implementation, but uses CWinApp::GetFirstDocTemplatePosition()
and CWinApp::GetNextDocTemplate() to obtain the valid CDocTemplate
pointers.
For both code samples, CMyApp is derived from CWinApp. Moreover, once a
valid CDocTemplate pointer is obtained, CDocTemplate::GetFirstDocPosition()
and CDocTemplate::GetNextDoc() are used in both samples to iterate through
the list of open documents for each document template. For MFC versions 3.x
and earlier, an application object's list of document template pointers is
contained in CWinApp::m_templateList, a CPtrList object. Sample Code 1
makes use of this fact. For MFC version 4.0, CWinApp uses a CDocManager to
maintain its list of document templates and provides the
GetFirstDocTemplatePosition() and GetNextDocTemplate() member functions to
traverse the list. Sample Code 2 exhibits this.
Sample Code 1 - MFC 3.x and prior
/* Compile options needed: none
*/
void CMyApp::GetDocumentList(CObList * pDocList)
{
ASSERT(pDocList->IsEmpty());
POSITION pos = m_templateList.GetHeadPosition();
while (pos)
{
CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
POSITION pos2 = pTemplate->GetFirstDocPosition();
while (pos2)
{
CDocument * pDocument;
if ((pDocument=pTemplate->GetNextDoc(pos2)) != NULL)
pDocList->AddHead(pDocument);
}
}
}
Sample Code 2 - MFC 4.0
/* Compile options needed: none
*/
void CMyApp::GetDocumentList(CObList * pDocList)
{
ASSERT(pDocList->IsEmpty());
POSITION pos = GetFirstDocTemplatePosition();
while (pos)
{
CDocTemplate* pTemplate = (CDocTemplate*)GetNextDocTemplate(pos);
POSITION pos2 = pTemplate->GetFirstDocPosition();
while (pos2)
{
CDocument * pDocument;
if ((pDocument=pTemplate->GetNextDoc(pos2)) != NULL)
pDocList->AddHead(pDocument);
}
}
}
Modification Type: | Minor | Last Reviewed: | 6/29/2004 |
---|
Keywords: | kbDocView kbhowto KB106455 |
---|
|