mfc >> Multiple Views: how to open both views at startup or on new document

by Manuel M » Sun, 10 Sep 2006 22:46:19 GMT

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?


mfc >> Multiple Views: how to open both views at startup or on new document

by Joseph M. Newcomer » Mon, 11 Sep 2006 09:44:11 GMT


In the OnNewDocument handler you could create the second view.
joe



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

mfc >> Multiple Views: how to open both views at startup or on new document

by Manuel M » Mon, 11 Sep 2006 20:09:12 GMT


Thanks for your reply. I'm trying to do that, with this piece of code:

BOOL CSequDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// (...)
InitDocument(); CMDIChildWnd* pNewFrame = (CMDIChildWnd*)
(CSequApp::m_pEditorTemplate->CreateNewFrame(pDoc, NULL));
if (pNewFrame == NULL)
return FALSE; // not created
ASSERT_KINDOF(CMDIChildWnd, pNewFrame);
CSequApp::m_pEditorTemplate->InitialUpdateFrame(pNewFrame, pDoc);
return TRUE;
}

The problem is that I don't know how to get the pointer to the
active document (pDoc). I've browsed through the member
functions of CDocument and CWinApp, and I can't find one
that gives me said pointer. How should I do that?

mfc >> Multiple Views: how to open both views at startup or on new document

by xrxst32 » Mon, 11 Sep 2006 20:47:47 GMT


Try "this" for this document.

mfc >> Multiple Views: how to open both views at startup or on new document

by Joseph M. Newcomer » Mon, 11 Sep 2006 22:34:09 GMT

'this'. You're IN the document!
joe




Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

mfc >> Multiple Views: how to open both views at startup or on new document

by Manuel M » Tue, 12 Sep 2006 00:19:02 GMT


Thank you guys! Now you see how much of a begginer I am.

But now I get a compiler error that looks very strange to me:
C:\(...)\SequDoc.cpp(79) : error C2227: left of '->CreateNewFrame' must
point to class/struct/union
C:\(...)\SequDoc.cpp(83) : error C2227: left of '->InitialUpdateFrame'
must point to class/struct/union

This is the code, with the troublesome lines marked:

BOOL CSequDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
InitDocument();
CMDIChildWnd* pNewFrame = (CMDIChildWnd*)
(CSequApp::m_pEditorTemplate->CreateNewFrame(this, NULL)); // <---
if (pNewFrame == NULL)
return FALSE; // not created
ASSERT_KINDOF(CMDIChildWnd, pNewFrame);
CSequApp::m_pEditorTemplate->InitialUpdateFrame(pNewFrame, this); //
<---

return TRUE;
}

Both pointers are of the correct type, CMultiDocTemplate*. And both
are declared as public in the application class and initialized in
CSequApp::InitInstance() (correctly, I assume, since otherwise
the program could not have worked before, when I wrote the first
post of this thread).

I have tried other ways, and it seems that it's illegal to access those
member variables in CSequApp. If I try to assign the value in this way:
CMultiDocTemplate* pEditorTemplate =
CSequApp::m_pEditorTemplate;
then I get "Illegal reference to data member
CSequApp::m_pEditorTemplate
in a static member function".

And it's no use defining a GetEditorTemplate() function in CSequApp,
because any calls to it seem to be illegal (still don't know why).
Those give me a "illegal call to non-static member function"
error.

It looks like it must be a big conceptual mistake on my side, is it?

mfc >> Multiple Views: how to open both views at startup or on new document

by Joseph M. Newcomer » Tue, 12 Sep 2006 02:17:17 GMT

I would be disinclined to access the app in the CDocument class.

Now that I revisit the problem, I basically overrode CWinApp:OnFileNew and
CWinApp::OnFileOpen in my CWinApp class and created the secondary view there. It has the
advantage that the template is readily accessible and doesn't require any special magic to
get to it.

Note that CSequApp::m_pEditorTemplate is *not* accessing the member variable; this is
erroneous syntax. You would have to write
((CSequApp*)AfxGetApp())->m_pEditorTemplate;
but you should not be including the CWinApp class in the CDocument module (I know
Microsoft puts it there, but the first thing to do is to eliminate it)
joe





Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

mfc >> Multiple Views: how to open both views at startup or on new document

by Manuel M » Tue, 12 Sep 2006 03:11:58 GMT

Done, I've overridden OnFileNew, copy-pasting code from CHKBOOK. It
now works OK:

void CSequApp::OnFileNew()
{
CString strNewFileName;
strNewFileName = "untitled.sequ";

// If file doesn't already exist, then create it.
CFile file;
CFileStatus status;
if (!file.GetStatus(strNewFileName, status))
{
file.Close();
}

// Open the file now that it has been created.
OpenDocumentFile(strNewFileName);
}

The only thing I don't like now is that if I close any of the two
views,
I am promted to save the document, and then again when I close
the other view (if I didn't save it before). But it's not much trouble,
so I'll let it be for the moment.

Thank you very much for your time and effort,
Manuel Mats.

Similar Threads

1. Multiple views Single Document MDI - no splitter views

Hi Folks,

I have a situation where I need 4 views in my application. All the 4
views should co exist and shall be updated independently/
simultaneously.

I searched the forum and all I got was how to create multiple views
for a SDI and for MDI creating multiple document templates and make
them refer to a single document .

I dont know whether creating multiple document templates is a good
solution as I read somewhere it is not an optimal solution.

Is there any simple/optimal method by which I can go on adding a view
for the document? If possible please paste a link to the sample
application.

Thanks in Advance.

Regrads,
JustLikeDat

2. Reuse of view for new Windows in CHtmlView-Back View or Home View

3. When To Initialize View Controls In a Document View MFC Architecture

Hello

I am writing my first Visual C++ MFC Document View application.  My View is derived from CFormView and I have already added some controls to it via the Dialog Editor in the Resource window
For example, I have added two Radio Buttons to the form

My question is, where is the proper place to initialize the controls.  I would like to set one of the radio buttons to checked when the Main Frame is launched.  Would I add a variable to the Document or View object and set it OnNewDocument?  Is there a concensus as to how this should be done

Thanks In Advance
John

4. doc/view 2 documents each with childframe and view

5. looking for sample:multiple view single document in C# or VB

Hi,
I am looking for sample of an application similar to outlook regarding UI

when user select a tree node in the left pane a view is changed in the right
pane

(it does not necessarily support  MFC doc view architecture )

thanks in advance


6. control excel from vc application or add excel view to a multiple document

7. Multiple views Single Document MDI

Hi Folks,

I have a situation where I need 4 views in my application. All the 4
views should co exist and shall be updated independently/
simultaneously.

I searched the forum and all I got was how to create multiple views
for a SDI and for MDI creating multiple document templates and make
them refer to a single document .

I dont know whether creating multiple document templates is a good
solution as I read somewhere it is not an optimal solution.

Is there any simple/optimal method by which I can go on adding a view
for the document? If possible please paste a link to the sample
application.

Thanks in Advance.

Regrads,
JustLikeDat

8. multiple view single document