CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    How to bitblt transparently ??

    Anybody have a good example on how to bitblt bitmaps transparently using GDI ?


  2. #2
    Join Date
    Mar 1999
    Location
    St. Louis
    Posts
    45

    Re: How to bitblt transparently ??

    This came out of CMenuSpawn, available from this site.

    void CMenuSpawn::TransparentBlt(CDC * pDestDc, int x, int y, int w, int h, CBitmap * pBmp, int sx, int sy, COLORREF crTransparent)
    {
    CDC memDC, maskDC, tempDC;
    maskDC.CreateCompatibleDC(pDestDc);
    CBitmap maskBitmap;

    //add these to store return of SelectObject() calls
    CBitmap* pOldMemBmp = NULL;
    CBitmap* pOldMaskBmp = NULL;

    memDC.CreateCompatibleDC(pDestDc);
    tempDC.CreateCompatibleDC(pDestDc);
    CBitmap bmpImage;
    bmpImage.CreateCompatibleBitmap( pDestDc, w, h);
    pOldMemBmp = memDC.SelectObject( &bmpImage );

    CBitmap * oldBmp = tempDC.SelectObject(pBmp);

    memDC.BitBlt( 0,0,w, h, &tempDC, sx, sy, SRCCOPY );

    // Create monochrome bitmap for the mask
    maskBitmap.CreateBitmap(w, h, 1, 1, NULL);
    pOldMaskBmp = maskDC.SelectObject( &maskBitmap );
    memDC.SetBkColor(crTransparent);

    // Create the mask from the memory DC
    maskDC.BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY);

    memDC.SetBkColor(RGB(0,0,0));
    memDC.SetTextColor(RGB(255,255,255));
    memDC.BitBlt(0, 0, w, h, &maskDC, 0, 0, SRCAND);

    // Set the foreground to black. See comment above.
    pDestDc->SetBkColor(RGB(255,255,255));
    pDestDc->SetTextColor(RGB(0,0,0));
    pDestDc->BitBlt(x, y, w, h, &maskDC, 0, 0, SRCAND);

    // Combine the foreground with the background
    pDestDc->BitBlt(x, y, w, h, &memDC, 0, 0, SRCPAINT);

    tempDC.SelectObject(oldBmp);
    if (pOldMaskBmp) maskDC.SelectObject( pOldMaskBmp );
    if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
    }

    // this variation does a color replace (useful for scrolling graphics)
    void CMenuSpawn::BackgroundBlt(CDC * pDestDc, int x, int y, int w, int h, CBitmap * pBmp, int sx, int sy, COLORREF crTransparent)
    {
    COLORREF backGround = ::GetSysColor(COLOR_BTNFACE);

    CDC memDC, maskDC, tempDC;
    maskDC.CreateCompatibleDC(pDestDc);
    CBitmap maskBitmap;

    //add these to store return of SelectObject() calls
    CBitmap* pOldMemBmp = NULL;
    CBitmap* pOldMaskBmp = NULL;

    memDC.CreateCompatibleDC(pDestDc);
    tempDC.CreateCompatibleDC(pDestDc);
    CBitmap bmpImage;
    bmpImage.CreateCompatibleBitmap( pDestDc, w, h);
    pOldMemBmp = memDC.SelectObject( &bmpImage );

    CBitmap * oldBmp = tempDC.SelectObject(pBmp);

    memDC.BitBlt( 0,0,w, h, &tempDC, sx, sy, SRCCOPY );

    // Create monochrome bitmap for the mask
    maskBitmap.CreateBitmap(w, h, 1, 1, NULL);
    pOldMaskBmp = maskDC.SelectObject( &maskBitmap );
    memDC.SetBkColor(crTransparent);

    // Create the mask from the memory DC
    maskDC.BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY);

    memDC.SetBkColor(backGround);
    memDC.SetTextColor(RGB(255,255,255));
    memDC.BitBlt(0, 0, w, h, &maskDC, 0, 0, SRCAND);

    // Set the foreground to black. See comment above.
    pDestDc->SetBkColor(RGB(255,255,255));
    pDestDc->SetTextColor(RGB(0,0,0));
    pDestDc->BitBlt(x, y, w, h, &maskDC, 0, 0, SRCAND);

    // Combine the foreground with the background
    pDestDc->BitBlt(x, y, w, h, &memDC, 0, 0, SRCPAINT);

    tempDC.SelectObject(oldBmp);
    if (pOldMaskBmp) maskDC.SelectObject( pOldMaskBmp );
    if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured