visual-c++bitmapmfcdrawocx

How to draw an OCX to a CBitmap (MFC, c++)


How can I draw an OCX (I do have the sources) to an CBitmap-Object or something alike?

Background: My client creates PDF-Documents and part of these documents is an Output from an OCX. The PDF-lib-Interface has a Method to put an Image from an CBitmap-Object to the PDF-Page. So what i want to do ist let the Program create an CBitmap-Object, pass that to the OCX to let it draw its content onto it and then pass the he CBitmap to the PDF-library to get it into the document. So the main question is: how to draw my ocx into a CBitmap-Object?

I'm using Visual C++, Windows, MFC/ATL. Thanks a lot


Solution

  • actually I didn't manage to render the OXC to a CBitmap (just got a black box drawn) but rendering into an ATL::CImage and making a CBitmap out of it worked:

        ATL::CImage* CPrnBasePrinter::DrawBeamerToImage(CSPCListView* pListViewWithBeamer, const CRect& rect4Beamer)
        {
            ASSERT(pListViewWithBeamer != nullptr);
    
            auto* pRetVal = new CImage();
    
            pRetVal->Create(rect4Beamer.Width(), rect4Beamer.Height(), 24);
            HDC hdcImage = pRetVal->GetDC();
    
            //Draw Control to CImage
            pListViewWithBeamer->DrawBeamerToDC(HandleToLong(hdcImage),
                rect4Beamer.left, rect4Beamer.top, rect4Beamer.right, rect4Beamer.bottom);
    
            pRetVal->ReleaseDC();
            return pRetVal;
        }
    
        void CPrnBasePrinter::DrawImageFromCImage(
            const ATL::CImage* pImage, const CRect& rect) const
        {
            CBitmap* pbmp2Print = CBitmap::FromHandle(*pImage);
    
            // Get the size of the bitmap
            BITMAP bmpInfo;
            pbmp2Print->GetBitmap(&bmpInfo);
    
    
            //virtual - Draws the CBitmap to an Printer-DC or a PDF-Document
            DrawImageFromLoadedBitmap(pbmp2Print, &bmpInfo, rect);
        }
    
        void CPrnBasePrinter::Draw()
        {
            //m_pListviewDataSource is an OCX capable of drawing itself into a given DC
            ATL::CImage* pBeamerImage = DrawBeamerToImage(m_pListviewDataSource, CRect(0, 0, 100, 50));
    
            if (pBeamerImage != nullptr){
                DrawImageFromCImage(pBeamerImage, CRect(0, 0, 100, 50));
                delete pBeamerImage;
            }
        }