Guidance
指路人
g.yi.org
software / rapidq / examples / gui / Edit & Richedit / REImage / REImage.cpp

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
/***************************************
**
** Images in RichEdit Controls
**
** All Free Source Code
** Code From Microsoft KB
** and Hani Atassi from Code Guru
**     (atassi @arabteam200.com)
** 
** Author: Elliott Sales de Andrade
** E-mail: quantum_analyst @hotmail.com
**
**
***************************************/

#include <windows.h>
#include <ole2.h>
#include <richedit.h>
#include <richole.h>
#include "test2.hpp"

// Code From MSDN Article KB 220844
// Just all wrapped up for C use
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;

	// Create the embedded object

	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;

	// To convert an ANSI string lpszA to a Unicode string lpszFileName

	int nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, NULL, NULL);

	LPWSTR lpszFileName = new WCHAR[nLen];

	::MultiByteToWideChar(CP_ACP, 0, lpszA, -1, lpszFileName, nLen);

	// use lpszFileName here

	// fill in FORMATETC struct FORMATETC formatEtc; lpFormatEtc = &formatEtc;
	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;
	};

	// 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)
			return 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);

	// 3 . Ready !
	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; //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;

	// 4 . Insert
	m_pRichEditOle->InsertObject(&reobject);

	// 5. CleanUp
	m_lpObject->Release();
	m_lpClientSite->Release();
	m_lpStorage->Release();
	lpLockBytes->Release();
	m_pRichEditOle->Release();
	delete []lpszFileName;

	return 1;
};

// ImageDataObject.h: Impementation for IDataObject Interface to be used 
//					     in inserting bitmap to the RichEdit Control.
// Author : Hani Atassi  (atassi@arabteam2000.com)
//////////////////////////////////////////////////////////////////////
// Static member functions
//////////////////////////////////////////////////////////////////////
// Added dwFlags for Eu, Elliott Sales de Andrade
int CImageDataObject::InsertBitmap(IRichEditOle* pRichEditOle, HBITMAP hBitmap, DWORD dwFlags)
{
	SCODE sc;

	// Get the image data object
	//
	CImageDataObject *pods = new CImageDataObject;
	LPDATAOBJECT lpDataObject;
	pods->QueryInterface(IID_IDataObject, (void **)&lpDataObject);

	pods->SetBitmap(hBitmap);

	// Get the RichEdit container site
	//
	IOleClientSite *pOleClientSite;	
	pRichEditOle->GetClientSite(&pOleClientSite);

	// Initialize a Storage Object
	//
	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;
	}

	// The final ole object which will be inserted in the richedit control
	//
	IOleObject *pOleObject; 
	pOleObject = pods->GetOleObject(pOleClientSite, pStorage);
	if (pOleObject == NULL)
		return 0;

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(pOleObject, TRUE);

	// Now Add the object to the RichEdit 
	//
	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;

	// Insert the bitmap at the current location in the richedit control
	//
	pRichEditOle->InsertObject(&reobject);

	// Release all unnecessary interfaces
	//
	pOleObject->Release();
	pOleClientSite->Release();
	pStorage->Release();
	lpLockBytes->Release();
	lpDataObject->Release();

	return 1;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void CImageDataObject::SetBitmap(HBITMAP hBitmap)
{
	STGMEDIUM stgm;
	stgm.tymed = TYMED_GDI;					// Storage medium = HBITMAP handle		
	stgm.hBitmap = hBitmap;
	stgm.pUnkForRelease = NULL;				// Use ReleaseStgMedium

	FORMATETC fm;
	fm.cfFormat = CF_BITMAP;				// Clipboard format = CF_BITMAP
	fm.ptd = NULL;							// Target Device = Screen
	fm.dwAspect = DVASPECT_CONTENT;			// Level of detail = Full content
	fm.lindex = -1;							// Index = Not applicaple
	fm.tymed = TYMED_GDI;					// Storage medium = HBITMAP handle

	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;
}

// My Code which Wraps CImageDataObject::InsertBitmap();
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;
	// 1. Duplicate and Insert
	HANDLE hDup;
	hDup = ::OleDuplicateData(hBitmap, CF_BITMAP, NULL);
	sc = CImageDataObject::InsertBitmap(m_pRichEditOle, (HBITMAP)hDup, dwFlags);
	if (sc == 0)
		return 0;

	// 2. CleanUp
	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;
};
掌柜推荐
 
 
¥1,380.00 ·
 
 
¥1,450.00 ·
 
 
¥477.00 ·
 
 
¥264.00 ·
 
 
¥1,280.00 ·
 
 
¥489.00 ·
© Mon 2024-11-25  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2002-10-30 20:15:38