MFCで手動でビューを作成する

5956 ワード

MDIプログラムでは、新規作成とオープンメニューがシステムに付属しており、ON_を通過できない場合がありますFILE_NEWはビューを表示し、あるタイプのビューは1つしか表示されないことが多い.
では、システムが持っているONを抜きにしてFILE_NEW命令、私たちは自分で書きます.
 
プログラムが起動すると、空のビューを新規に作成したくありません.大きなフレームワークだけでいいです.
appのInitInstance関数では、次の行をコメントします.
 //         、DDE、          
//CCommandLineInfo cmdInfo;
//ParseCommandLine(cmdInfo);


//
// /RegServer、/Register、/Unregserver /Unregister , FALSE。
//if (!ProcessShellCommand(cmdInfo))
// return FALSE;

メインフレームを作成するコードは次のとおりです.
 //     MDI     
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
pMainFrame->ShowWindow(SW_SHOW);
pMainFrame->UpdateWindow();

プレゼンテーションのためにビューを作成するには、メニューにメニュー項目を追加し、WinAppでももちろんMainFrameで処理できます.コードは簡単です
void CMainFrame::OnTestCreate()
{
// TODO:
CDocTemplate* pDocTemplate = ((CxxApp*)AfxGetApp())->m_pDocTemplate;
ASSERT_VALID(pDocTemplate);
pDocTemplate->OpenDocumentFile(NULL);

}

私はmfcの中のソースコードをめくったことがあります.主な処理は:
CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
BOOL bMakeVisible)
{
CDocument* pDocument = CreateNewDocument();
if (pDocument == NULL)
{
TRACE(traceAppMsg, 0, "CDocTemplate::CreateNewDocument returned NULL.
");
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT_VALID(pDocument);

BOOL bAutoDelete = pDocument->m_bAutoDelete;
pDocument->m_bAutoDelete = FALSE; // don't destroy if something goes wrong
CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);
pDocument->m_bAutoDelete = bAutoDelete;
if (pFrame == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
delete pDocument; // explicit delete on error
return NULL;
}
ASSERT_VALID(pFrame);

if (lpszPathName == NULL)
{
// create a new document - with default document name
SetDefaultTitle(pDocument);

// avoid creating temporary compound file when starting up invisible
if (!bMakeVisible)
pDocument->m_bEmbedded = TRUE;

if (!pDocument->OnNewDocument())
{
// user has be alerted to what failed in OnNewDocument
TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE.
");
pFrame->DestroyWindow();
return NULL;
}

// it worked, now bump untitled count
m_nUntitledCount++;
}
else
{
// open an existing document
CWaitCursor wait;
if (!pDocument->OnOpenDocument(lpszPathName))
{
// user has be alerted to what failed in OnOpenDocument
TRACE(traceAppMsg, 0, "CDocument::OnOpenDocument returned FALSE.
");
pFrame->DestroyWindow();
return NULL;
}
pDocument->SetPathName(lpszPathName);
}

InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
return pDocument;
}

一部の機能が適切でない場合は、この関数を参照して変更することもできます.