📄 olecli1.cpp
字号:
}
// all items are "contained" -- this makes our reference to this object
// weak -- which is needed for links to embedding silent update.
OleSetContainedObject(m_lpObject, TRUE);
// considered loaded at this point
m_nItemState = loadedState;
}
CATCH_ALL(e)
{
Release(); // release the object just in case
ASSERT_VALID(this);
DELETE_EXCEPTION(e);
return FALSE;
}
END_CATCH_ALL
// set state to loaded
ASSERT(m_nItemState != emptyState);
// otherwise no errors, return success!
ASSERT_VALID(this);
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////
// COleClientItem create API variants
BOOL COleClientItem::CreateFromClipboard(
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get clipboard contents
COleDataObject dataObject;
if (!dataObject.AttachClipboard())
return FALSE;
// create from IDataObject
BOOL bResult = CreateFromData(&dataObject, render, cfFormat, lpFormatEtc);
ASSERT_VALID(this);
return bResult;
}
BOOL COleClientItem::CreateLinkFromClipboard(
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get clipboard contents
COleDataObject dataObject;
if (!dataObject.AttachClipboard())
return FALSE;
// create from IDataObject
BOOL bResult = CreateLinkFromData(&dataObject, render, cfFormat, lpFormatEtc);
ASSERT_VALID(this);
return bResult;
}
BOOL COleClientItem::CreateStaticFromClipboard(
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get clipboard contents
COleDataObject dataObject;
if (!dataObject.AttachClipboard())
return FALSE;
// create from IDataObject
BOOL bResult = CreateStaticFromData(&dataObject, render, cfFormat,
lpFormatEtc);
ASSERT_VALID(this);
return bResult;
}
// Creation from IDataObject (used for drag-drop)
BOOL COleClientItem::CreateFromData(COleDataObject* pDataObject,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the object
LPOLECLIENTSITE lpClientSite = GetClientSite();
LPDATAOBJECT lpDataObject = pDataObject->GetIDataObject(FALSE);
SCODE sc = ::OleCreateFromData(lpDataObject, IID_IUnknown, render,
lpFormatEtc, lpClientSite, m_lpStorage, (LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
BOOL COleClientItem::CreateLinkFromData(COleDataObject* pDataObject,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the link
LPOLECLIENTSITE lpClientSite = GetClientSite();
LPDATAOBJECT lpDataObject = pDataObject->GetIDataObject(FALSE);
SCODE sc = ::OleCreateLinkFromData(lpDataObject, IID_IUnknown,
render, lpFormatEtc, lpClientSite, m_lpStorage, (LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
BOOL COleClientItem::CreateStaticFromData(COleDataObject* pDataObject,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the link
LPOLECLIENTSITE lpClientSite = GetClientSite();
LPDATAOBJECT lpDataObject = pDataObject->GetIDataObject(FALSE);
SCODE sc = ::OleCreateStaticFromData(lpDataObject, IID_IUnknown,
render, lpFormatEtc, lpClientSite, m_lpStorage, (LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
// Creation from files (in OLE 1.0, the user did this through packager)
BOOL COleClientItem::CreateFromFile(LPCTSTR lpszFileName, REFCLSID clsid,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
USES_CONVERSION;
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the object
LPOLECLIENTSITE lpClientSite = GetClientSite();
SCODE sc = ::OleCreateFromFile(clsid, T2COLE(lpszFileName),
IID_IUnknown, render, lpFormatEtc, lpClientSite, m_lpStorage,
(LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
BOOL COleClientItem::CreateLinkFromFile(LPCTSTR lpszFileName,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
USES_CONVERSION;
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the link
LPOLECLIENTSITE lpClientSite = GetClientSite();
SCODE sc = ::OleCreateLinkToFile(T2COLE(lpszFileName),
IID_IUnknown, render, lpFormatEtc, lpClientSite, m_lpStorage,
(LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
// create from class name (for insert item dialog)
BOOL COleClientItem::CreateNewItem(REFCLSID clsid,
OLERENDER render, CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_pDocument != NULL);
ASSERT(lpFormatEtc == NULL ||
AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
// get storage for the object via virtual function call
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = _AfxFillFormatEtc(lpFormatEtc, cfFormat, &formatEtc);
// attempt to create the object
LPOLECLIENTSITE lpClientSite = GetClientSite();
SCODE sc = ::OleCreate(clsid, IID_IUnknown,
render, lpFormatEtc, lpClientSite, m_lpStorage, (LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// More advanced creation
BOOL COleClientItem::CreateCloneFrom(const COleClientItem* pSrcItem)
{
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT_VALID(pSrcItem);
ASSERT(m_pDocument != NULL);
// create storage for the item
m_dwItemNumber = GetNewItemNumber();
GetItemStorage();
ASSERT(m_lpStorage != NULL);
// save the object first
LPPERSISTSTORAGE lpPersistStorage =
QUERYINTERFACE(pSrcItem->m_lpObject, IPersistStorage);
ASSERT(lpPersistStorage != NULL);
SCODE sc = ::OleSave(lpPersistStorage, m_lpStorage, FALSE);
lpPersistStorage->SaveCompleted(NULL);
lpPersistStorage->Release();
if (sc != S_OK)
{
// failed the save, do not attempt to create clone
m_scLast = sc;
return FALSE;
}
// get information on the view advise type
ASSERT(pSrcItem->m_lpViewObject != NULL);
DWORD dwAspect;
IAdviseSink* pAdviseSink;
pAdviseSink = NULL;
VERIFY(pSrcItem->m_lpViewObject->GetAdvise(&dwAspect, NULL, &pAdviseSink) ==
S_OK);
if( pAdviseSink != NULL )
{
RELEASE(pAdviseSink);
}
// then load the new object from the new storage
LPOLECLIENTSITE lpClientSite = GetClientSite();
sc = ::OleLoad(m_lpStorage, IID_IUnknown,
lpClientSite, (LPLP)&m_lpObject);
BOOL bResult = FinishCreate(sc);
ASSERT_VALID(this);
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// Storage for COleClientItem objects (memory and compound files)
BOOL COleClientItem::IsModified() const
{
SCODE sc;
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
// get IPersistStorage interface, and call IsDirty
LPPERSISTSTORAGE lpPersistStorage =
QUERYINTERFACE(m_lpObject, IPersistStorage);
if( lpPersistStorage != NULL )
{
sc = lpPersistStorage->IsDirty();
lpPersistStorage->Release();
}
else
{
LPPERSISTSTREAMINIT lpPersistStreamInit;
lpPersistStreamInit = QUERYINTERFACE( m_lpObject, IPersistStreamInit );
if( lpPersistStreamInit != NULL )
{
sc = lpPersistStreamInit->IsDirty();
lpPersistStreamInit->Release();
}
else
{
LPPERSISTSTREAM lpPersistStream;
lpPersistStream = QUERYINTERFACE( m_lpObject, IPersistStream );
if( lpPersistStream != NULL )
{
sc = lpPersistStream->IsDirty();
lpPersistStream->Release();
}
else
{
sc = E_NOINTERFACE;
}
}
}
// S_OK == S_TRUE, therefore object dirty!
return sc == S_OK || FAILED(sc);
}
void COleClientItem::GetItemStorageFlat()
{
ASSERT_VALID(this);
ASSERT(m_lpStorage == NULL);
ASSERT(m_lpLockBytes == NULL);
SCODE sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &m_lpLockBytes);
if (sc != S_OK)
AfxThrowOleException(sc);
ASSERT(m_lpLockBytes != NULL);
sc = ::StgCreateDocfileOnILockBytes(m_lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
if (sc != S_OK)
{
VERIFY(m_lpLockBytes->Release() == 0);
m_lpLockBytes = NULL;
AfxThrowOleException(sc);
}
ASSERT(m_lpStorage != NULL);
ASSERT_VALID(this);
}
void COleClientItem::ReadItemFlat(CArchive& ar)
{
ASSERT_VALID(this);
ASSERT(m_lpStorage == NULL);
ASSERT(m_lpLockBytes == NULL);
// read number of bytes in the ILockBytes
DWORD dwBytes;
ar >> dwBytes;
// allocate enough memory to read entire block
HGLOBAL hStorage = ::GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, dwBytes);
if (hStorage == NULL)
AfxThrowMemoryException();
LPVOID lpBuf = ::GlobalLock(hStorage);
ASSERT(lpBuf != NULL);
DWORD dwBytesRead = ar.Read(lpBuf, dwBytes);
::GlobalUnlock(hStorage);
// throw exception in case of partial object
if (dwBytesRead != dwBytes)
{
::GlobalFree(hStorage);
AfxThrowArchiveException(CArchiveException::endOfFile);
}
SCODE sc = CreateILockBytesOnHGlobal(hStorage, TRUE, &m_lpLockBytes);
if (sc != S_OK)
{
::GlobalFree(hStorage);
AfxThrowOleException(sc);
}
ASSERT(m_lpLockBytes != NULL);
ASSERT(::StgIsStorageILockBytes(m_lpLockBytes) == S_OK);
sc = ::StgOpenStorageOnILockBytes(m_lpLockBytes, NULL,
STGM_SHARE_EXCLUSIVE|STGM_READWRITE, NULL, 0, &m_lpStorage);
if (sc != S_OK)
{
VERIFY(m_lpLockBytes->Release() == 0);
m_lpLockBytes = NULL;
// ILockBytes::Release will GlobalFree the hStorage
AfxThrowOleException(sc);
}
// attempt to load the object from the storage
LPUNKNOWN lpUnk = NULL;
sc = ::OleLoad(m_lpStorage, IID_IUnknown, GetClientSite(),
(LPLP)&lpUnk);
CheckGeneral(sc);
ASSERT(lpUnk != NULL);
m_lpObject = QUERYINTERFACE(lpUnk, IOleObject);
lpUnk->Release();
if (m_lpObject == NULL)
AfxThrowOleException(E_OUTOFMEMORY);
ASSERT_VALID(this);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -