本文一步一步讲解怎么使用进度条,同时也说明了最基本的控件的使用方式。
1、根据向导建立一个新工程。我创建的工程:ProcessbarT
2、在视图类声明中添加基类:MCoeControlObserver
class CProcessbarTAppView : public CCoeControl,MCoeControlObserver
3、重写MCoeControlObserver的虚函数 virtual void HandleControlEventL(CCoeControl *aControl, TCoeEvent aEventType);
4、声明进度条类和时间控件(动态修改进度条值)
private:
CEikProgressInfo* iPbar;
CPeriodic* iPeriodicTimer;
5、为了使用控件,要将CCoeControl中的三个函数重载。
(1)、SizeChanged() 函数:
void CProcessbarTAppView::SizeChanged()
{
//DrawNow();
}
(2)、CountComponentControls()函数:
TInt CProcessbarTAppView::CountComponentControls() const { return 1; // return nbr of controls inside this container }
(3)、ComponentControl(TInt aIndex)函数:
CCoeControl* CProcessbarTAppView::ComponentControl(TInt aIndex) const { switch ( aIndex ) { case 0: return iPbar; default: return NULL; } }
6、添加进度条控件
(1)、添加资源文件
//-------------------------定义的进度条-----------------------------------------------
RESOURCE PROGRESSINFO r_progress_bar
{
finalval = 240;
width = 120;
height = 20;
}
(2)、在函数ConstructL中构建进度条
TResourceReader reader;
iCoeEnv->CreateResourceReaderLC(reader, R_PROGRESS_BAR);
iPbar = new(ELeave)CEikProgressInfo();
iPbar->ConstructFromResourceL( reader );
CleanupStack::PopAndDestroy();
iPbar->SetPosition(TPoint(10,40));
iPbar->SetContainerWindowL(*this);
7、设置时间控件,使其动起来:
(1)、初始化时间并启动计时功能
iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
if( !iPeriodicTimer->IsActive() )
{
iPeriodicTimer->Start(500000,200000,TCallBack(Period,this));
}
(2)、时间控件处理函数声明:
static TInt Period(TAny* aPtr); void DoStep();
(3)、时间控件处理函数实现:
TInt CProcessbarTAppView::Period(TAny* aPtr)
{
(static_cast<CProcessbarTAppView*>(aPtr))->DoStep();
return TRUE;
}
void CProcessbarTAppView::DoStep()
{
iPbar->IncrementAndDraw(20);
// CEikProgressInfo类提供了多个设置进度的函数,比较常用的是IncrementAndDraw()函数和SetAndDraw()函数,
// IncrementAndDraw()函数的作用是将进度条增加aInc步,并刷新进度条显示。aInc可以是负数,此时进度条的进度会向后跳帧gdang进度条值达到
// 或者超过finalval定义的值后,进度条上显示为最大的进度。
// SetAndDraw()函数的作用是将当前进度设置成参数aValue代表的一个绝对的值。
}
8、最后不要忘记释放内存。
CProcessbarTAppView::~CProcessbarTAppView()
{
// No implementation required
if( iPbar )
{
delete iPbar;
}
if( iPeriodicTimer->IsActive() )
{
iPeriodicTimer->Cancel();
}
if( iPeriodicTimer )
{
delete iPeriodicTimer;
}
}
9、最后效果图:
注:所用到的头文件和库请参考SDK