void CFriendWnd::OnPaint()
{
CPaintDC dc(this);

    
CDC *memDC = new CDC;
memDC->CreateCompatibleDC( &dc );
CBitmap* bitmap = new CBitmap;
bitmap->LoadBitmap( IDB_BITMAP1 );
memDC->SelectObject( bitmap );
//dc.BitBlt(0,0,100,100,memDC,0,0,SRCCOPY); 
TransparentBlt(dc.m_hDC, 0, 0, 100, 100, 
           memDC->m_hDC, 0, 0, 100, 100, RGB(255,255,255));}为什么运行以后 我的整张图片都透明了啊

解决方案 »

  1.   

    检查bitmap->LoadBitmap( IDB_BITMAP1 );是否load成功
      

  2.   

    TransparentBlt()的返回值是BOOL型的,
    你用BOOL b=TransparentBlt(dc.m_hDC, 0, 0, 100, 100, 
               memDC->m_hDC, 0, 0, 100, 100, RGB(255,255,255));
    调试一下,看看b是TRUE还是FALSE。
      

  3.   

    oid TransparentBlt2( HDC hdcDest,      // 目标DC
     int nXOriginDest,   // 目标X偏移
     int nYOriginDest,   // 目标Y偏移
     int nWidthDest,     // 目标宽度
     int nHeightDest,    // 目标高度
     HDC hdcSrc,         // 源DC
     int nXOriginSrc,    // 源X起点
     int nYOriginSrc,    // 源Y起点
     int nWidthSrc,      // 源宽度
     int nHeightSrc,     // 源高度
     UINT crTransparent  // 透明色,COLORREF类型
     )
    {
    HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest); // 创建兼容位图
    HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL); // 创建单色掩码位图
    HDC hImageDC = CreateCompatibleDC(hdcDest);
    HDC hMaskDC = CreateCompatibleDC(hdcDest);
    hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP);
    hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC, hMaskBMP);

    // 将源DC中的位图拷贝到临时DC中
    if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc)
    BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
    else
    StretchBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, 
    hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);

    // 设置透明色
    SetBkColor(hImageDC, crTransparent); // 生成透明区域为白色,其它区域为黑色的掩码位图
    BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);

    // 生成透明区域为黑色,其它区域保持不变的位图
    SetBkColor(hImageDC, RGB(0,0,0));
    SetTextColor(hImageDC, RGB(255,255,255));
    BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND); // 透明部分保持屏幕不变,其它部分变成黑色
    SetBkColor(hdcDest,RGB(0xff,0xff,0xff));
    SetTextColor(hdcDest,RGB(0,0,0));
    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);

    // "或"运算,生成最终效果
    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);

    SelectObject(hImageDC, hOldImageBMP);
    DeleteDC(hImageDC);
    SelectObject(hMaskDC, hOldMaskBMP);
    DeleteDC(hMaskDC);
    DeleteObject(hImageBMP);
    DeleteObject(hMaskBMP);

    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CTransparentBltTestDlg::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting
    if (IsIconic())
    { SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    COLORREF crMask = RGB(0x00,0x00,0xff);
    BITMAP bm;
    m_bmpFootball.GetBitmap(&bm);
    int nWidth = bm.bmWidth;
    int nHeight = bm.bmHeight;
    CDC ImageDC;
    ImageDC.CreateCompatibleDC(&dc);
    CBitmap *pOldImageBMP = ImageDC.SelectObject(&m_bmpFootball);
    //::TransparentBlt(dc.m_hDC,50,40,nWidth,nHeight,ImageDC.m_hDC,0,0,nWidth,nHeight,crMask);
    TransparentBlt2(dc.m_hDC,50,40,nWidth,nHeight,ImageDC.m_hDC,0,0,nWidth,nHeight,crMask);
    ImageDC.SelectObject(pOldImageBMP);
    }
    }