Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

managing multiple doc templates

Posted on 1999-06-21
9
Medium Priority
?
342 Views
Last Modified: 2013-11-20
i have an SDI app with multiple doc templates. they are created in CWinApp::InitInstance like this...

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
  IDR_MAINFRAME,
  RUNTIME_CLASS(CPEDoc),
  RUNTIME_CLASS(CMainFrame),   // main SDI frame window
  RUNTIME_CLASS(CPEView));
AddDocTemplate(pDocTemplate);

// note: the following template is _not_ registered with
//  the framework.
m_pImageTemplate = new CSingleDocTemplate(
  IDR_IMAGEFRAME,
  RUNTIME_CLASS(CPEDoc),
  RUNTIME_CLASS(CImageFrame),
  RUNTIME_CLASS(CImageView));

the second template is for displaying a popup mini frame window which shows a bitmap image rendering of the document.

there is a menu command to create and display this window which has code like this...

void CMainFrame::OnViewFormatImage()
{
  CPEApp* pApp = (CPEApp*) AfxGetApp();

  CPEDoc* pDoc = (CPEDoc*) GetActiveDocument();
  ASSERT(pDoc);

  CImageFrame* pFrame = (CImageFrame*) pApp->m_pImageTemplate->CreateNewFrame(pDoc, NULL);
  ASSERT(pFrame);
  ASSERT(pFrame->IsKindOf(RUNTIME_CLASS(CImageFrame)));

  pApp->m_pImageTemplate->InitialUpdateFrame(pFrame, pDoc);
}

this all works ok but here's the problem:
1. how do i determine if this window is currently displayed; all i seem to have access to is the doc template instance: m_pImageTemplate.
2. if it is displayed then how do i close it?
0
Comment
Question by:paulburns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1307482
I have a suggestion that you define CImageFrame* pFrame; in your .h file and pFrame = NULL; in the .cpp file. When it is created, Check pFrame != NULL and you know it is there. Can you use pFrame->DestroyWindow() to close it? And set it to NULL again.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1307483
1. Get the current active view and check its type.

CWnd *pMainWnd = AfxGetMainWnd ();

CView *pView = static_cast<CFrameWnd *>(pMainWnd)->GetActiveFrame ()->GetActiveView ();

if (pView->IsKindOf (RUNTIME_CLASS (CImageView)) {
TRACE (_T ("Image view already active\n"));
}

2.To close a view, post a WM_CLOSE message to its frame.

CImageFrame* pFrame;

pFrame->PostMessage (WM_CLOSE);
0
 
LVL 2

Author Comment

by:paulburns
ID: 1307484
sorry, that code dont work: the frame i want to close may not be the active one.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:naveenkohli
ID: 1307485
Then we can take the alternate approach..
1. Go through each and every view associated with the active document and see if that view is valid or not.

CWnd *pWnd = AfxGetMainWnd ();
ASSERT_KINDOF (CFrameWnd, pWnd);
CFrameWnd *pFrame = statoc_cast<CFrameWnd *> (pWnd);
CFrameWnd *pActiveFrame = pFrame->GetActiveFrame ();

CDocument *pDoc = pActiveFrame->GetActiveDocument ();

POSITION pos = pDoc->GetFirstViewPosition ();

while (pos != NULL) {
CView *pView = pDoc->GetNextView (pos);
if (pView != NULL && pView->IsKindOf (RUNTIME_CLASS (CImageView)) {
// the image view is active.
}
}
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1307486
After getting if the image view is active or not, try using DestroyWindow on that view.

pImageView->DestroyWindow ();

Good Luck!
0
 
LVL 1

Expert Comment

by:dspellman
ID: 1307487
Trestan sort of suggested the approach I propose.  IMHO the doc/view architecture is an impediment to using MFC (MFC is an impediment to programming Windows, and Windows is just an impediment to using a PC, etc.)  In my doc/view applications, I create global pointers to each view so that I can access them anywhere and anytime I need to without worrying about how to find them (which is a bigger problem than writing the code usually).  

In this case, I would create a CImageFrame* as a global variable.  Initialize it to NULL at program startup, and then save the view pointer when the view is initially created.  When you need to find the view, you know where it is and can do things like test whether it is open because it is global and always available.  You can call any member function in the view using the pointer.  When you close/destroy the view, set the pointer to NULL again so you know it is not there.  

Forget about messing around with the doc/view rigamarole and live happily ever after.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1307488
i don't know how good is the idea of using global variables. that points out that there is something wrong with the design of application. its alsways a bad programming practice to rely on global variables. it totally defeats the purpose of OOD. especially the objects which directly affect ur view. and how will you manage the access from different threads.

may be its my way of thinking...

good luck!!

0
 
LVL 2

Author Comment

by:paulburns
ID: 1307489
I chose to use the doc-view architecture in this case because it seemed logical: i just needed to display a different view of the document. Also MFC does have some good features here (IMHO): view creation/destruction is managed automatically and there is a standard doc-view communication method - UpdateAllViews, OnUpdate, etc.

The only thing unusual in my case is that i'm using a different frame as well as a different view.

Trestan's idea works and is the simplest so i'll stick with that. But this doc-view stuff does seem a bit odd: the doc template is used to create the frame/doc/view connection but then doesn't seem to be used at all after that?!?

Anyway, thanks everyone for all the help. Trestan, points are yours.

0
 
LVL 8

Accepted Solution

by:
trestan earned 400 total points
ID: 1307490
Thanks. I just came back to EE.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

730 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