方法一:
void CDialog::Draw( const TRect& aRect ) const
{
CWindowGc& gc = SystemGc();
TRgb colorAlpha(KRgbBlack);
colorAlpha.SetAlpha(100);//0时为完全透明
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.SetBrushColor(colorAlpha);
gc.DrawRect(aRect);
}
方法二:需要设备支持 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();
}