CDocument, creating without View
Alexander Stigsen -- ccc4864@vip.cybercity.dk
Friday, June 07, 1996
Win95, VC++ v4.0
How do I create a Document without an attached View?
I need to first load the document and do some processing on it and then let
the user select which View to use from the Toolbar.
I appreciate any help I can get..
Alexander Stigsen
Dale Wilson -- dale@drant1.dra.com
Monday, June 10, 1996
> Date: Friday, June 07, 1996 10:55PM
> Win95, VC++ v4.0
> How do I create a Document without an attached View?
With great difficulty, if at all!
> I need to first load the document and do some processing on it and then
let
> the user select which View to use from the Toolbar.
How about creating a helper class which has the guts of the Document. Read
that class in; analyze it; and when the user selects a view, create the real
doc/view and hand the pre-read data to the doc.
Alternatively, how about creating a dummy view which goes away when the real
view is selected (similar, maybe to the Document1 created by Word when you
start it cold.)
Dale@dra.com
Roger Onslow -- Roger_Onslow@compsys.com.au
Tuesday, June 11, 1996
>How do I create a Document without an attached View?
Simple, just like creating any other class
eg: on heap: CMyDoc* pMyDoc = new CMyDoc;
eg: on stack: CMyDoc mydoc;
Documents don't have to be created through a
doc template (unless you want to simultaneously
create view with a frame)
Roger
craigtt@ccmail.PE-Nelson.COM
Tuesday, June 11, 1996
It actually can be done with a reasonable effort. Follow the code in
DOCMULTI.CPP in CMultiDocTemplate::OpenDocumentFile.
You have to handle the menu items for opening the document yourself so
you can create the document and call its OnOpenDocument function to
read it into memory. Once in memory, you can do your preliminary
processing and ask the user to select what kind of view he wants.
Once the user has selected the view type, use a document template of
the correct type and call the member CreateNewFrame to create the view
and then InitialUpdateFrame.
You can derive your own class from CMultiDocTemplate and add a
function to handle the view creation given the already loaded
document.
Tim Craig
PE-Nelson
______________________________ Reply Separator _________________________________
Subject: RE: CDocument, creating without View
Author: mfc-l@netcom.com at SMTPLINK-PEN
Date: 6/10/96 10:13 PM
> Date: Friday, June 07, 1996 10:55PM
> Win95, VC++ v4.0
> How do I create a Document without an attached View?
With great difficulty, if at all!
> I need to first load the document and do some processing on it and then
let
> the user select which View to use from the Toolbar.
How about creating a helper class which has the guts of the Document. Read
that class in; analyze it; and when the user selects a view, create the real
doc/view and hand the pre-read data to the doc.
Alternatively, how about creating a dummy view which goes away when the real
view is selected (similar, maybe to the Document1 created by Word when you
start it cold.)
Dale@dra.com
Dan Opperman -- DOpperman@msn.com
Thursday, June 13, 1996
To create a document without views, you have to tell MFC not to delete the
document when it does not have any views:
CMyDocument::CMyDocument()
{
m_bAutoDelete = FALSE;
}
You'll need to remember to delete the document yourself. You can set
m_bAutoDelete to TRUE before closing the last view.
Then, you can create documents directly
class CMyApp : public CWinApp
{
...
private:
CDocument* m_pDocument;
CTemplate* m_pTemplate;
...
};
BOOL CMyApp::InitInstance()
{
...
// save the template
m_pTemplate = new CMultiDocTemplate(
IDR_MYTYPE,
RUNTIME_CLASS(CMyDocument),
RUNTIME_CLASS(CMyFrame),
RUNTIME_CLASS(CMyView));
if (!m_pTemplate)
return FALSE;
AddDocTemplate(m_pTemplate);
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
// create a document without a view
m_pDocument = m_pTemplate->CreateNewDocument();
if (!m_pDocument)
return FALSE;
return m_pDocument->OnNewDocument();
}
It will probably make sense to override CWinApp::OnFileNew and
CWinApp::OnFileOpen since you don't want their default behavior. You can use
one of these to add the views:
void CMyApp::OnFileOpen()
{
ASSERT(m_pDocument);
ASSERT(m_pTemplate);
// add a view to the document
CFrameWnd* pFrame = m_pTemplate->CreateNewFrame(m_pDocument, 0);
if (!pFrame)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return;
}
pFrame->InitialUpdateFrame(m_pDocument, TRUE);
}
You could extend this to support different types of views by storing pointers
to each of the templates. Then, you the appropriate template when you call
CMultiDocTemplate::CreateNewFrame. Obviously, the code in OnFileOpen is what
you'll want to repeat in your OnToolbarButton handlers.
Hope this helps,
Daniel Opperman
DOpperman@msn.com
----------
From: owner-mfc-l@netcom.com on behalf of Dale Wilson
Sent: Monday, June 10, 1996 10:18 AM
To: mfc-l
Subject: RE: CDocument, creating without View
> Date: Friday, June 07, 1996 10:55PM
> Win95, VC++ v4.0
> How do I create a Document without an attached View?
With great difficulty, if at all!
> I need to first load the document and do some processing on it and then
let
> the user select which View to use from the Toolbar.
How about creating a helper class which has the guts of the Document. Read
that class in; analyze it; and when the user selects a view, create the real
doc/view and hand the pre-read data to the doc.
Alternatively, how about creating a dummy view which goes away when the real
view is selected (similar, maybe to the Document1 created by Word when you
start it cold.)
Dale@dra.com
| Вернуться в корень Архива
|