我查看了一下 VIEWRICH.CPP 的代码,就是 CRichEditView 的实现代码,难怪 MFC 默认生成的文档视图结构(选择 CRichEditView 作为视图基类),找不到那些菜单的实现代码,原来都在 CRichEditView 里.
我看了下 ID_OLE_INSERT_NEW, OnInsertObject 的实现,就是那个"插入对象"的菜单的实现,实现的是标准的插入对象对话框,用的是 COleInsertDialog ,但是怎么样才能在基于对话款的程序中使用它呢,对话框中有 CRichEditCtrl,难道非得要用文档视图结构?
不用标准的 COleInsertDialog 插入对象也可以,但是感觉就是不舒服,怎样才能在基于对话框的程序中使用 COleInsertDialog 实现对象的插入与显示?//附 CRichEditView::OnInsertObject() 实现代码:void CRichEditView::OnInsertObject()
{
// Invoke the standard Insert Object dialog box to obtain information
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return; CWaitCursor wait; CRichEditCntrItem* pItem = NULL;
TRY
{
// create item from dialog results
pItem = GetDocument()->CreateClientItem();
pItem->m_bLock = TRUE;
if (!dlg.CreateItem(pItem))
{
pItem->m_bLock = FALSE;
AfxThrowMemoryException(); // any exception will do
} HRESULT hr = InsertItem(pItem);
pItem->UpdateItemType(); pItem->m_bLock = FALSE; if (hr != NOERROR)
AfxThrowOleException(hr); // if insert new object -- initially show the object
if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
pItem->DoVerb(OLEIVERB_SHOW, this);
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE);
}
END_CATCH
}
我看了下 ID_OLE_INSERT_NEW, OnInsertObject 的实现,就是那个"插入对象"的菜单的实现,实现的是标准的插入对象对话框,用的是 COleInsertDialog ,但是怎么样才能在基于对话款的程序中使用它呢,对话框中有 CRichEditCtrl,难道非得要用文档视图结构?
不用标准的 COleInsertDialog 插入对象也可以,但是感觉就是不舒服,怎样才能在基于对话框的程序中使用 COleInsertDialog 实现对象的插入与显示?//附 CRichEditView::OnInsertObject() 实现代码:void CRichEditView::OnInsertObject()
{
// Invoke the standard Insert Object dialog box to obtain information
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return; CWaitCursor wait; CRichEditCntrItem* pItem = NULL;
TRY
{
// create item from dialog results
pItem = GetDocument()->CreateClientItem();
pItem->m_bLock = TRUE;
if (!dlg.CreateItem(pItem))
{
pItem->m_bLock = FALSE;
AfxThrowMemoryException(); // any exception will do
} HRESULT hr = InsertItem(pItem);
pItem->UpdateItemType(); pItem->m_bLock = FALSE; if (hr != NOERROR)
AfxThrowOleException(hr); // if insert new object -- initially show the object
if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
pItem->DoVerb(OLEIVERB_SHOW, this);
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE);
}
END_CATCH
}
或者直接同时使用CDocument,需要熟悉MFC代码
你可以研究一下COleDocument的代码,它里面有一个CObList m_docItemList;是保存OleItem的数据的
我建议你还是使用Document/View结构,否则有相当多的工作要做