Reset existing HBITMAP as desktop background (Win32)

I wish to create a transparent window over the desktop.
For that purpose I've created an HDC with a background of the desktop (created HBITMAP of the desktop and applied it to my HDC), and invoked UpdateLayeredWindow.

So far, so good.
for performance issues i need to keep a persistent GDI+ object, meaning my HDC and HBITMAP need to stay the same handles between paintings (assuming the desktop DC didn't change), same as in this question.

In the first painting iteration all goes well. in the second painting iteration, since the HDC and HBITMAP haven't changed, I repaint on the existing HDC, meaning i get double images (the background is not erased).

Here's a code example of what I'm doing:

bool SomeUI::Draw()
{
    BLENDFUNCTION blend = {0};
    POINT ptPos = {0};
    SIZE sizeWnd = {0};
    POINT ptSrc = {0};
    BOOL bUpdate = FALSE;

    // Get the client rect
    RECT rctWindow;
    bool bGot = GetWindowRect(rctWindow);
    if (!bGot)
        return false;

    // Get the desktop's device context
    HDC hDCDesktop = GetDC(NULL);
    if (!hDCDesktop)
        return false;

    int nWidth = abs(rctWindow.right - rctWindow.left);
    int nHeight = abs(rctWindow.bottom - rctWindow.top);

    // Create 32Bit bitmap to apply PNG transparency
    VOID *ppvBits = NULL;
    BITMAPINFO BitmapInfo = {0};
    BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    BitmapInfo.bmiHeader.biWidth = nWidth;
    BitmapInfo.bmiHeader.biHeight = nHeight;
    BitmapInfo.bmiHeader.biPlanes = 1;
    BitmapInfo.bmiHeader.biBitCount = 32;
    BitmapInfo.bmiHeader.biCompression = BI_RGB;

    HBITMAP hBmp = CreateDIBSection(hDCDesktop, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);
    if (!hBmp || hBmp==(HBITMAP)ERROR_INVALID_PARAMETER)
        goto releaseHandles;

    // Create a compatible DC and select the newly created bitmap
    if (!m_hDC)
    {
        m_hDC = CreateCompatibleDC(hDCDesktop);
        if (!m_hDC)
            goto releaseHandles;

        SelectObject(m_hDC, hBmp);
    }
    else
    {
        ///////////////////////////////////////////////////////////////////////
        //
        // The problem lies here, this is where I need to reset the HBITMAP 
        // according to the desktop here (to have a transparent DC to work on)
        //
        ///////////////////////////////////////////////////////////////////////
    }

    // The drawing logic
    bool bInnerDraw = Draw(m_hDC);
    if (!bInnerDraw)
        goto releaseHandles;

    // Call UpdateLayeredWindow
    blend.BlendOp = AC_SRC_OVER;
    blend.SourceConstantAlpha = 255;
    blend.AlphaFormat = AC_SRC_ALPHA;
    sizeWnd.cx = nWidth;
    sizeWnd.cy = nHeight;
    ptPos.x = rctWindow.left;
    ptPos.y = rctWindow.top;
    bUpdate = UpdateLayeredWindow(m_hWnd, hDCDesktop, &ptPos, &sizeWnd, m_hDC, &ptSrc, 0, &blend, ULW_ALPHA);
    if (!bUpdate)
        goto releaseHandles;

releaseHandles:
    // releasing handles
}

Any ideas?

Upvotes: 2

Views: 784

Answers (1)

Found the Answer:

In order to reset the persistent HBITMAP, (reminder: it needs to stay the same handle), we'll set the desktop background of that area to a temporary HBITMAP and copy it to the persistent HBITMAP.
To achieve that (copying from one HBITMAP to the other), We'll create a temporary HDC and select the temporary HBITMAP to it, and copy the temporary HDC to the persistent HDC, usint BitBlt

Here's the code:

        hBmpTemp = CreateDIBSection(hDCDesktop, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);
        if (!hBmpTemp || hBmpTemp==(HBITMAP)ERROR_INVALID_PARAMETER)
            goto releaseHandles;

        HDC hTempDC = CreateCompatibleDC(NULL);
        if (!hTempDC)
            goto releaseHandles;

        SelectObject(hTempDC, hBmpTemp);

        ::BitBlt(m_hPersistentDC, 0, 0, nWidth, nHeight, hTempDC, rctWindow.left, rctWindow.top, SRCCOPY);

        ::DeleteDC(hTempDC);

Upvotes: 2

Related Questions