64,648
社区成员
发帖
与我相关
我的任务
分享
bool const RichEditA::InsertObject( HWND hRichEdit, LPCTSTR pszFileName, My_FORMATETC * pFTEtc )
{
/** @brief 见: MSDN: http://msdn.microsoft.com/en-us/library/dd387916%28v=vs.85%29.aspx */
HRESULT hr;
/** @brief Get the IRichEditOle interface. */
LPRICHEDITOLE pRichEditOle;
MySendMessage( hRichEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichEditOle );
if ( NULL == pRichEditOle )
{
return false;
}
/** @brief Create structured storage. */
LPLOCKBYTES pLockBytes = NULL;
hr = ::CreateILockBytesOnHGlobal( NULL, TRUE, &pLockBytes );
if ( FAILED( hr ) )
{
return false;
}
LPSTORAGE pStorage;
hr = ::StgCreateDocfileOnILockBytes( pLockBytes,
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE,
0, &pStorage );
if ( FAILED( hr ) )
{
return false;
}
/** @brief Set up the data format. */
//LPFORMATETC
My_FORMATETC * formatEtc = pFTEtc;
formatEtc->cfFormat = 0;
//formatEtc->cfFormat = CF_BITMAP;
formatEtc->ptd = NULL;
formatEtc->dwAspect = DVASPECT_CONTENT;
formatEtc->lindex = -1;
formatEtc->tymed = TYMED_NULL; //TYMED_NULL;
/** @brief Get a pointer to the display site. */
LPOLECLIENTSITE pClientSite;
hr = pRichEditOle->GetClientSite(&pClientSite);
if ( FAILED( hr ) )
{
return false;
}
/** @brief Create the object and retrieve its IUnknown interface. */
LPUNKNOWN pUnk;
CLSID clsid = CLSID_NULL;
QString strFile( "c:\\scroll.bmp" );
//LPCOLESTR oleStrFile = reinterpret_cast<const wchar_t*>( pszFileName );
LPCOLESTR oleStrFile = reinterpret_cast<LPCOLESTR>( strFile.utf16() );
hr = ::OleCreateFromFile( clsid, oleStrFile, IID_IUnknown, OLERENDER_DRAW, (LPFORMATETC)formatEtc, pClientSite, pStorage, (void**)&pUnk );
pClientSite->Release();
if ( FAILED( hr ) )
{
return false;
}
/** @brief Get the IOleObject interface to the object. */
LPOLEOBJECT pObject;
hr = pUnk->QueryInterface( IID_IOleObject, (void**)&pObject );
pUnk->Release();
if ( FAILED( hr ) )
{
return false;
}
hr = ::OleCreateFromFile( clsid, oleStrFile, IID_IUnknown, OLERENDER_DRAW, (LPFORMATETC)formatEtc, pClientSite, pStorage, (void**)&pUnk );
/** @brief To ensure that references are counted correctly, notify the object that it is contained. */
::OleSetContainedObject(pObject, true );
/** @brief Set up object info. */
REOBJECT reobject; // = { sizeof(REOBJECT)};
memset( &reobject, 0, sizeof( reobject ) );
hr = pObject->GetUserClassID( &clsid );
if ( FAILED( hr ) )
{
pObject->Release();
return false;
}
reobject.cbStruct = sizeof( reobject );
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_DYNAMICSIZE | REO_BELOWBASELINE /*| REO_DONTNEEDPALETTE */ | REO_GETMETAFILE | REO_STATIC; //REO_RESIZABLE | REO_BELOWBASELINE | REO_DYNAMICSIZE;
reobject.dwUser = 0;
reobject.poleobj = pObject;
reobject.polesite = pClientSite;
reobject.pstg = pStorage;
SIZEL sizel = { 0 };
reobject.sizel = sizel;
/** @brief Move the caret to the end of the text and add a carriage return. */
MySendMessage(hRichEdit, EM_SETSEL, 0, -1);
DWORD dwStart = 0, dwEnd =0;
MySendMessage(hRichEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
MySendMessage(hRichEdit, EM_SETSEL, dwEnd+1, dwEnd+1);
MySendMessage(hRichEdit, EM_REPLACESEL, TRUE, (WPARAM)L"\n");
/** @brief Insert the object. */
hr = pRichEditOle->InsertObject(&reobject);
/** @brief Clean up. */
pObject->Release();
pRichEditOle->Release();
if ( FAILED( hr ) )
{
return false;
}
return true;
}
42: // Object flags
43: #define REO_NULL (0x00000000L) // No flags
44: #define REO_READWRITEMASK (0x0000003FL) // Mask out RO bits
45: #define REO_DONTNEEDPALETTE (0x00000020L) // Object doesn't need palette
46: #define REO_BLANK (0x00000010L) // Object is blank
47: #define REO_DYNAMICSIZE (0x00000008L) // Object defines size always
48: #define REO_INVERTEDSELECT (0x00000004L) // Object drawn all inverted if sel
49: #define REO_BELOWBASELINE (0x00000002L) // Object sits below the baseline
50: #define REO_RESIZABLE (0x00000001L) // Object may be resized
51: #define REO_LINK (0x80000000L) // Object is a link (RO)
52: #define REO_STATIC (0x40000000L) // Object is static (RO)
53: #define REO_SELECTED (0x08000000L) // Object selected (RO)
54: #define REO_OPEN (0x04000000L) // Object open in its server (RO)
55: #define REO_INPLACEACTIVE (0x02000000L) // Object in place active (RO)
56: #define REO_HILITED (0x01000000L) // Object is to be hilited (RO)
57: #define REO_LINKAVAILABLE (0x00800000L) // Link believed available (RO)
58: #define REO_GETMETAFILE (0x00400000L) // Object requires metafile (RO)