Crating CDocument from scratch

In a wizard-generated MFC app, the framework generates code to open documents when the user opens a document or creates a new one.

What I want to do is automatically creating a document (and subsequntly display it) as a result of a given operation.

Unfortunately, I fail to find the right sequence of operations. Is it sth like :

CMyDoc *pDoc = new CMyDoc(blah,blah)
pDocTemplate->AddDocument(pDoc)

or rather :

pDoc = pDocTemplate->CreateNewDocument()
pDoc->MyCustomInitialization()
pDocTemplate->CreateNewFrame(...)

I always get a bunch of errors in CreateNewFrame. Possibly my CDocument instance is not initialized correctly : is it my business to initialize the view/ the frame ?

I can't just send a message IDD_FILE_NEW beacuse I need to provide some parameters for the creation of the very document.

Thanks for any hints !
gambasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikeblasCommented:
Is this an MDI app or an SDI app?  What you do will be different for each architecture.

Presumably, if it's an SDI app, you want to close the existing doc and replace it with your new one, right? And if it's an MDI app, you just want to open the new one, correct?

Or are you doing things that are sick and not natural?

..B ekiM
0
GlennDeanCommented:
Hi gambas:
   Have you overwrote CFrameWnd's CreateView() fucntion?  When you write
   pDocTemplate->CreateNewFrame(pDoc,NULL);
   that code causes Windows to send a WM_CREATE message to the frame.  A cascase of calls occurs which ends up in your frame's CreateView function.  It may be possible that the source of your errors is that your view object isn't being created or not being created properly because of no CreateView function in your frame class.  
   Glenn
0
RAVIDCommented:
hi,
well u can add a new class and make CDocument as the base class then u will get all the features of CDocument and u can use it as u wish.
go to class wizard select add new class and make the base class as CDocument.

regards
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

migelCommented:
Hi!
here is code we use to make same task:
{
   ASSERT( pTemplate != NULL ) ;
   CDocument *pDoc = NULL ;
   CFrameWnd *pFrame ;

   pDoc = pTemplate->CreateNewDocument() ;
   if( pDoc == NULL )
       return NULL ;
// here you can update doc props, but remember! all views and frames aren`t created yet!
   pFrame = pTemplate->CreateNewFrame( pDoc, NULL ) ;
   if( pFrame != NULL )
       pTemplate->InitialUpdateFrame( pFrame, pDoc ) ;
}

this code correctly worked if you pTemplate class exist in the APP templates list (added by AddDocTemplate)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gambasAuthor Commented:
Year,

in fact migel is right, I found out that the problem was related to the fact that I called CreateNewFrame in a separate thread (the creation of the new document is compute-intesive and is performed in a worker thread)

Do the same without a thread (which is exactly what Migel proposes) and everything works fine.

I'll post another question so you can explain me the basics of MFC & threads !

Cheers
0
migelCommented:
so? what will be with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.