[Webinar] Streamline your web hosting managementRegister Today

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

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)

or rather :

pDoc = pDocTemplate->CreateNewDocument()

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 !
1 Solution
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
Hi gambas:
   Have you overwrote CFrameWnd's CreateView() fucntion?  When you write
   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.  
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.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

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)
gambasAuthor Commented:

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 !

so? what will be with this question?

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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