為Symbian程式中的圖片增加半透明效果

gudesheng發表於2008-01-03

我封裝了一個方法,傳入相應的圖片,就會把圖片變成半透明效果並顯示在程式介面上.

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

 

效果如下:

原圖片為

轉變後的圖片為



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1465656


相關文章