R
habs jetzt geschafft. das problem lag an den verwendeten größen. hier noch mal die verbesserte version:
void CXTGlobal::BltTransparentBitmap(CDC *cDC, CBitmap *bBitmap, int iPosX, int iPosY, int iWidth, int iHight, COLORREF cTransparent, UINT uFlags, int iBmpWidth, int iBmpHeight)
{
COLORREF crOldBack = cDC->SetBkColor(RGB(255, 255, 255));
COLORREF crOldText = cDC->SetTextColor(RGB(0, 0, 0));
CDC dcImage, dcTrans;
CBitmap *pOldBitmapImage, *pOldBitmapTrans, bitmapTrans;
CRect rStretchPos;
// Create two memory dcs for the image and the mask
dcImage.CreateCompatibleDC(cDC);
dcTrans.CreateCompatibleDC(cDC);
// Select the image into the appropriate dc
pOldBitmapImage = dcImage.SelectObject(bBitmap);
// Build mask based on transparent colour
dcImage.SetBkColor(cTransparent);
if (uFlags & BTB_STRETCH)
{
if ((iBmpWidth == -1) && (iBmpHeight == -1))
{
iBmpWidth = iWidth;
iBmpHeight = iHight;
}
rStretchPos.left = (uFlags & BTB_FLIP_HORIZONTAL) ? (iBmpWidth - 1) : (0);
rStretchPos.right = (uFlags & BTB_FLIP_HORIZONTAL) ? (-iBmpWidth) : (iBmpWidth);
rStretchPos.top = (uFlags & BTB_FLIP_VERTICAL) ? (iBmpHeight - 1) : (0);
rStretchPos.bottom = (uFlags & BTB_FLIP_VERTICAL) ? (-iBmpHeight) : (iBmpHeight);
}
// Is the picture wanted to be smaller?
if ((uFlags & BTB_STRETCH) && (iBmpWidth != -1) && (iBmpHeight != -1) && ((iWidth < iBmpWidth) || (iHight < iBmpHeight)))
{
CRect rNormal;
// Mirror normal Size, too
rNormal.left = (uFlags & BTB_FLIP_HORIZONTAL) ? (iWidth - 1) : (0);
rNormal.right = (uFlags & BTB_FLIP_HORIZONTAL) ? (-iWidth) : (iWidth);
rNormal.top = (uFlags & BTB_FLIP_VERTICAL) ? (iHight - 1) : (0);
rNormal.bottom = (uFlags & BTB_FLIP_VERTICAL) ? (-iHight) : (iHight);
// Create the mask bitmap
bitmapTrans.CreateBitmap(iBmpWidth, iBmpHeight, 1, 1, NULL);
// Select the mask bitmap into the appropriate dc
pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);
dcTrans.StretchBlt(0, 0, iWidth, iHight, &dcImage, 0, 0, iBmpWidth, iBmpHeight, SRCCOPY);
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, rNormal.left, rNormal.top, rNormal.right, rNormal.bottom, SRCAND);
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
}
else
{
// Create the mask bitmap
bitmapTrans.CreateBitmap(iWidth, iHight, 1, 1, NULL);
// Select the mask bitmap into the appropriate dc
pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);
dcTrans.BitBlt(0, 0, iWidth, iHight, &dcImage, 0, 0, SRCCOPY);
if (uFlags & BTB_STRETCH)
{
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCAND);
cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
}
else
{
cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcImage, 0, 0, SRCINVERT);
cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, 0, 0, SRCAND);
cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcImage, 0, 0, SRCINVERT);
}
}
// Restore settings
dcImage.SelectObject(pOldBitmapImage);
dcTrans.SelectObject(pOldBitmapTrans);
cDC->SetBkColor(crOldBack);
cDC->SetTextColor(crOldText);
}
EDIT:
hatte stretchen nicht berücksichtigt, jetzt passt aber alles