|
-
November 22nd, 2008, 07:51 AM
#1
Adding pictures to richtextboxes through OLE
I am currently trying to add pictures into a RichTextBox usig pure win32 api(no MFC). I found the following code.
The problem: if i call the same function to add more than 1 picture, it fails and has no effect.
Eg AddPic(pic1);
AddPic(pic2);
Global variables:
LPRICHEDITOLE m_pRichEditOle;
LPOLEOBJECT m_lpObject;
LPSTORAGE m_lpStorage;
LPOLECLIENTSITE m_lpClientSite;
HWND hwnd; //rcb handle
CODE:
Code:
bool CreateFromFile(LPCTSTR lpszFileName)
{
USES_CONVERSION;
LPLOCKBYTES lpLockBytes = NULL;
CLSID clsid = CLSID_NULL;
OLERENDER render = OLERENDER_DRAW;
CLIPFORMAT cfFormat = 0;
LPFORMATETC lpFormatEtc = NULL;
m_pRichEditOle=NULL;
m_lpObject=NULL;
m_lpStorage=NULL;
m_lpClientSite=NULL;
SendMessage(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
SCODE sc;
sc = CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
if (sc != S_OK)
MessageBox(0,L"err",L"",0);
sc = StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
if (sc != S_OK)
{
if(lpLockBytes->Release() != 0)
MessageBox(0,L"err",L"",0);
lpLockBytes = NULL;
MessageBox(0,L"err",L"",0);;
}
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = &formatEtc;
lpFormatEtc->cfFormat = cfFormat;
lpFormatEtc->ptd = NULL;
lpFormatEtc->dwAspect = DVASPECT_CONTENT;
lpFormatEtc->lindex = -1;
lpFormatEtc->tymed = TYMED_NULL;
// attempt to create the object
m_pRichEditOle->GetClientSite(&m_lpClientSite);
sc = OleCreateFromFile(clsid, T2COLE(lpszFileName),
IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite, m_lpStorage,
(void**)&m_lpObject);
if (sc != S_OK)
MessageBox(0,L"err",L"",0);;
// m_lpObject is currently an IUnknown, convert to IOleObject
if (m_lpObject != NULL)
{
LPUNKNOWN lpUnk = m_lpObject;
lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
lpUnk->Release();
if (m_lpObject == NULL)
MessageBox(0,L"err",L"",0);;
}
// 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);
return TRUE;
}
void AddPic(LPCTSTR lpszFileName)
{
REOBJECT reobject;
CreateFromFile(lpszFileName);
ZeroMemory(&reobject, sizeof(REOBJECT));
reobject.cbStruct = sizeof(REOBJECT);
CLSID clsid;
SCODE sc = m_lpObject->GetUserClassID(&clsid);
if (sc != S_OK)
MessageBox(0,L"err",L"",0);
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_RESIZABLE | REO_BELOWBASELINE;
reobject.dwUser = 0;
reobject.poleobj = m_lpObject;
reobject.polesite = m_lpClientSite;
reobject.pstg = m_lpStorage;
SIZEL sizel;
sizel.cx = sizel.cy = 0;
reobject.sizel = sizel;
SendMessage(hwnd, EM_SETSEL, 0, -1);
DWORD dwStart, dwEnd;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
SendMessage(hwnd, EM_SETSEL, dwEnd+1, dwEnd+1);
m_pRichEditOle->InsertObject(&reobject);
if (m_lpObject)
{
m_lpObject->Release();
m_lpObject = NULL;
}
if (m_lpStorage)
{
m_lpStorage->Release();
m_lpStorage = NULL;
}
if (m_lpClientSite)
{
m_lpClientSite->Release();
m_lpClientSite = NULL;
}
if(m_pRichEditOle)
{
m_pRichEditOle->Release();
m_pRichEditOle = NULL;
}
}
Appreciate any help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|