S
Oder was garantiert immer geht:
//Microsofts Lösung aus Q79212:
void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, int xStart, int yStart,
COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
HBITMAP hBitAndBack, hBitAndObject, hBitAndMem, hBitSave,
hBitBackOld, hBitObjectOld, hBitMemOld, hBitSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bm);
ptSize.x = bm.bmWidth;
ptSize.y = bm.bmHeight;
DPtoLP(hdcTemp, &ptSize, 1);
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
hBitAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, 0);
hBitAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, 0);
hBitAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
hBitSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
hBitBackOld = (HBITMAP)SelectObject(hdcBack, hBitAndBack);
hBitObjectOld = (HBITMAP)SelectObject(hdcObject, hBitAndObject);
hBitMemOld = (HBITMAP)SelectObject(hdcMem, hBitAndMem);
hBitSaveOld = (HBITMAP)SelectObject(hdcSave, hBitSave);
SetMapMode(hdcTemp, GetMapMode(hdc));
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
cColor = SetBkColor(hdcTemp, cTransparentColor);
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
SetBkColor(hdcTemp, cColor);
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart, SRCCOPY);
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp ,0, 0, SRCPAINT);
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hdcBack, hBitBackOld));
DeleteObject(SelectObject(hdcObject, hBitObjectOld));
DeleteObject(SelectObject(hdcMem, hBitMemOld));
DeleteObject(SelectObject(hdcSave, hBitSaveOld));
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}