Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created MDI Template like Following
C++
CMultiDocTemplate* pRunTemplate;

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

I am opening view on toolbar button click
C++
ON_COMMAND(ID_RUNVIEW, &CMerilisaRApp::OnRunview)
 
 void CMerilisaRApp::OnRunview()
   {
	this->pRunTemplate->OpenDocumentFile(NULL,1);
   }

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?
Posted
Updated 17-Sep-12 4:44am
v2
Comments
Richard MacCutchan 17-Sep-12 11:12am    
When you have opened the view set the menu item and/or toolbar to the disabled state so it cannot be selected again. Then when you close the view you can re-enable the toolbar item.
adityarao31 17-Sep-12 12:11pm    
Yah it seems to be very nice solution , I will surely try it Tomorrow

1 solution

You can iterate through your templates, documents, and views:
// Get the position of the first document template in the app
POSITION posTmpl = GetFirstDocTemplatePosition();	
while (posTmpl)
{	
    // Get the first document template.
    CDocTemplate *pTmpl = GetNextDocTemplate(posTmpl);
    if (pTmpl)
    {	
        // Get the position of the first document associated with the template
        POSITION posDoc = pTmpl->GetFirstDocPosition();
        while (posDos)
        {
            CDocument *pDoc = tmpl->GetNextDoc(posDos);
            if (pDoc)
            {
                // Get the position of the first view for the document
                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)
 
Share this answer
 
Comments
adityarao31 17-Sep-12 12:05pm    
Dear Sir, I have One Doc class ,but five templates with Five CFormView classes,In that case outer loop is applicable so CFormView *pFView = pDoc->GetNextView(posView); sentence I will write,I will get CFormView class pointer,but how I will know that Is it already Opened or not ?
Jochen Arndt 17-Sep-12 12:12pm    
If you get a pointer to a view, the view exists. Views that are actually not opened are not enumerated (because there is actually no instance of the view in memory).
adityarao31 17-Sep-12 12:14pm    
Yes It is perfectly good solution,Thank you very much.
adityarao31 17-Sep-12 12:19pm    
One more doubt I have Five CFormView derived classes linked with single document
How I can know which view is currently opened,as Code above just seems to give CFormView pointer, How can I find which one is open ?
Jochen Arndt 17-Sep-12 12:41pm    
You can do this by comparing the enumerated template pointer with those pointers stored when creating the templates (this requires that you store the pointers).

If you have a basic view class derived (e.g. CFormView -> CBaseView -> CView1, CView2, ...) you may add a member var to that class identifying the view/template type and check it using a cast for the enumerated view:
CBaseView *pView = static_cast<cbaseview*>(pDoc->GetNextViewPosition(posView));
if (pView->GetType() == VIEW1) ...

There are other solutions like tracking the views in the document class, using the title of the view and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900