Insert any HBITMAP (Bitmap) in your RichEdit Control
2011-08-19 17:27
453 查看
http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/
There are a lot of articles in Codeguru or MSDN that discuss how to insert a bitmap file inside the RichEdit control. Those articles are not working when the image is in the resource or within a handle (HBITMAP).
My article is based on the MSDN code "HOWTO: Insert a Bitmap Into an RTF Document Using the RichEdit Control", and the use of IDataObject Interface. When you refer back to the MSDN article, you will find that it uses the OLE API (OleCreateFromFile), which accepts
the name of the file as a string. There are many other ways to create an OLE object (IOleObject). Those OLE APIs are started with the prefix (OleCreate). The one that I will use here is (OleCreateStaticFromData), which allows us to build an Ole object that
contains only a representation without any relative data. The data for sure might be an HBITMAP value that represents an image.
How do you create an OleObject (IOleObject) using the mentioned OLE API? In order for that function to return an IOleObject pointer, you should prepare many objects before invoking it. This includes objects like IDataObject, IStorage, and IOleClientSite. Preparing
the last two objects is the same method shown in the MSDN article. But, what about IDataObject?
There are two methods to get the data object of the HBITMAP. One uses COleDataSource, the MFC implementation for IDataObject interface. The second method is to supply your own implementation of IDataObject. The second one, which I use in the code, is the preferable
method if you are not going to use MFC in your application.
Post a comment
Email Article
Print Article
Share
Articles
The first method uses the following code:
Using the returned lpDataObject and the other objects you can insert the Object in the RichEdit.
The second method is to write your own IOleObject Implementation as shown in the next code:
The previous implementation for IDataObject is specialized for holding HBITMAP handle.
To use the previous implementation to insert any image in the RichEdit, use the static member function CImageDataObject::InsertBitmap. This static function accepts two parameters:
IRichEditOle* pRichEditOle : A pointer to IRichEditOle interface for the RichEdit control. You can use the method GetRichEditOle() in the MFC CRichEditCtrl class to obtain that pointer, or use the following code:
HBITMAP hBitmap : The bitmap handle of the image. The class is responsible of freeing or closing the handle. So, don't close it your self.
For a bonus you will learn how to use high-color images in the toolbar by using CImageList. One of the advantages of using CImageList is that it automatically masks out the background color.
That's it, and enjoy!
There are a lot of articles in Codeguru or MSDN that discuss how to insert a bitmap file inside the RichEdit control. Those articles are not working when the image is in the resource or within a handle (HBITMAP).
My article is based on the MSDN code "HOWTO: Insert a Bitmap Into an RTF Document Using the RichEdit Control", and the use of IDataObject Interface. When you refer back to the MSDN article, you will find that it uses the OLE API (OleCreateFromFile), which accepts
the name of the file as a string. There are many other ways to create an OLE object (IOleObject). Those OLE APIs are started with the prefix (OleCreate). The one that I will use here is (OleCreateStaticFromData), which allows us to build an Ole object that
contains only a representation without any relative data. The data for sure might be an HBITMAP value that represents an image.
How do you create an OleObject (IOleObject) using the mentioned OLE API? In order for that function to return an IOleObject pointer, you should prepare many objects before invoking it. This includes objects like IDataObject, IStorage, and IOleClientSite. Preparing
the last two objects is the same method shown in the MSDN article. But, what about IDataObject?
There are two methods to get the data object of the HBITMAP. One uses COleDataSource, the MFC implementation for IDataObject interface. The second method is to supply your own implementation of IDataObject. The second one, which I use in the code, is the preferable
method if you are not going to use MFC in your application.
Post a comment
Email Article
Print Article
Share
Articles
The first method uses the following code:
STGMEDIUM stgm; stgm.tymed = TYMED_GDI; // Storage medium = HBITMAP handle stgm.hBitmap = hBitmap; stgm.pUnkForRelease = NULL; // Use ReleaseStgMedium COleDataSource *pDataSource = new COleDataSource; pDataSource->CacheData(CF_BITMAP, &stgm); LPDATAOBJECT lpDataObject = (LPDATAOBJECT)pDataSource->GetInterface(&IID_IDataObject);
Using the returned lpDataObject and the other objects you can insert the Object in the RichEdit.
The second method is to write your own IOleObject Implementation as shown in the next code:
class CImageDataObject : IDataObject { public: // This static function accepts a pointer to IRochEditOle // and the bitmap handle. // After that the function insert the image in the current // position of the RichEdit // static void InsertBitmap(IRichEditOle* pRichEditOle, HBITMAP hBitmap); private: ULONG m_ulRefCnt; BOOL m_bRelease; // The data being bassed to the richedit // STGMEDIUM m_stgmed; FORMATETC m_fromat; public: CImageDataObject() : m_ulRefCnt(0) { m_bRelease = FALSE; } ~CImageDataObject() { if (m_bRelease) ::ReleaseStgMedium(&m_stgmed); } // Methods of the IUnknown interface // STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject) { if (iid == IID_IUnknown || iid == IID_IDataObject) { *ppvObject = this; AddRef(); return S_OK; } else return E_NOINTERFACE; } STDMETHOD_(ULONG, AddRef)(void) { m_ulRefCnt++; return m_ulRefCnt; } STDMETHOD_(ULONG, Release)(void) { if (--m_ulRefCnt == 0) { delete this; } return m_ulRefCnt; } // Methods of the IDataObject Interface // STDMETHOD(GetData)(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) { HANDLE hDst; hDst = ::OleDuplicateData(m_stgmed.hBitmap, CF_BITMAP, NULL); if (hDst == NULL) { return E_HANDLE; } pmedium->tymed = TYMED_GDI; pmedium->hBitmap = (HBITMAP)hDst; pmedium->pUnkForRelease = NULL; return S_OK; } STDMETHOD(GetDataHere)(FORMATETC* pformatetc, STGMEDIUM* pmedium ) { return E_NOTIMPL; } STDMETHOD(QueryGetData)(FORMATETC* pformatetc ) { return E_NOTIMPL; } STDMETHOD(GetCanonicalFormatEtc)(FORMATETC* pformatectIn , FORMATETC* pformatetcOut ) { return E_NOTIMPL; } STDMETHOD(SetData)(FORMATETC* pformatetc , STGMEDIUM* pmedium , BOOL fRelease ) { m_fromat = *pformatetc; m_stgmed = *pmedium; return S_OK; } STDMETHOD(EnumFormatEtc)(DWORD dwDirection , IEnumFORMATETC** ppenumFormatEtc ) { return E_NOTIMPL; } STDMETHOD(DAdvise)(FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection) { return E_NOTIMPL; } STDMETHOD(DUnadvise)(DWORD dwConnection) { return E_NOTIMPL; } STDMETHOD(EnumDAdvise)(IEnumSTATDATA **ppenumAdvise) { return E_NOTIMPL; } // Some Other helper functions // void SetBitmap(HBITMAP hBitmap); IOleObject *GetOleObject(IOleClientSite *pOleClientSite, IStorage *pStorage); };
The previous implementation for IDataObject is specialized for holding HBITMAP handle.
To use the previous implementation to insert any image in the RichEdit, use the static member function CImageDataObject::InsertBitmap. This static function accepts two parameters:
IRichEditOle* pRichEditOle : A pointer to IRichEditOle interface for the RichEdit control. You can use the method GetRichEditOle() in the MFC CRichEditCtrl class to obtain that pointer, or use the following code:
::SendMessage((HWND)m_ctlRichText.GetHwnd(), EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
HBITMAP hBitmap : The bitmap handle of the image. The class is responsible of freeing or closing the handle. So, don't close it your self.
For a bonus you will learn how to use high-color images in the toolbar by using CImageList. One of the advantages of using CImageList is that it automatically masks out the background color.
That's it, and enjoy!
相关文章推荐
- Autocomplection in RichEdit Control
- VMware 12安装CentOS 6.9时出现:The centos disc was not found in any of your drives.Please insert the centos disc and press OK to retry
- Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]
- Csharp:user WebControl Read Adobe PDF Files In Your Web Browser
- Embed an HTML control in your own window using plain C
- MySQL5.7出现Your password has expired. To log in you must change it using a client that supports expir
- MFC Initilization of General Service Control about InitCommonControls和InitCommonControlsEx(MFC 通用控件的初始化InitCommonControls和InitCo
- Your password has expired. To log in you must change it using a client that supports expired passwords.
- Your password has expired. To log in you must change it using a client that supports expired passwor
- An extender can't be in a different UpdatePanel than the control it extends
- Control character in cookie value, consider BASE64 encoding your value
- 33.Some non-DBA users in your database have been granted ANY TABLE system privileges and they
- Embed an HTML control in your own window using plain C
- Check your file - it most likely has keys in far negative or positive timeline.
- This may be due to a corruption of the heap, which indicates a bug in *.exe or any of the DLLs it has loaded.
- Connection for controluser as defined in your configuration failed.
- Your password has expired. To log in you must change it using a client that supports expired passwor
- 关于跨进程使用回调函数的研究:以跨进程获取Richedit中RTF流为例(在Delphi 初始化每一个TWinControl 对象时,将会在窗体 的属性(PropData)中加入一些标志,DLL的HInstance的值与HOST 进程的HInstance并不一致)
- 完美解决Your Firefox profile cannot be loaded. It may be missing or inaccessible
- 在RichEdit中插入Bitmap