Click to See Complete Forum and Search --> : Pasting bitmap into a RichEditCtrl


eperales
April 13th, 1999, 02:29 PM
Hi all!
I have a window with a RichEditControl, I want to paste a bitmap into the window, but I always get an Out Of Memory error when calling InsertObject.
Does anybody has some working code to paste a bitmap into a CRichEditCtrl?

void CEWnd::OnEditPaste()
{
// create item from dialog results
COleClientItem* pItem =new COleClientItem((COleDocument*)m_pDocument);


COleDataObject dataobj;
dataobj.AttachClipboard();
dataobj.BeginEnumFormats();
FORMATETC fe;
while(dataobj.GetNextFormat(&fe))
{
TRACE("OnEditPaste -W- cf %x\n",fe.cfFormat);
}
if (!pItem->CreateStaticFromData(&dataobj))
return;
CString strType;
pItem->GetUserType(USERCLASSTYPE_FULL,strType);
TRACE("OnEditPaste -D- name %s\n",strType);

pItem->SetDrawAspect(DVASPECT_CONTENT);
pItem->UpdateLink();

IRichEditOle* pREO=GetRichEditCtrl().GetIRichEditOle();
ASSERT(pREO!= NULL);

REOBJECT reo;
LPOLECLIENTSITE pocs;
pREO->GetClientSite(&pocs);


reo.cbStruct = sizeof(REOBJECT);
reo.poleobj = pItem->m_lpObject;
reo.pstg = pItem->m_lpStorage;
reo.polesite = pocs;
ASSERT(reo.poleobj != NULL);
ASSERT(reo.pstg != NULL);
ASSERT(reo.polesite != NULL);
reo.poleobj->AddRef();
reo.pstg->AddRef();
reo.polesite->AddRef();

reo.sizel.cx = reo.sizel.cy = 0; // let richedit determine initial size
reo.dvaspect = pItem->GetDrawAspect();
reo.dwFlags = REO_RESIZABLE;
reo.dwUser = 0;
reo.cp = REO_CP_SELECTION;

HRESULT hr = pREO->InsertObject(&reo);
if(FAILED(hr))
{
// upon failure throw file exception (item will be cleaned up)
AfxThrowOleException(hr); // always throws OUTOFMEM
return;
}

CHARRANGE cr;
GetRichEditCtrl().GetSel(cr);
cr.cpMin = cr.cpMax -1;
GetRichEditCtrl().SetSel(cr);
}