d_wzrdv_z
May 24th, 1999, 11:21 PM
I don't use the document class in my apps - it just sits there, and usually doesn't get in the way (I do use the view class). However, currently I am doing some processing in the mainframe's oncreate method, and if I return FALSE to exit the application, I get an annoying afxdialog saying 'Failed to create empty document'; Any solution would really be appreciated.
Thanks,
Henry Venn
JohnCz
June 1st, 1999, 01:39 PM
Create App without a document.
John Cz
d_wzrdv_z
June 2nd, 1999, 09:45 PM
So :
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate (
IDR_MAINFRAME,
RUNTIME_CLASS (CMyDoc),
RUNTIME_CLASS (CMyFrameWnd),
RUNTIME_CLASS (CMyView));
AddDocTemplate (pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine (cmdInfo);
if (!ProcessShellCommand (cmdInfo))
return FALSE;
return TRUE;
is the code which creates the document, and I don't want a document: I can't just remove the AddDocTemplate (pDocTemplate); statement and I still require the view class to be linked to the main frame.
d_wzrdv_z
June 2nd, 1999, 09:56 PM
Found it in the Stingray MFC FAQ :
How do I keep my application from creating a new document at startup?
Add this call:
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing
just before the call to ProcessShellCommand in the app's InitInstance.
lechner-cos1@kaman.com, email, 1/6/96
d_wzrdv_z
JohnCz
June 2nd, 1999, 10:34 PM
What I meant was to create application that does NOT use document template. All work that framework does for you would have to be done by you.
Your solution you will produce an error in creation of the main frame window and you will get assertion error.
The changesyou should make:
Your InitInstance should look like this:
BOOL CYourApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
SetRegistryKey(_T("John’s Demo Applications"));
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object.
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
// create and load the frame with its resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been initialized, so show and update it.
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
You can use View class created for you by App Wizard, but remove GetDocument declaration and implementation, and from OnDraw remove line: ASSERT_VALID(pDoc);
In your MainFrame class declare pointer of the view class type, and initialize it to NULL.
I called this pointer m_pView. Since view class has protected constructor we will have to use different approach creating view object.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//this is part you have to add:
/**************************************************************************/
// create a view
CRuntimeClass* pView = RUNTIME_CLASS(CNoDocView); //I called view class CnoDocView, but replace it with yours class name
m_pView = (CNoDocView*)pView->CreateObject();
if (!m_pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("Failed to create view window\n");
return -1;
}
/**************************************************************************/
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
Using class wizard insert virtual function OnCmdMsg:
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// view will process commands
if (m_pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
// otherwise, do default handling
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
I hope I did not forget some stupid detail.
If you have any further problem drop me e-mail.
Good luck,
John Cz