#include <windows.h>
#include <ole2.h>
#include <richedit.h>
#include <richole.h>
#include "test2.hpp"
extern "C" __declspec(dllexport) int InsertBitmapFromFile(HWND rEdit, char *lpszA, DWORD dwFlags) {
LPRICHEDITOLE m_pRichEditOle;
LPOLEOBJECT m_lpObject;
LPSTORAGE m_lpStorage;
LPOLECLIENTSITE m_lpClientSite;
::SendMessage((HWND)rEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
if (m_pRichEditOle == NULL)
return 0;
LPLOCKBYTES lpLockBytes = NULL;
CLSID clsid = CLSID_NULL;
OLERENDER render = OLERENDER_NONE;
CLIPFORMAT cfFormat = CF_BITMAP;
LPFORMATETC lpFormatEtc = new FORMATETC;
SCODE sc;
sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
if (sc != S_OK)
return 0;
sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
if (sc != S_OK)
return 0;
int nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, NULL, NULL);
LPWSTR lpszFileName = new WCHAR[nLen];
::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, lpszFileName, nLen);
lpFormatEtc->cfFormat = cfFormat;
lpFormatEtc->ptd = NULL;
lpFormatEtc->dwAspect = DVASPECT_CONTENT;
lpFormatEtc->lindex = -1;
lpFormatEtc->tymed = TYMED_FILE;
m_pRichEditOle->GetClientSite(&m_lpClientSite);
sc = ::OleCreateFromFile(clsid,
(LPCOLESTR)lpszFileName,
IID_IUnknown, render, lpFormatEtc, m_lpClientSite, m_lpStorage,
(void**)&m_lpObject);
if (sc != S_OK) {
char lpMsgBuf[10000];
::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 10000, NULL );
MessageBox( 0, lpMsgBuf,"Error", MB_OK );
return 0;
};
if (m_lpObject != NULL) {
LPUNKNOWN lpUnk = m_lpObject;
lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
lpUnk->Release();
if (m_lpObject == NULL)
return 0;
};
OleSetContainedObject(m_lpObject, TRUE);
REOBJECT reobject;
ZeroMemory(&reobject, sizeof(REOBJECT));
reobject.cbStruct = sizeof(REOBJECT);
sc = m_lpObject->GetUserClassID(&clsid);
if (sc != S_OK)
return 0;
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = dwFlags; 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;
m_pRichEditOle->InsertObject(&reobject);
m_lpObject->Release();
m_lpClientSite->Release();
m_lpStorage->Release();
lpLockBytes->Release();
m_pRichEditOle->Release();
delete []lpszFileName;
return 1;
};
int CImageDataObject::InsertBitmap(IRichEditOle* pRichEditOle, HBITMAP hBitmap, DWORD dwFlags)
{
SCODE sc;
CImageDataObject *pods = new CImageDataObject;
LPDATAOBJECT lpDataObject;
pods->QueryInterface(IID_IDataObject, (void **)&lpDataObject);
pods->SetBitmap(hBitmap);
IOleClientSite *pOleClientSite;
pRichEditOle->GetClientSite(&pOleClientSite);
IStorage *pStorage;
LPLOCKBYTES lpLockBytes = NULL;
sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
if (sc != S_OK)
return 0;
sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &pStorage);
if (sc != S_OK)
{
lpLockBytes->Release();
lpLockBytes = NULL;
return 0;
}
IOleObject *pOleObject;
pOleObject = pods->GetOleObject(pOleClientSite, pStorage);
if (pOleObject == NULL)
return 0;
OleSetContainedObject(pOleObject, TRUE);
REOBJECT reobject;
ZeroMemory(&reobject, sizeof(REOBJECT));
reobject.cbStruct = sizeof(REOBJECT);
CLSID clsid;
sc = pOleObject->GetUserClassID(&clsid);
if (sc != S_OK)
return 0;
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = dwFlags;
reobject.poleobj = pOleObject;
reobject.polesite = pOleClientSite;
reobject.pstg = pStorage;
pRichEditOle->InsertObject(&reobject);
pOleObject->Release();
pOleClientSite->Release();
pStorage->Release();
lpLockBytes->Release();
lpDataObject->Release();
return 1;
}
void CImageDataObject::SetBitmap(HBITMAP hBitmap)
{
STGMEDIUM stgm;
stgm.tymed = TYMED_GDI; stgm.hBitmap = hBitmap;
stgm.pUnkForRelease = NULL;
FORMATETC fm;
fm.cfFormat = CF_BITMAP; fm.ptd = NULL; fm.dwAspect = DVASPECT_CONTENT; fm.lindex = -1; fm.tymed = TYMED_GDI;
this->SetData(&fm, &stgm, TRUE);
}
IOleObject *CImageDataObject::GetOleObject(IOleClientSite *pOleClientSite, IStorage *pStorage)
{
SCODE sc;
IOleObject *pOleObject;
sc = ::OleCreateStaticFromData(this, IID_IOleObject, OLERENDER_FORMAT,
&m_fromat, pOleClientSite, pStorage, (void **)&pOleObject);
if (sc != S_OK)
return NULL;
return pOleObject;
}
extern "C" __declspec(dllexport) int InsertBitmapFromData(HWND rEdit, HBITMAP hBitmap, DWORD dwFlags) {
LPRICHEDITOLE m_pRichEditOle;
::SendMessage((HWND)rEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
if (m_pRichEditOle == NULL)
return 0;
SCODE sc;
HANDLE hDup;
hDup = ::OleDuplicateData(hBitmap, CF_BITMAP, NULL);
sc = CImageDataObject::InsertBitmap(m_pRichEditOle, (HBITMAP)hDup, dwFlags);
if (sc == 0)
return 0;
m_pRichEditOle->Release();
return 1;
};
int DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch(fdwReason) {
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
};
return 1;
};
|
|