Transparent ATL Controls 
by Tom Armstrong and Mark Nelson


Listing One
BOOL CTransCtrl::OnEraseBkgnd(CDC* pDC)
{
    CWnd*  pWndParent = GetParent();
    POINT  pt;
    pt.x = pt.y = 0;
    MapWindowPoints(pWndParent, &pt, 1);
    OffsetWindowOrgEx(pDC->m_hDC, pt.x, pt.y, &pt);
    ::SendMessage( pWndParent->m_hWnd, WM_ERASEBKGND, (WPARAM)pDC->m_hDC, 0 );
    SetWindowOrgEx( pDC->m_hDC, pt.x, pt.y, NULL );
    return 1;
}


Listing Two
<HTML>
<HEAD>
<TITLE>Test page for TransCtl</TITLE>
</HEAD>
<BODY BACKGROUND="background.gif">
<OBJECT ID="TransCtl" WIDTH=128 HEIGHT=128
     CLASSID="CLSID:B25D9AF5-E760-11D0-A052-00A0247B7657"
     CODEBASE="TransparentControl.dll">
     <PARAM NAME="ImageFile" VALUE="http://www.widgetware.com/image01.bmp">
</OBJECT>


Listing Three
static HRESULT Download( T* pT, ATL_PDATAAVAILABLE pFunc, BSTR bstrURL,
                     IUnknown* pUnkContainer = NULL, BOOL bRelative = FALSE)
{
 CComObject<COurBindStatusCallback<T> > *pbsc;
 HRESULT hRes = CComObject<COurBindStatusCallback<T> >::CreateInstance(&pbsc);
 if (FAILED(hRes))
      return hRes;
 return pbsc->StartAsyncDownload(pT,pFunc,bstrURL,pUnkContainer,bRelative);
}

Listing Four
enum tagREADYSTATE
{
   READYSTATE_UNINITIALIZED = 0,
   READYSTATE_LOADING       = 1,
   READYSTATE_LOADED        = 2,
   READYSTATE_INTERACTIVE   = 3,
   READYSTATE_COMPLETE      = 4
}  READYSTATE;



Listing Five
// If we're still downloading the image, draw some text and return
if ( m_nReadyState != READYSTATE_COMPLETE )
{
   DrawString( di.hdcDraw, 
               "Downloading bitmap...",
               &rc );
   return S_OK;
}


Listing Six
// If this is the first time through OnDraw, we need to setup the DIB
if (! m_dib.IsInitialized() )
{
   m_dib.SetBitmapInfoHeader( m_pbBuffer );
   m_dib.Create( di.hdcDraw );
   m_dib.SetBits( m_pbBuffer );
}

Listing Seven
HDC hdcMask = ::CreateCompatibleDC( di.hdcDraw );
HBITMAP bmMask = ::CreateBitmap( columns, rows, 1, 1, NULL );
HBITMAP hOldMaskBitmap = (HBITMAP) SelectObject( hdcMask, bmMask );
SetBkColor( HDC( m_dib ), RGB( 255, 255, 255 ) );
SetTextColor( HDC( m_dib ), RGB( 0, 0, 0 ) );
BitBlt( hdcMask, 0, 0, columns, rows, HDC( m_dib ), 0, 0, SRCCOPY );


Listing Eight
BitBlt(di.hdcDraw, rc.left, rc.top, columns, rows, HDC(m_dib),0,0,SRCINVERT);
BitBlt(di.hdcDraw, rc.left, rc.top, columns, rows, hdcMask, 0, 0, SRCAND );
BitBlt(di.hdcDraw, rc.left, rc.top, columns, rows, HDC(m_dib),0,0,SRCINVERT);




2


