YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


VC++ Article: Insert a bitmap file - HBITMAP into a Rich Edit Control, use COleDataSource, CF_BITMAP, and STGMEDIUM

By Hani Atassi

 

Environment: VC++ 6 SP5, Win2000 SP2

There are a lot of articles in ucancode 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 mucancode.nets out the background color.

That's it, and enjoy!

Downloads

Download demo project - 55 Kb
Download source - 3 Kb

 

 

Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net