VC++ 6.0, MFC: Two different views attached to same document.
-
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?
-
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?
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
-
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
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).
-
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).
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
-
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
Jun Du wrote:
- 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:
- 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... -
Jun Du wrote:
- 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:
- 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... -
Jun Du wrote:
- 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:
- 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...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
-
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
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.
-
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.
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
-
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
-
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.