CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Automatically create multiple doc/view in MDI in MFC

    Hi.

    How do I create multiple document/views at the start of the program?
    I would prefer it as a doc/view list with IDs that I can access from other part of the program.

    For example I have a MenuBar with "Create 2x2" and I would like to dynamically create 4 doc/views
    in 2x2 matrix with each showing different data based on the doc that belongs to it.

    Also how do I disable automatic creation of the first view?
    You just divided by zero, didn't you?

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Automatically create multiple doc/view in MDI in MFC

    Quote Originally Posted by Odiee View Post
    Also how do I disable automatic creation of the first view?
    I guess you mean the automatic open of the first doc (and the first view) which is usually done if no comannd line parameter has been given?

    Then, in your app's class InitInstance function search for the call of "ParseCommandLine(cmdInfo);" and add a line after it:

    Code:
        ParseCommandLine(cmdInfo);
        cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
    To create a new doc programmatically:

    From any function of your app do the following:

    Code:
            POSITION pos = GetFirstDocTemplatePosition();
            CDocTemplate* pT = GetNextDocTemplate(pos);
            CDocument* pDoc = pT->OpenDocumentFile(NULL);
    Note: This will create a new doc of the first doc template class. If you're using more then one doc templates (not by default on wizzard created MDI app's) you'll need to add some more stuff.

    Hope that helps

    With regards
    Programartist

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Automatically create multiple doc/view in MDI in MFC

    First, handle ID_FILE_NEW command in your CWinApp-derived class and do nothing inside.
    Next, loop into document templates list and create the documents and frames as you wish.
    Example:
    Code:
    void CMyMDIApp::OnOpenAllViews() 
    {
       POSITION posDocTemplate = GetFirstDocTemplatePosition();
       while(NULL != posDocTemplate)
       {
          CDocTemplate* pDocTemplate = GetNextDocTemplate(posDocTemplate);
          POSITION posDoc = pDocTemplate->GetFirstDocPosition();
          if(NULL == posDoc)
          {
             pDocTemplate->OpenDocumentFile(NULL);
          }
       }
    }
    [ Later edit ]
    I've noticed that assigning CCommandLineInfo::FileNothing to cmdInfo.m_nShellCommand, suggested by ProgramArtist is more elegant than handling ID_FILE_NEW.
    Last edited by ovidiucucu; April 21st, 2011 at 05:37 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: Automatically create multiple doc/view in MDI in MFC

    That's exactly what I was looking for.
    Thanks!
    You just divided by zero, didn't you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured