?
Solved

How  to know View  is already opened

Posted on 2012-09-17
2
Medium Priority
?
610 Views
Last Modified: 2012-09-18
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
Comment
Question by:ADITYA RAO
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 38408940
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
 
LVL 35

Expert Comment

by:sarabande
ID: 38410421
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question