Transparenz
-
Hallo,
ich möchte gerne in ein Fenster ein Bitmap mit Colorkey bekommen.Also ich möchte Bitmaps mit Colorkey laden und sie in mein Fenster einbinden. Ist das Möglich ??
Bye
-
nachtrag:
würde es etwas bringen, wenn ich ein Bitmap in ein Icon konviertiere ? Oder geht es auch ein bischen eleganter ??
Btw. können Icons jede Größe annehmen ??
-
Meinst du TransparentBlt?!
-
Dies findet man auch irgendwo in der MSDN:
void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart, short yStart, COLORREF cTransparentColor) { BITMAP bm; COLORREF cColor; HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave; HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld; HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave; POINT ptSize; hdcTemp = CreateCompatibleDC(hdc); SelectObject(hdcTemp, hBitmap); // Select the bitmap GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm); ptSize.x = bm.bmWidth; // Get width of bitmap ptSize.y = bm.bmHeight; // Get height of bitmap DPtoLP(hdcTemp, &ptSize, 1); // Convert from device // to logical points // Create some DCs to hold temporary data. hdcBack = CreateCompatibleDC(hdc); hdcObject = CreateCompatibleDC(hdc); hdcMem = CreateCompatibleDC(hdc); hdcSave = CreateCompatibleDC(hdc); // Create a bitmap for each DC. DCs are required for a number of // GDI functions. // Monochrome DC bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); // Monochrome DC bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); // Each DC must select a bitmap object to store pixel data. bmBackOld = SelectObject(hdcBack, bmAndBack); bmObjectOld = SelectObject(hdcObject, bmAndObject); bmMemOld = SelectObject(hdcMem, bmAndMem); bmSaveOld = SelectObject(hdcSave, bmSave); // Set proper mapping mode. SetMapMode(hdcTemp, GetMapMode(hdc)); // Save the bitmap sent here, because it will be overwritten. BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // Set the background color of the source DC to the color. // contained in the parts of the bitmap that should be transparent cColor = SetBkColor(hdcTemp, cTransparentColor); // Create the object mask for the bitmap by performing a BitBlt // from the source bitmap to a monochrome bitmap. BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // Set the background color of the source DC back to the original // color. SetBkColor(hdcTemp, cColor); // Create the inverse of the object mask. BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY); // Copy the background of the main DC to the destination. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart, SRCCOPY); // Mask out the places where the bitmap will be placed. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND); // Mask out the transparent colored pixels on the bitmap. BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND); // XOR the bitmap with the background on the destination DC. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT); // Copy the destination to the screen. BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY); // Place the original bitmap back into the bitmap sent here. BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY); // Delete the memory bitmaps. DeleteObject(SelectObject(hdcBack, bmBackOld)); DeleteObject(SelectObject(hdcObject, bmObjectOld)); DeleteObject(SelectObject(hdcMem, bmMemOld)); DeleteObject(SelectObject(hdcSave, bmSaveOld)); // Delete the memory DCs. DeleteDC(hdcMem); DeleteDC(hdcBack); DeleteDC(hdcObject); DeleteDC(hdcSave); DeleteDC(hdcTemp); }
Und dies ist meine Version:
HBITMAP CreateMask(HBITMAP hBmpSrc, COLORREF clTransparent) { HBITMAP hBmpMask; BITMAP bm; HDC hdcMask = CreateCompatibleDC(NULL); HDC hdcSrc = CreateCompatibleDC(NULL); COLORREF clPrev; if(!hBmpSrc) return NULL; // Create mask bitmap GetObject(hBmpSrc, sizeof(BITMAP), &bm); hBmpMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL); // Select bitmaps into DCs SelectObject(hdcMask, hBmpMask); SelectObject(hdcSrc, hBmpSrc); // Blit source dc into mask dc clPrev = SetBkColor(hdcSrc, clTransparent); BitBlt(hdcMask, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY); SetBkColor(hdcSrc, clPrev); // Clean up DeleteObject(hdcMask); DeleteObject(hdcSrc); return hBmpMask; } //--------------------------------------------------------------------------- VOID TransparentBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, HBITMAP hBmpMask, COLORREF rgbTransparent) { HDC hdcMask = CreateCompatibleDC(NULL); SelectObject(hdcMask, hBmpMask); BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCINVERT); BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcMask, nXSrc, nYSrc, SRCAND); BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCINVERT); SelectObject(hdcMask, (HBITMAP)NULL); DeleteDC(hdcMask); }
-
und jetzt all-in-one
void DrawTransparentBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, COLORREF clrColor) { HDC hScreenDC = CreateCompatibleDC(hDC), hMemDC = CreateCompatibleDC(hDC), hMaskDC = CreateCompatibleDC(hDC); BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),&bm); int cx = bm.bmWidth, cy = bm.bmHeight; HBITMAP hBmp = CreateCompatibleBitmap(hDC,cx,cy), hMask = CreateBitmap(cx,cy,1,1,0); SelectObject(hScreenDC,hBitmap), SelectObject(hMemDC,hBmp); SelectObject(hMaskDC,hMask); BitBlt(hMemDC,0,0,cx,cy,hScreenDC,0,0,SRCCOPY); SetBkColor(hMemDC,clrColor); BitBlt(hMaskDC,0,0,cx,cy,hMemDC,0,0,SRCCOPY); BitBlt(hMemDC,0,0,cx,cy,hMaskDC,0,0,SRCINVERT); BitBlt(hDC,x,y,cx,cy,hMaskDC,0,0,SRCAND); BitBlt(hDC,x,y,cx,cy,hMemDC,0,0,SRCPAINT); DeleteDC(hMaskDC), DeleteDC(hMemDC), DeleteDC(hScreenDC); DeleteObject(hMask), DeleteObject(hBmp); }
EDIT: Man könnte ein Bitmap mit Alpha-Channel nehmen, dann braucht man das ganze nicht mehr. Der Nachteil - es funktioniert erst ab Windows ME oder 2000?
-
Du hast aber 5 BitBlt's und ich nur 4.