symbian半透明效果

概述

本文描述了如何在支持EColor16MA显示模式的设备上创建半透明窗口。

前提条件

设备必须支持EColor16MA显示模式。使用以下代码可以检查设备支持的显示模式。

if(CCoeEnv::Static()->ScreenDevice()->DisplayMode()==EColor16MA)
{
// 支持EColor16MA
}

方案

本方案最初由william.edwards在UIQ论坛的"半透明窗口"系列文章中阐明 (UIQ论坛已经关闭,但仍然可以使用Google搜索"Semi-Transparent Windows #1"和"Semi-Transparent Windows #2",然后查看缓存的网页).

void CControlOnTopCtrl::ConstructL(const TRect& aRect, RWindowGroup* aWindowGroup)
{
TRect rc(aRect);
TInt w = rc.Width();
TInt h = rc.Height();
 
CreateWindowL(aWindowGroup);
for (TInt i = 0; i < 2; i++)
{
w /= 2;
h /= 2;
}
 
SetUniqueHandle(EMyCueControl);
 
TRect rc_new(w, h, w+15, h+15);
SetRect(rc_new);
// begin by chen, code from William's post
// we need to set the display mode to ensure that it has an
// alpha-component specified. However, the system may instead return
// another display mode. We don't have to pay much attention, as the
// call to SetTransparencyAlphaChannel will check wether the Window
// Server and the window's display mode support this kind of
// transparency
Window().SetRequiredDisplayMode(EColor16MA);
TRgb backgroundColour = KRgbWhite; // for example
if(KErrNone == Window().SetTransparencyAlphaChannel())
{
// we now have a semi-transparent window,
// so we have to not clear the window region in an
// opaque colour before drawing it each time;
// 0 alpha is completely transparent,
// 255 alpha is completely opaque
backgroundColour.SetAlpha(0);
}
Window().SetBackgroundColor(backgroundColour);
// end by chen
ActivateL();
}

该代码已经在S60第五版仿真器和一台支持EColor16MA显示模式的设备上验证通过, 以下是仿真器屏幕截图 (透明窗口上的红点):

 

 

 

传入相应的图片,就会把图片变成半透明效果并显示在程序界面上.

  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

你可能感兴趣的:(symbian)