Monday, May 16, 2011

CreateCompatibleDC() example c c++

Following code fragment shows an example of CreateCompatibleDC() function. CreateCompatibleDC(hdc) function is used to create a device context(DC) that is compatible with the given dc(hdc). Most of the time, a compatible DC is created to hold a bitmap for a loaded image from another source. The image in the DC is then bitblit into the screen DC.[CreateCompatibleDC() example]


        {
            PNGWrapper        *pPNG = new PNGWrapper("E:/Research/libpng/debug/testpng.png");
            HDC                hDC = GetDC(hWnd);
            HDC                hMemDC = CreateCompatibleDC(hDC);
            HBITMAP            hOldBMP, hNewBMP;

            pPNG->readINDEX();
            //pPNG->convert(PNG_PIXEL_BGRA_32);
           
            // Display Image onto Screen for validation.
            //
            hNewBMP = CreateBitmap(pPNG->getWidth(), pPNG->getHeight(), 1, 32, pPNG->getBuffer());
            hOldBMP = (HBITMAP)SelectObject(hMemDC, hNewBMP);

            BitBlt(hDC, 0, 0, pPNG->getWidth(), pPNG->getHeight(), hMemDC, 0, 0, SRCCOPY);

            SelectObject(hMemDC, hOldBMP);
            DeleteObject(hNewBMP);
            DeleteDC(hMemDC);
            ReleaseDC(hWnd, hDC);
            delete pPNG;
        }