Icons anzeigen (MS VS C++ 6.0)

sand13r

Erfahrenes Mitglied
Hallihallo werte Freunde der Sonne,


ich habe ein kleines Problem und zwar muss ich in MS VS 6.0 ein tranzparenten Button laden.

dies wird so gemacht

C++:
CBUTTON m_cFwd;
.
.

 m_cFwd.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_GO)));

dadurch kann ich das Bitmap anzeigen.. nun geht in Bitmaps aber keine Transparenz nur in gifs ico etc. kein Problem nehme ich halt ein Transparentes ico. kann man ja auch ganz einfach hinzufügen.
gesagt getan nur leider weis ich nicht wie ich die Icons an der Stelle vom CButton anzeigen kann.. ich habs schon so versucht..

C++:
m_cFwd.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1)));

geht aber leider nicht...

weis jemand Rat?


danke schonmal im Vorraus


greetz sand13r
 
Zuletzt bearbeitet:
Hi,

also mit der folgenden methode kannst du Transparente Grafiken zeichnen.

Dafür muss deine Grafik allerdings den gesamten transparenten Bereich in einer bestimmten Farbe haben, welche dann transparent gezeichnet wird.


Code:
void MyClass::TransBlt( CDC *pDC, int nXDest, int nYDest, int nWidth,
                                    int nHeight/*, HBITMAP hBitmap, int nXSrc, int   nYSrc, 
                                    COLORREF colorTransparent, HPALETTE hPal */)
{
    CDC memDC, maskDC, tempDC;

    maskDC.CreateCompatibleDC(pDC);
    CBitmap maskBitmap;

    //add these to store return of SelectObject() calls
    CBitmap* pOldMemBmp = NULL;
    CBitmap* pOldMaskBmp = NULL;
    HBITMAP hOldTempBmp = NULL;

    memDC.CreateCompatibleDC(pDC);
    tempDC.CreateCompatibleDC(pDC);
    CBitmap bmpImage;
    bmpImage.CreateCompatibleBitmap( pDC, nWidth, nHeight );
    pOldMemBmp = memDC.SelectObject( &bmpImage );

	HPALETTE hPal = NULL;
	HBITMAP hBitmap = (HBITMAP)GetSafeHandle();
    // Select and realize the palette
    if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && hPal )
    {
        ::SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
        pDC->RealizePalette();

        ::SelectPalette( memDC, hPal, FALSE );
    }

    hOldTempBmp = (HBITMAP) ::SelectObject( tempDC.m_hDC, hBitmap );

    memDC.BitBlt( 0,0,nWidth, nHeight, &tempDC, 0, 0, SRCCOPY );

    // Create monochrome bitmap for the mask
    maskBitmap.CreateBitmap( nWidth, nHeight, 1, 1, NULL );
    pOldMaskBmp = maskDC.SelectObject( &maskBitmap );
    memDC.SetBkColor( RGB(0,255,0));

    // Create the mask from the memory DC
    maskDC.BitBlt( 0, 0, nWidth, nHeight, &memDC, 0, 0, SRCCOPY );

    // Set the background in memDC to black. Using SRCPAINT with black 
    // and any other color results in the other color, thus making 
    // black the transparent color
    memDC.SetBkColor(RGB(0,0,0));
    memDC.SetTextColor(RGB(255,255,255));
    memDC.BitBlt(0, 0, nWidth, nHeight, &maskDC, 0, 0, SRCAND);

    // Set the foreground to black. See comment above.
    pDC->SetBkColor(RGB(255,255,255));
    pDC->SetTextColor(RGB(0,0,0));
    pDC->BitBlt(nXDest, nYDest, nWidth, nHeight, &maskDC, 0, 0, SRCAND);

    // Combine the foreground with the background
    pDC->BitBlt(nXDest, nYDest, nWidth, nHeight, &memDC,
        0, 0, SRCPAINT);


    if (hOldTempBmp)
        ::SelectObject( tempDC.m_hDC, hOldTempBmp);
    if (pOldMaskBmp)
        maskDC.SelectObject( pOldMaskBmp );
    if (pOldMemBmp)
        memDC.SelectObject( pOldMemBmp );
}

Die Methode habe ich selbst mal im Internet gefunden und sie funktioniert prächtig ;-)

Gruß,

Peter
 
Gehe mal im Dialogdesigner zu den Eigenschaften des Buttons, Auf der Registerkarte "Formate" musst du die Option "Symbol" (NICHT "Bitmap") aktivieren.

Gruß
MCoder
 
Zurück