我封装了一个方法,传入相应的图片,就会把图片变成半透明效果并显示在程序界面上.
void CMegajoyContainer::ConvertToAlpha(CWindowGc& gc,CFbsBitmap *imgToBrighten) const
{
CFbsBitmap* iEmptyBitmap = new ( ELeave ) CFbsBitmap();
CleanupStack::PushL( iEmptyBitmap );
User::LeaveIfError( iEmptyBitmap->Create( imgToBrighten->SizeInPixels(), CEikonEnv::Static()->DefaultDisplayMode() ) );
CFbsBitmapDevice *iGD = CFbsBitmapDevice::NewL( iEmptyBitmap );
CleanupStack::PushL( iGD );
CFbsBitGc *iGC;
User::LeaveIfError( iGD->CreateContext( iGC ) );
iGC->SetPenStyle( CGraphicsContext::ENullPen );
iGC->SetBrushColor( KRgbWhite );
iGC->SetBrushStyle( CGraphicsContext::ESolidBrush );
iGC->DrawRect( TRect(TPoint(0,0), iEmptyBitmap->SizeInPixels()) );
iGC->SetBrushStyle( CGraphicsContext::ENullBrush );
delete iGC;
CleanupStack::PopAndDestroy( iGD );
CFbsBitmap* iEmptyBitmapMask = new ( ELeave ) CFbsBitmap();
CleanupStack::PushL( iEmptyBitmapMask );
User::LeaveIfError( iEmptyBitmapMask->Create( imgToBrighten->SizeInPixels(), EGray256 ) );
iGD = CFbsBitmapDevice::NewL( iEmptyBitmapMask );
CleanupStack::PushL( iGD );
User::LeaveIfError( iGD->CreateContext( iGC ) );
iGC->SetPenStyle( CGraphicsContext::ENullPen );
iGC->SetBrushColor( KRgbGray );
iGC->SetBrushStyle( CGraphicsContext::ESolidBrush );
iGC->DrawRect( TRect(TPoint(0,0), iEmptyBitmap->SizeInPixels()) );
iGC->SetBrushStyle( CGraphicsContext::ENullBrush );
delete iGC;
CleanupStack::PopAndDestroy( iGD );
gc.BitBltMasked(TPoint(0,0),iEmptyBitmap,TRect(TPoint(0,0),iEmptyBitmapMask->SizeInPixels()),iEmptyBitmapMask,EFalse);
CleanupStack::PopAndDestroy(iEmptyBitmapMask);
CleanupStack::PopAndDestroy( iEmptyBitmap );
}
注意要在.mmp文件中加入库文件
LIBRARY bitgdi.lib
LIBRARY ws32.lib
LIBRARY fbscli.lib
效果如下:
原图片为
转变后的图片为