• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

How to know View is already opened

I created  MDI Template like Following

     
CMultiDocTemplate* pRunTemplate;

Open in new window

     
 pRunTemplate = new CMultiDocTemplate(IDR_MerilisaRTYPE,
		RUNTIME_CLASS(CMerilisaRDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CRunView));
	if (!pRunTemplate)
		return FALSE;
	AddDocTemplate(pRunTemplate);

Open in new window

I  am opening view  on toolbar button click
   
ON_COMMAND(ID_RUNVIEW, &CMerilisaRApp::OnRunview)

Open in new window


   
void CMerilisaRApp::OnRunview()
   {
	this->pRunTemplate->OpenDocumentFile(NULL,1);
   }

Open in new window


  But  problem is that as many times I  click toolbar button that many times view opens
and I  want to open  view only ones.So how to find out  that view is already opened?
0
ADITYA RAO
Asked:
ADITYA RAO
  • 2
1 Solution
 
sarabandeCommented:
the OpenDocumentFile normally expects a (file) name as first argument. you passed NULL, so it always calls YourApp::OnFileNew what creates a new empty document + frame + view.

if you don't have files you could override CWinApp::OpenDocumentFile by YourApp::OpenDocumentFile and implement like

CDocument * YourApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
     CDocTemplate * pTempl;
     CRunView * pRunView = NULL;
     POSITION p1 = GetFirstDocTemplatePosition( ) ;
     while (p1)
     {
          pTempl = GetNextDocTemplate(p1);
          POSITION p2 = GetFirstDocTemplatePosition( ) ;
          while (p2 && !found)
          {
               CDocument * pDoc = pTempl->GetNextDoc(p2) ;
               if (pDoc->IsKindOf( RUNTIME_CLASS( CMerilisaRDoc ) ) )
                   return pDoc;
          }
     }
     return CWinApp::OpenDocumentFile(lpszFileName);
}

Open in new window


the code would return currently opened CMerilisaRDoc if existent.

Sara
0
 
sarabandeCommented:
as you have multiple documents you might want the OpenDocumentFile to handle only the CRunFile document separately. you could do so by passing a name argument:

CDocument * CMerilisaRApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
     CDocTemplate * pTempl;
     CRunView * pRunView = NULL;
     CString mrdoc = "MerilisaRDoc";
     if (lpszFileName != NULL && mrdoc ==  lpszFileName)
     {
        POSITION p1 = GetFirstDocTemplatePosition( ) ;
        while (p1)
        {
          pTempl = GetNextDocTemplate(p1);
          POSITION p2 = pTempl->GetFirstDocPosition( ) ;
          while (p2)
          {
               CDocument * pDoc = pTempl->GetNextDoc(p2) ;
               if (pDoc->IsKindOf( RUNTIME_CLASS( CMerilisaRDoc ) ) )
                   return pDoc;
          }
        }
    }
    return CWinApp::OpenDocumentFile(lpszFileName);
} 

void CMerilisaRApp::OnRunview()
{
     CDocument pDoc = this->OpenDocumentFile("MerilisaRDoc");
     if (pDoc != NULL)
     {
           POSITION p = pDoc->GetFirstViewPosition(); 
           if (p != NULL)
           {
               CRunView * pView = (CRunView *)pDoc->GetNextView(p);
               if (pView != NULL)
               {
                     pView->SetActiveWindow();
               }
           }
     }
}

Open in new window


Alternatively you could spare calling OpenDocumentFile from handler function and get the document directly from template pointer (i recognized now that pRunTemplate is a member of CMerilisaRApp):

POSITION p = pGetFirstDocPosition( ) ;
while (pRunTemplate != NULL && p != NULL)
{
      CDocument * pDoc = this->pRunTemplate->GetNextDoc(p) ;
      ....

Open in new window




Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now