📄 embedole.cpp
字号:
// Close the Notes session
if (IsNotesInitialized)
Session.Term();
cout << "All Done. Hit return to exit: ";
cin.getline(CommandBuf, 50);
// All done.
return(0);
}
void EmbedInNotes (
LNNotesSession & Session,
char * pDocPath,
char * pDbFilename,
char * pDbServer,
char * pOleUserType,
char * pFilePath,
LNOLEGUID * pApiGuid,
char * pOleProgID,
LNRichText * pGraphicContainer
) {
BOOL throwFailure = FALSE;
LNDatabase Db;
LNDocument Doc;
LNText TitleText;
LNRichText BodyItem;
LNRTCursor Cursor;
LNRTCursor graphicCursor;
LNRTOLEObject rtOleObject;
LNGraphic newGraphic;
// Make the error handler throw all errors encountered during execution.
LNSetThrowAllErrors(TRUE);
try
{
// Try to fetch the new graphic for the OLE object
pGraphicContainer->GetCursor (&graphicCursor);
graphicCursor.GotoFirst (LNRTTYPE_GRAPHIC, &newGraphic);
// Get the specified database.
Session.GetDatabase(pDbFilename, &Db, pDbServer);
// Open it.
Db.Open();
// Create a new document.
Db.CreateDocument (&Doc);
// Open the document.
Doc.Open();
cout << "Created new document." << endl;
// Create the Title.
TitleText.SetValue ("Embedded OLE Object");
Doc.CreateItem ("Subject", TitleText, LNITEMFLAGS_SUMMARY);
// Create the Body.
Doc.CreateItem("Body", &BodyItem);
// Obtain a cursor.
BodyItem.GetCursor(&Cursor);
// Add the opening text
BodyItem.Insert ("Embedding OLE object from file ", &Cursor);
BodyItem.Insert (pDocPath, &Cursor);
BodyItem.Insert (" (Application ", &Cursor);
BodyItem.Insert (pOleUserType, &Cursor);
BodyItem.Insert (")", &Cursor);
BodyItem.StartNewParagraph (&Cursor);
// An embedded OLE object has 3 components in the C++ API:
// 1) The LNRTOLEObject is the rich text component.
// 2) The LNOLEObject contains the OLE activation information.
// 3) The actual OLE object content, stored as file attachments.
// This sample uses LNRichText::CreateOLEObject () to construct
// all these components.
// Embed the OLE object.
BodyItem.CreateOLEObject (
pFilePath,
*pApiGuid,
LNOLECLIPBOARDFORMAT_METAFILE,
pOleProgID,
pOleUserType,
&Cursor,
"",
&rtOleObject);
// If we have a new graphic, insert it
if (!newGraphic.IsNull ())
{
rtOleObject.SetGraphic (newGraphic);
}
BodyItem.StartNewParagraph (&Cursor);
// Add the trailing text.
BodyItem.Insert ("End of body", &Cursor);
// Update the document (and write the BodyItem to the database).
Doc.Save();
// Close the document, free item memory.
Doc.Close();
cout << "Embedded OLE object from file " << pDocPath <<
" using application " << pOleUserType << endl;
}
// Error handler. If an error occurred, get the text of the error
// message and display it.
catch (LNSTATUS lnerror)
{
char ErrorBuf[ERR_BUF_SIZE];
ErrorBuf[0] = '\0';
LNGetErrorMessage(lnerror, ErrorBuf, ERR_BUF_SIZE);
cout << "Error: " << ErrorBuf << endl;
throwFailure = TRUE;
}
// Close the database, free document memory.
Db.Close();
if (throwFailure)
throw (CFailure ());
}
// Get the file name (if any) for an OLE object, and
// convert the name from UNICODE to multi-byte characters.
void GetMultiByteTempFileName (
LPMALLOC lpMalloc,
LPSTORAGE lpStorage,
char * pBuffer,
int bufferSize
) {
STATSTG StatStg; // OLE storage info
// Fetch the name of the temp file
if (FAILED(lpStorage->Stat (&StatStg, STATFLAG_DEFAULT)))
throw ("Stat failed; serious OLE error!!");
// Convert that name to multi-byte form
WideCharToMultiByte (
CP_ACP,
0,
StatStg.pwcsName,
-1,
pBuffer,
bufferSize,
NULL,
NULL);
// Release the storage for the OLE pathname
lpMalloc->Free (StatStg.pwcsName);
}
// Copy the contents of an OLE object into
// the destination IStorage.
void CopyObjectToIStorage (
LPOLEOBJECT lpOleObject,
LPSTORAGE lpDestStorage
) {
BOOL throwFailure = FALSE;
LPPERSISTSTORAGE lpPersistStorage = NULL; // Pointer to OLE object's IPersistStorage interface
// Get the IPersistStorage interface for this object
if (FAILED(lpOleObject->QueryInterface (
IID_IPersistStorage,
(void FAR * FAR *) &lpPersistStorage)))
{
throw ("Cannot obtain IPersistStorage interface");
}
try
{
// Copy the server's data to the substorage
if (FAILED(OleSave (lpPersistStorage, lpDestStorage, TRUE)))
throw ("Storing object data failed");
// Provide the expected SaveCompleted() call
if (FAILED(lpPersistStorage->SaveCompleted (NULL)))
throw ("SaveCompleted failed; OLE server error!!");
}
catch (char * pMessage)
{
cout << "*** Error: " << pMessage << endl;
throwFailure = TRUE;
}
catch (CFailure)
{
throwFailure = TRUE;
}
//Release the IPersistStorage interface
if (((LPPERSISTSTORAGE) NULL) != lpPersistStorage)
{
lpPersistStorage->Release ();
lpPersistStorage = NULL;
}
if (throwFailure)
throw (CFailure ());
}
// Try to get the graphic representation
// of the OLE object.
// Note that a failure in this routine shouldn't
// cause an error in the application!
void GetGraphic (
LPOLEOBJECT lpOleObject,
LNRichText * pGraphicContainer
) {
LPDATAOBJECT lpDataObject = NULL;
FORMATETC formatEtc;
STGMEDIUM stgMedium;
try
{
if (!OleIsRunning (lpOleObject))
if (FAILED (OleRun (lpOleObject)))
throw ("Cannot run OLE object");
// Try to get the IDataObject interface
if (FAILED (lpOleObject->QueryInterface (IID_IDataObject, (void **) (&lpDataObject))))
throw ("Object does not support IDataObject");
// // Set up the FORMATETC structure for the metafile
// formatEtc.cfFormat = CF_METAFILEPICT;
// formatEtc.ptd = NULL;
// formatEtc.dwAspect = DVASPECT_CONTENT;
// formatEtc.lindex = -1;
// formatEtc.tymed = TYMED_MFPICT;
//
// // Clear the STGMEDIUM structure
// memset ((void *) (&stgMedium), 0, sizeof (stgMedium));
//
// if ((SUCCEEDED (lpDataObject->GetData (&formatEtc, &stgMedium))) &&
// (NULL != stgMedium.hGlobal))
// {
// // Got the metafile - convert to Notes format
// ConvertMetafileToNotes (stgMedium, &metaFile);
//
// if (NULL != stgMedium.pUnkForRelease)
// (stgMedium.pUnkForRelease)->Release ();
// else
// DeleteMetaFile (stgMedium.hMetaFilePict);
// }
// Set up the FORMATETC structure for the bitmap
formatEtc.cfFormat = CF_BITMAP;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
formatEtc.tymed = TYMED_GDI;
// Clear the STGMEDIUM structure
memset ((void *) (&stgMedium), 0, sizeof (stgMedium));
if ((SUCCEEDED (lpDataObject->GetData (&formatEtc, &stgMedium))) &&
(NULL != stgMedium.hGlobal))
{
// Got the bitmap - convert to Notes format
ConvertBitmapToLNGraphic (&stgMedium, pGraphicContainer);
if (NULL != stgMedium.pUnkForRelease)
(stgMedium.pUnkForRelease)->Release ();
else
DeleteObject (stgMedium.hBitmap);
}
}
catch (char * pMessage)
{
cout << ">>> Warning: " << pMessage << endl;
}
catch (CFailure)
{
;
}
// Release resources
if (((LPDATAOBJECT) NULL) != lpDataObject)
{
lpDataObject->Release ();
lpDataObject = NULL;
}
}
// Since Notes won't convert a bitmap directly to rich text,
// let's use the C++ API facility to call an import library
// to do the job for us!
//
// If unable to convert the bitmap to an LNGraphic, the
// LNGraphic is left uninitialized.
void ConvertBitmapToLNGraphic (
STGMEDIUM * pStgMedium,
LNRichText * pGraphicContainer
) {
char tempFilePath [] = "d:\\apipp\\samples\\embedole\\temp.bmp";
PBITMAPINFO pBitmapInfo;
LNRTCursor cursor;
HDC hDc;
pBitmapInfo = CreateBitmapInfoStruct (pStgMedium->hBitmap);
if (((PBITMAPINFO) NULL) == pBitmapInfo)
return;
hDc = CreateCompatibleDC (NULL);
if (((HDC) NULL) == hDc)
return;
if (!CreateBMPFile (tempFilePath, pBitmapInfo, pStgMedium->hBitmap, hDc))
{
DeleteDC (hDc);
return;
}
try
{
pGraphicContainer->GetEndCursor (&cursor);
pGraphicContainer->Import (tempFilePath, &cursor);
}
catch (LNSTATUS lnerror)
{
char ErrorBuf[ERR_BUF_SIZE];
ErrorBuf[0] = '\0';
LNGetErrorMessage(lnerror, ErrorBuf, ERR_BUF_SIZE);
cout << "Error: " << ErrorBuf << endl;
}
DeleteDC (hDc);
DeleteFile (tempFilePath);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -