DrawTransparentBitmap
-
Hi,
ich hab den Code hier mal hier im Forum gesehen:
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 = static_cast<HBITMAP>(SelectObject(hdcBack, bmAndBack)); bmObjectOld = static_cast<HBITMAP>(SelectObject(hdcObject, bmAndObject)); bmMemOld = static_cast<HBITMAP>(SelectObject(hdcMem, bmAndMem)); bmSaveOld = static_cast<HBITMAP>(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); }leider werden bei mir nur schwarze Rechtecke gezeichnet!!!

Muss ich noch was machen ausser die Funktion aufrufen?
-
Ich habe mir den Code jetzt nicht genauers angeschaut, aber wie rufst du die Funktion auf? Und hast du mal die verschiedenen Rückgabewerte kontrolliert?
-
alos alle werte sind gefühlt... keiner davon ist NULL oder so ...
-
wie meinst du das mit aufrufen?
Hab ne Klasse dafür... und die Werte sind beim Aufruf der Funktion gefüllt...
-
Was genau übergibst du der Funktion? Zeig doch mal den Code, wie du das Ganze eingebunden hast

-
ok, das ist nicht ganz so einfach... ich hab das in classen gebaut:
Hier mal nur der "wichtige" teilclass CGButton { private: HDC hdcButton; HWND hwnd; HBITMAP hBitmap; HINSTANCE hInstance; [...] public: CGButton(HWND HWnd, HINSTANCE HInstance, int iWM_MessageCode, int PICTURE_1, int iXPosition, int iYPosition, int iHeight, int iWidth); ~CGButton(); void SetButtonPics(int PICTURE); void PaintButton(); void PaintButton(HDC hdc); [...] }; //****SetButtonPics()****// //Zum setzen der Bilder und zum ändern der Position und Größe void CGButton::SetButtonPics(int PICTURE) { HDC hdc; hdc = GetDC(hwnd); hBitmap = (HBITMAP)LoadImage(hInstance,MAKEINTRESOURCE(PICTURE),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION); hdcButton = CreateCompatibleDC(hdc); SelectObject(hdcButton, hBitmap); DeleteObject(hBitmap); ReleaseDC(hwnd,hdc); } //****PaintButton()****// //Zum Zeichnen des Buttons void CGButton::PaintButton(HDC hdc) { if (Visible == TRUE) { DrawTransparentBitmap(hdc, hBitmap, sSize.XPosition, sSize.YPosition, Color); if (PaintStatus == BUTTON_2) { HPEN hPen, hOldPen; hPen = CreatePen (PS_SOLID, 1, GetSysColor(COLOR_APPWORKSPACE)); hOldPen = (HPEN) SelectObject(hdc, hPen); MoveToEx(hdc,sSize.XPosition, sSize.YPosition, NULL); LineTo(hdc, sSize.XPosition + sSize.Width - 1, sSize.YPosition); LineTo(hdc, sSize.XPosition + sSize.Width - 1, sSize.YPosition + sSize.Height - 1); LineTo(hdc, sSize.XPosition, sSize.YPosition + sSize.Height - 1); LineTo(hdc, sSize.XPosition, sSize.YPosition); DeleteObject(SelectObject(hdc, hOldPen)); } } } //****PaintButton()****// //Zum Zeichnen des Buttons void CGButton::PaintButton() { HDC hdc; if (Visible == TRUE) { hdc = GetDC(hwnd); PaintButton(hdc); ReleaseDC(hwnd,hdc); } }
-
Lösche keine Objekte, die in einen Geräte-Kontext eingesetzt sind!
(Man hört sich das auf deutsch scheiße an)
-
du meinst:
DeleteObject(hBitmap);
=> stimmt das passt nicht. ich probier das mal aus, dann meld ich mich nochmal
-
bringt auch nichts

-
Warum setzt du eigentlich hBitmap in einen Geräte Kontext ein, den du niemals verwendest? DrawTranspar... setzt das Bitmap ja auch in einen DC ein, bzw. versucht es, aber ich glaube es schafft es nicht, weil das Bitmap schon in einem anderen DC selektiert ist.
Wenn das auch nicht hilft, musst du halt mal alle Rückgabewerte kontrollieren, dann findest du auch allein den Fehler.
-
D@niel $chumann schrieb:
Warum setzt du eigentlich hBitmap in einen Geräte Kontext ein, den du niemals verwendest? DrawTranspar... setzt das Bitmap ja auch in einen DC ein, bzw. versucht es, aber ich glaube es schafft es nicht, weil das Bitmap schon in einem anderen DC selektiert ist.
mh... das hab ich nicht gewusst...
-
und das war mein Fehler

Sorry, das hab ich echt nicht gewusst

Danke!!!
-
Ich habe da auch gleich mal ne Frage: Wieso benutzt du nicht TransparentBlt
Ich weiß zwar selber nicht wie das Funktioniert aber vielleicht klappt es ja bei dir.
-
Ich benutze es nicht, weil das JETZT funktioniert

Ansonsten gerne...