You can iterate through your templates, documents, and views:
POSITION posTmpl = GetFirstDocTemplatePosition();
while (posTmpl)
{
CDocTemplate *pTmpl = GetNextDocTemplate(posTmpl);
if (pTmpl)
{
POSITION posDoc = pTmpl->GetFirstDocPosition();
while (posDos)
{
CDocument *pDoc = tmpl->GetNextDoc(posDos);
if (pDoc)
{
POSITION posView = pDoc->GetFirstViewPosition();
while (posView)
{
CView *pView = pDoc->GetNextView(posView);
}
}
}
}
}
If you have only one template, you may remove the outer loop and use your
pRunTemplate
. If you have only one view per document, the innermost while condition can be replaced by an if condition.
The check if a view is already opened depends on your document/view organization. If no file is opened by your app, there is no document for the template. If a file is opened, you may compare the name using CDocument::GetPathName(). You may also check special state members of your document and view classes by casting the
GetNext...()
return values to your derived classes.
Finally you can put the check into an
ID_RUNVIEW
update handler. Then the command is disabled when the check fails indiacting this to the user:
ON_UPDATE_COMMAND_UI(ID_RUNVIEW, OnUpdateRunview)