CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Controls >> Rich Edit Control


Insert any HBITMAP (Bitmap) in your RichEdit Control
Rating:

Hani Atassi (view profile)
August 14, 2001


(continued)



Environment: VC6 SP5, Win2000 SP2

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.

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!

Downloads

Download demo project - 55 Kb
Download source - 3 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
How to implement a progress bar in RichEdit control like MSN messenger? - Zhoudi (04/04/2005)
Memory leak fixed. Please update your code - mateia (02/15/2005)
When you delete an image from the edit, how can u free the memory? - skyvense (08/26/2004)
Addin masked bitmaps? - allanmb (06/07/2004)
Send EM_GETOLEINTERFACE as often as possible - Legacy CodeGuru (02/13/2004)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES