Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. VC++ 6.0, MFC: Two different views attached to same document.

VC++ 6.0, MFC: Two different views attached to same document.

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    requemao
    wrote on last edited by
    #1

    Following the CHKBOOK sample in MSDN I finally have two different views associated with the same document, and working simultaneously. The problem is that when I run my program I only get the first view. The second view only appears when I *open* a document from a file, and not when I start a new one. The cause is no wonder, because the piece of code that opens the second view is only inside the OpenDocumentFile() function. That makes sense in the sample, which is designed to work always on a file (either is opens an existing file, or it creates a new one). Q: What code should I add where, for my application to open both views on a *new* document? Furthermore, I have learnt that you can resize a view by calling its parent frame's MoveWindow() function. I would like to size each of my two views to a certain desired size at startup. What is the appropriate place to add this code?

    J 1 Reply Last reply
    0
    • R requemao

      Following the CHKBOOK sample in MSDN I finally have two different views associated with the same document, and working simultaneously. The problem is that when I run my program I only get the first view. The second view only appears when I *open* a document from a file, and not when I start a new one. The cause is no wonder, because the piece of code that opens the second view is only inside the OpenDocumentFile() function. That makes sense in the sample, which is designed to work always on a file (either is opens an existing file, or it creates a new one). Q: What code should I add where, for my application to open both views on a *new* document? Furthermore, I have learnt that you can resize a view by calling its parent frame's MoveWindow() function. I would like to size each of my two views to a certain desired size at startup. What is the appropriate place to add this code?

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      requemao wrote:

      Q: What code should I add where, for my application to open both views on a *new* document?

      None. The sample has handled multi-view management already. After you create/open a document, the main frame adds "Check" and "BooK" menu items to your view menu. You should be to switch views by clicking on either "Check" or "Book".

      Best, Jun

      R 1 Reply Last reply
      0
      • J Jun Du

        requemao wrote:

        Q: What code should I add where, for my application to open both views on a *new* document?

        None. The sample has handled multi-view management already. After you create/open a document, the main frame adds "Check" and "BooK" menu items to your view menu. You should be to switch views by clicking on either "Check" or "Book".

        Best, Jun

        R Offline
        R Offline
        requemao
        wrote on last edited by
        #3

        Sorry, I did not explain it correctly: It's not the Checkbook application that I'm working on. I am working on an application of my own, which needs to manage and coordinate two different views. I did not know how to achieve that, so I had a look at several articles on CodeGuru and old topics on microsoft.public.vc.mfc, with little to no success. Finally I found the Checkbook sample and tried to imitate it. I succeeded, except that the second view is only shown when I *open* an existing document, but not when I simply start the program or click File/New. I know I should add some code somewhere, because right now there is only piece of code that spawns the second view, and that is inside CMyApp::OpenDocumentFile(). The question is: where is the appropriate place for that code? (And what is exactly the code, if that's not asking too much).

        J 1 Reply Last reply
        0
        • R requemao

          Sorry, I did not explain it correctly: It's not the Checkbook application that I'm working on. I am working on an application of my own, which needs to manage and coordinate two different views. I did not know how to achieve that, so I had a look at several articles on CodeGuru and old topics on microsoft.public.vc.mfc, with little to no success. Finally I found the Checkbook sample and tried to imitate it. I succeeded, except that the second view is only shown when I *open* an existing document, but not when I simply start the program or click File/New. I know I should add some code somewhere, because right now there is only piece of code that spawns the second view, and that is inside CMyApp::OpenDocumentFile(). The question is: where is the appropriate place for that code? (And what is exactly the code, if that's not asking too much).

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          Can't be sure without seeing your code. You may verify a couple of things: 1) Your app overrides OpenDocumentFile() so that it displays any views beside the default. The base class OpenDocumentFile() opens the first view as the default. 2) Let your app to handle file-new command, and call the overriden OpenDocumentFile() in the command handler.

          Best, Jun

          R 1 Reply Last reply
          0
          • J Jun Du

            Can't be sure without seeing your code. You may verify a couple of things: 1) Your app overrides OpenDocumentFile() so that it displays any views beside the default. The base class OpenDocumentFile() opens the first view as the default. 2) Let your app to handle file-new command, and call the overriden OpenDocumentFile() in the command handler.

            Best, Jun

            R Offline
            R Offline
            requemao
            wrote on last edited by
            #5

            Jun Du wrote:

            1. Your app overrides OpenDocumentFile() so that it displays any views beside the default.

            Yes, this is done. When I open an existing document (and ONLY then), both views appear and work correctly. It is done this way (just like the sample): CDocument* CSequApp::OpenDocumentFile(LPCTSTR lpszFileName) { CSequDoc* pDoc = (CSequDoc*) CWinApp::OpenDocumentFile(lpszFileName); if (pDoc == NULL) return NULL; CFrameWnd* pNewFrame = m_pEditorTemplate->CreateNewFrame(pDoc, NULL); if (pNewFrame == NULL) return pDoc; m_pEditorTemplate->InitialUpdateFrame(pNewFrame, pDoc); }

            Jun Du wrote:

            1. Let your app to handle file-new command, and call the overriden OpenDocumentFile() in the command handler.

            At present, I have this in the message map: ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) If I understand you correctly, I should change it to... ON_COMMAND(ID_FILE_NEW, OnFileNew) ...and then define the OnFileNew function: void CSequApp::OnFileNew() { CWinApp::OnFileNew(); ... (some call to OpenDocumentFile()) } Is this right? In that case, what's the code I have to add there? I don't think I'll get it right myself...

            J 2 Replies Last reply
            0
            • R requemao

              Jun Du wrote:

              1. Your app overrides OpenDocumentFile() so that it displays any views beside the default.

              Yes, this is done. When I open an existing document (and ONLY then), both views appear and work correctly. It is done this way (just like the sample): CDocument* CSequApp::OpenDocumentFile(LPCTSTR lpszFileName) { CSequDoc* pDoc = (CSequDoc*) CWinApp::OpenDocumentFile(lpszFileName); if (pDoc == NULL) return NULL; CFrameWnd* pNewFrame = m_pEditorTemplate->CreateNewFrame(pDoc, NULL); if (pNewFrame == NULL) return pDoc; m_pEditorTemplate->InitialUpdateFrame(pNewFrame, pDoc); }

              Jun Du wrote:

              1. Let your app to handle file-new command, and call the overriden OpenDocumentFile() in the command handler.

              At present, I have this in the message map: ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) If I understand you correctly, I should change it to... ON_COMMAND(ID_FILE_NEW, OnFileNew) ...and then define the OnFileNew function: void CSequApp::OnFileNew() { CWinApp::OnFileNew(); ... (some call to OpenDocumentFile()) } Is this right? In that case, what's the code I have to add there? I don't think I'll get it right myself...

              J Offline
              J Offline
              Jun Du
              wrote on last edited by
              #6

              requemao wrote:

              If I understand you correctly, I should change it to... ON_COMMAND(ID_FILE_NEW, OnFileNew) ...and then define the OnFileNew function: void CSequApp::OnFileNew() { CWinApp::OnFileNew(); ... (some call to OpenDocumentFile()) }

              Yes.

              Best, Jun

              1 Reply Last reply
              0
              • R requemao

                Jun Du wrote:

                1. Your app overrides OpenDocumentFile() so that it displays any views beside the default.

                Yes, this is done. When I open an existing document (and ONLY then), both views appear and work correctly. It is done this way (just like the sample): CDocument* CSequApp::OpenDocumentFile(LPCTSTR lpszFileName) { CSequDoc* pDoc = (CSequDoc*) CWinApp::OpenDocumentFile(lpszFileName); if (pDoc == NULL) return NULL; CFrameWnd* pNewFrame = m_pEditorTemplate->CreateNewFrame(pDoc, NULL); if (pNewFrame == NULL) return pDoc; m_pEditorTemplate->InitialUpdateFrame(pNewFrame, pDoc); }

                Jun Du wrote:

                1. Let your app to handle file-new command, and call the overriden OpenDocumentFile() in the command handler.

                At present, I have this in the message map: ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) If I understand you correctly, I should change it to... ON_COMMAND(ID_FILE_NEW, OnFileNew) ...and then define the OnFileNew function: void CSequApp::OnFileNew() { CWinApp::OnFileNew(); ... (some call to OpenDocumentFile()) } Is this right? In that case, what's the code I have to add there? I don't think I'll get it right myself...

                J Offline
                J Offline
                Jun Du
                wrote on last edited by
                #7

                Check out this technical article [^]; it describes how OnFileNew() should be overridden. Basically, in case of multiple document templates, don't call base class'

                CWinApp::OnFileNew();

                in CSequApp::OnFileNew(). Add code to retrieve the file name first and then call

                OpenDocumentFile(fileName);

                The resulting code should be similar to CHKBOOK sample code.

                Best, Jun

                R 1 Reply Last reply
                0
                • J Jun Du

                  Check out this technical article [^]; it describes how OnFileNew() should be overridden. Basically, in case of multiple document templates, don't call base class'

                  CWinApp::OnFileNew();

                  in CSequApp::OnFileNew(). Add code to retrieve the file name first and then call

                  OpenDocumentFile(fileName);

                  The resulting code should be similar to CHKBOOK sample code.

                  Best, Jun

                  R Offline
                  R Offline
                  requemao
                  wrote on last edited by
                  #8

                  Jun Du wrote:

                  in CSequApp::OnFileNew(). Add code to retrieve the file name first and then call OpenDocumentFile(fileName);

                  I must be overlooking something. Even after reading the whole tech note and browsing trough the member functions of CDocument and CWinApp, I still haven't found out how to retrieve the file name (which is required by the OpenDocumentFile() function). Can you please point me to the function I need? Manuel M.

                  J 1 Reply Last reply
                  0
                  • R requemao

                    Jun Du wrote:

                    in CSequApp::OnFileNew(). Add code to retrieve the file name first and then call OpenDocumentFile(fileName);

                    I must be overlooking something. Even after reading the whole tech note and browsing trough the member functions of CDocument and CWinApp, I still haven't found out how to retrieve the file name (which is required by the OpenDocumentFile() function). Can you please point me to the function I need? Manuel M.

                    J Offline
                    J Offline
                    Jun Du
                    wrote on last edited by
                    #9

                    The relevant part from the article is: "One common customization of ID_FILE_NEW is to provide a different and more graphical choice of document types. In this case you can implement your own CMyApp::OnFileNew and place it in your message map instead of CWinApp::OnFileNew. There is no need to call the base class implementation." Maybe this is where you find confusing: The base class OnFileNew() doesn't ask for the file name, because it automatically creates one ("New") for you. However, it only displays one view. Without overriding OnFileNew(), you won't get more than one view at the startup. You have to override OnFileNew() and call OpenDocumentFile(), which requires a file name and the file must be created before the call. After reviewing the CHKBOOK sample code carefully, you'll find its OnFileNew() does only two things: 1) Prompt a GUI to ask for the new file name and create the file. 2) Call the overridden OpenDocumentFile() with the file name. It didn't call base class' OnFileNew(), which I think you know why now. So, the code before calling OpenDocumentFile() should be: 1) Constructing a file name (either asking the user or use "New" as the default). 2) Creating that file physically on the current folder. Note that this is how CHKBOOK is implemented and it looks like what we have to do if we use OpenDocumentFile().

                    Best, Jun

                    R 1 Reply Last reply
                    0
                    • J Jun Du

                      The relevant part from the article is: "One common customization of ID_FILE_NEW is to provide a different and more graphical choice of document types. In this case you can implement your own CMyApp::OnFileNew and place it in your message map instead of CWinApp::OnFileNew. There is no need to call the base class implementation." Maybe this is where you find confusing: The base class OnFileNew() doesn't ask for the file name, because it automatically creates one ("New") for you. However, it only displays one view. Without overriding OnFileNew(), you won't get more than one view at the startup. You have to override OnFileNew() and call OpenDocumentFile(), which requires a file name and the file must be created before the call. After reviewing the CHKBOOK sample code carefully, you'll find its OnFileNew() does only two things: 1) Prompt a GUI to ask for the new file name and create the file. 2) Call the overridden OpenDocumentFile() with the file name. It didn't call base class' OnFileNew(), which I think you know why now. So, the code before calling OpenDocumentFile() should be: 1) Constructing a file name (either asking the user or use "New" as the default). 2) Creating that file physically on the current folder. Note that this is how CHKBOOK is implemented and it looks like what we have to do if we use OpenDocumentFile().

                      Best, Jun

                      R Offline
                      R Offline
                      requemao
                      wrote on last edited by
                      #10

                      It now works, thanks to you! I appreciate the effort you have put in explaining this to me and giving me accurate pointers. You have taught me quite something, thank you very much. Manuel M.

                      J 1 Reply Last reply
                      0
                      • R requemao

                        It now works, thanks to you! I appreciate the effort you have put in explaining this to me and giving me accurate pointers. You have taught me quite something, thank you very much. Manuel M.

                        J Offline
                        J Offline
                        Jun Du
                        wrote on last edited by
                        #11

                        You're welcome. Glad it worked out finally.

                        Best, Jun

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups