今天看看了symbian的活动对象介绍,于是写一个有关活动对象的小程序,该程序是一个GUI程序,在view动态显示当前的时间,使用到的活动对象定义如下:
对应类中函数实现:
if (iState == EUninitialized)
{
// Do something the first time RunL() is called
iState = EInitialized;
}
else if (iState != EError)
{
iView.DrawNow();
}
用来调用View中的Draw函数,使得程序中视图可以每1秒钟刷新(重绘)一次。
下面是View类的代码:
例如在class CHelloGUIAppView : public CCoeControl之前添加class CAOTest;否则编译器编译的时候提示CAOTest未声明。
下面是View类中实现代码:
/**/
/*
============================================================================
Name : AOTest.h
Author : yuankai
Version : 1.0
Copyright : [email protected]
Description : CAOTest declaration
============================================================================
*/
#ifndef AOTEST_H
#define AOTEST_H
#include < e32base.h > // For CActive, link against: euser.lib
#include < e32std.h > // For RTimer, link against: euser.lib
#include " HelloGUIAppView.h "
class CHelloGUIAppView;
class CAOTest : public CActive
{
public:
// Cancel and destroy
~CAOTest();
// Two-phased constructor.
static CAOTest* NewL(CHelloGUIAppView& view);
// Two-phased constructor.
static CAOTest* NewLC(CHelloGUIAppView& view);
public:
// New functions
// Function for making the initial request
void StartL(TTimeIntervalMicroSeconds32 aDelay);
private:
// C++ constructor
CAOTest(CHelloGUIAppView& view);
// Second-phase constructor
void ConstructL();
private:
// From CActive
// Handle completion
void RunL();
// How to cancel me
void DoCancel();
// Override to handle leaves from RunL(). Default implementation causes
// the active scheduler to panic.
TInt RunError(TInt aError);
private:
enum TAOTestState
{
EUninitialized, // Uninitialized
EInitialized, // Initalized
EError
// Error condition
};
private:
TInt iState; // State of the active object
RTimer iTimer; // Provides async timing service
TInt iCount;
CHelloGUIAppView & iView;//视图类对象的引用
} ;
#endif // AOTEST_H
============================================================================
Name : AOTest.h
Author : yuankai
Version : 1.0
Copyright : [email protected]
Description : CAOTest declaration
============================================================================
*/
#ifndef AOTEST_H
#define AOTEST_H
#include < e32base.h > // For CActive, link against: euser.lib
#include < e32std.h > // For RTimer, link against: euser.lib
#include " HelloGUIAppView.h "
class CHelloGUIAppView;
class CAOTest : public CActive
{
public:
// Cancel and destroy
~CAOTest();
// Two-phased constructor.
static CAOTest* NewL(CHelloGUIAppView& view);
// Two-phased constructor.
static CAOTest* NewLC(CHelloGUIAppView& view);
public:
// New functions
// Function for making the initial request
void StartL(TTimeIntervalMicroSeconds32 aDelay);
private:
// C++ constructor
CAOTest(CHelloGUIAppView& view);
// Second-phase constructor
void ConstructL();
private:
// From CActive
// Handle completion
void RunL();
// How to cancel me
void DoCancel();
// Override to handle leaves from RunL(). Default implementation causes
// the active scheduler to panic.
TInt RunError(TInt aError);
private:
enum TAOTestState
{
EUninitialized, // Uninitialized
EInitialized, // Initalized
EError
// Error condition
};
private:
TInt iState; // State of the active object
RTimer iTimer; // Provides async timing service
TInt iCount;
CHelloGUIAppView & iView;//视图类对象的引用
} ;
#endif // AOTEST_H
对应类中函数实现:
/**/
/*
============================================================================
Name : AOTest.cpp
Author : yuankai
Version : 1.0
Copyright : [email protected]
Description : CAOTest implementation
============================================================================
*/
#include " AOTest.h "
CAOTest::CAOTest(CHelloGUIAppView & view) :
CActive(EPriorityStandard) // Standard priority
,iCount( 0 ),iView(view)
{
}
CAOTest * CAOTest::NewLC(CHelloGUIAppView & view)
{
CAOTest* self = new (ELeave) CAOTest(view);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CAOTest * CAOTest::NewL(CHelloGUIAppView & view)
{
CAOTest* self = CAOTest::NewLC(view);
CleanupStack::Pop(); // self;
return self;
}
void CAOTest::ConstructL()
{
User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
CActiveScheduler::Add(this); // Add to scheduler
}
CAOTest:: ~ CAOTest()
{
Cancel(); // Cancel any request, if outstanding
iTimer.Close(); // Destroy the RTimer object
// Delete instance variables if any
}
void CAOTest::DoCancel()
{
iTimer.Cancel();
}
void CAOTest::StartL(TTimeIntervalMicroSeconds32 aDelay)
{
Cancel(); // Cancel any request, just to be sure
iState = EUninitialized;
iTimer.After(iStatus, aDelay); // Set for later
SetActive(); // Tell scheduler a request is active
}
void CAOTest::RunL()
{
if (iState == EUninitialized)
{
// Do something the first time RunL() is called
iState = EInitialized;
}
else if (iState != EError)
{
iView.DrawNow();
}
iTimer.After(iStatus, 1000000); // Set for 1 sec later
SetActive(); // Tell scheduler a request is active
}
TInt CAOTest::RunError(TInt aError)
{
return aError;
}
在上面的代码中我添加
============================================================================
Name : AOTest.cpp
Author : yuankai
Version : 1.0
Copyright : [email protected]
Description : CAOTest implementation
============================================================================
*/
#include " AOTest.h "
CAOTest::CAOTest(CHelloGUIAppView & view) :
CActive(EPriorityStandard) // Standard priority
,iCount( 0 ),iView(view)
{
}
CAOTest * CAOTest::NewLC(CHelloGUIAppView & view)
{
CAOTest* self = new (ELeave) CAOTest(view);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CAOTest * CAOTest::NewL(CHelloGUIAppView & view)
{
CAOTest* self = CAOTest::NewLC(view);
CleanupStack::Pop(); // self;
return self;
}
void CAOTest::ConstructL()
{
User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
CActiveScheduler::Add(this); // Add to scheduler
}
CAOTest:: ~ CAOTest()
{
Cancel(); // Cancel any request, if outstanding
iTimer.Close(); // Destroy the RTimer object
// Delete instance variables if any
}
void CAOTest::DoCancel()
{
iTimer.Cancel();
}
void CAOTest::StartL(TTimeIntervalMicroSeconds32 aDelay)
{
Cancel(); // Cancel any request, just to be sure
iState = EUninitialized;
iTimer.After(iStatus, aDelay); // Set for later
SetActive(); // Tell scheduler a request is active
}
void CAOTest::RunL()
{
if (iState == EUninitialized)
{
// Do something the first time RunL() is called
iState = EInitialized;
}
else if (iState != EError)
{
iView.DrawNow();
}
iTimer.After(iStatus, 1000000); // Set for 1 sec later
SetActive(); // Tell scheduler a request is active
}
TInt CAOTest::RunError(TInt aError)
{
return aError;
}
if (iState == EUninitialized)
{
// Do something the first time RunL() is called
iState = EInitialized;
}
else if (iState != EError)
{
iView.DrawNow();
}
用来调用View中的Draw函数,使得程序中视图可以每1秒钟刷新(重绘)一次。
下面是View类的代码:
/**/
/*
============================================================================
Name : HelloGUIAppView.h
Author :
Copyright : Your copyright notice
Description : Declares view class for application.
============================================================================
*/
#ifndef __HELLOGUIAPPVIEW_h__
#define __HELLOGUIAPPVIEW_h__
// INCLUDES
#include " AOTest.h "
#include < coecntrl.h >
#include < e32base.h >
#include < e32std.h >
class CAOTest;
// CLASS DECLARATION
class CHelloGUIAppView : public CCoeControl
{
public:
// New methods
/**//**
* NewL.
* Two-phased constructor.
* Create a CHelloGUIAppView object, which will draw itself to aRect.
* @param aRect The rectangle this view will be drawn to.
* @return a pointer to the created instance of CHelloGUIAppView.
*/
static CHelloGUIAppView* NewL(const TRect& aRect);
/**//**
* NewLC.
* Two-phased constructor.
* Create a CHelloGUIAppView object, which will draw itself
* to aRect.
* @param aRect Rectangle this view will be drawn to.
* @return A pointer to the created instance of CHelloGUIAppView.
*/
static CHelloGUIAppView* NewLC(const TRect& aRect);
/**//**
* ~CHelloGUIAppView
* Virtual Destructor.
*/
virtual ~CHelloGUIAppView();
public:
// Functions from base classes
/**//**
* From CCoeControl, Draw
* Draw this CHelloGUIAppView to the screen.
* @param aRect the rectangle of this view that needs updating
*/
void Draw(const TRect& aRect) const;
/**//**
* From CoeControl, SizeChanged.
* Called by framework when the view size is changed.
*/
virtual void SizeChanged();
/**//**
* From CoeControl, HandlePointerEventL.
* Called by framework when a pointer touch event occurs.
* Note: although this method is compatible with earlier SDKs,
* it will not be called in SDKs without Touch support.
* @param aPointerEvent the information about this event
*/
virtual void HandlePointerEventL(const TPointerEvent& aPointerEvent);
/**//*
* 进行活动对象的初始化工作
*
* */
void ininMyActiveObject();
private:
// Constructors
CAOTest* iMyAO;
/**//**
* ConstructL
* 2nd phase constructor.
* Perform the second phase construction of a
* CHelloGUIAppView object.
* @param aRect The rectangle this view will be drawn to.
*/
void ConstructL(const TRect& aRect);
/**//**
* CHelloGUIAppView.
* C++ default constructor.
*/
CHelloGUIAppView();
} ;
#endif // __HELLOGUIAPPVIEW_h__
// End of File
在上面代码中一定要在类上方提前声明一下类中用到的其他类型:
============================================================================
Name : HelloGUIAppView.h
Author :
Copyright : Your copyright notice
Description : Declares view class for application.
============================================================================
*/
#ifndef __HELLOGUIAPPVIEW_h__
#define __HELLOGUIAPPVIEW_h__
// INCLUDES
#include " AOTest.h "
#include < coecntrl.h >
#include < e32base.h >
#include < e32std.h >
class CAOTest;
// CLASS DECLARATION
class CHelloGUIAppView : public CCoeControl
{
public:
// New methods
/**//**
* NewL.
* Two-phased constructor.
* Create a CHelloGUIAppView object, which will draw itself to aRect.
* @param aRect The rectangle this view will be drawn to.
* @return a pointer to the created instance of CHelloGUIAppView.
*/
static CHelloGUIAppView* NewL(const TRect& aRect);
/**//**
* NewLC.
* Two-phased constructor.
* Create a CHelloGUIAppView object, which will draw itself
* to aRect.
* @param aRect Rectangle this view will be drawn to.
* @return A pointer to the created instance of CHelloGUIAppView.
*/
static CHelloGUIAppView* NewLC(const TRect& aRect);
/**//**
* ~CHelloGUIAppView
* Virtual Destructor.
*/
virtual ~CHelloGUIAppView();
public:
// Functions from base classes
/**//**
* From CCoeControl, Draw
* Draw this CHelloGUIAppView to the screen.
* @param aRect the rectangle of this view that needs updating
*/
void Draw(const TRect& aRect) const;
/**//**
* From CoeControl, SizeChanged.
* Called by framework when the view size is changed.
*/
virtual void SizeChanged();
/**//**
* From CoeControl, HandlePointerEventL.
* Called by framework when a pointer touch event occurs.
* Note: although this method is compatible with earlier SDKs,
* it will not be called in SDKs without Touch support.
* @param aPointerEvent the information about this event
*/
virtual void HandlePointerEventL(const TPointerEvent& aPointerEvent);
/**//*
* 进行活动对象的初始化工作
*
* */
void ininMyActiveObject();
private:
// Constructors
CAOTest* iMyAO;
/**//**
* ConstructL
* 2nd phase constructor.
* Perform the second phase construction of a
* CHelloGUIAppView object.
* @param aRect The rectangle this view will be drawn to.
*/
void ConstructL(const TRect& aRect);
/**//**
* CHelloGUIAppView.
* C++ default constructor.
*/
CHelloGUIAppView();
} ;
#endif // __HELLOGUIAPPVIEW_h__
// End of File
例如在class CHelloGUIAppView : public CCoeControl之前添加class CAOTest;否则编译器编译的时候提示CAOTest未声明。
下面是View类中实现代码:
/**/
/*
============================================================================
Name : HelloGUIAppView.cpp
Author :
Copyright : Your copyright notice
Description : Application view implementation
============================================================================
*/
// INCLUDE FILES
#include < coemain.h >
#include " HelloGUIAppView.h "
#include < e32math.h >
// ============================ MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CHelloGUIAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView * CHelloGUIAppView::NewL( const TRect & aRect)
{
CHelloGUIAppView* self = CHelloGUIAppView::NewLC(aRect); //使用NewLC进行视图的创建
CleanupStack::Pop(self); //把构造的对象的指针弹出清除栈中
return self;
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView * CHelloGUIAppView::NewLC( const TRect & aRect)
{
CHelloGUIAppView* self = new (ELeave) CHelloGUIAppView; //使用默认的构造函数进行视图的创建
CleanupStack::PushL(self); //把构造的对象的指针压入清除栈中
self->ConstructL(aRect); //使用第2阶段的构造函数进行视图的创建
return self;
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::ConstructL( const TRect & aRect)
{
// Create a window for this application view
CreateWindowL(); //为应用程序的视图创建一个window
// Set the windows size
SetRect(aRect); //设置视图窗口的大小为aRect
// Activate the window, which makes it ready to be drawn
ActivateL(); //激活窗口使得可以在它上面绘图
//初始化自己定义的活动对象
ininMyActiveObject();
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::CHelloGUIAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView::CHelloGUIAppView()
{
// No implementation required
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::~CHelloGUIAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView:: ~ CHelloGUIAppView()
{
// No implementation required
if(iMyAO)
{
iMyAO->Cancel();
delete iMyAO;
iMyAO=NULL;
}
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::Draw( const TRect & aRect) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc(); //获得标准的绘图上下文
// Gets the control's extent //获得当前控件大小的矩形
TRect drawRect(Rect());
// Clears the screen
gc.Clear(drawRect); //进行屏幕清除操作
// 自己写的绘图代码
//设置画刷的风格
//gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
//设置画笔的风格大小
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetPenSize(TSize(1,1));
//设置画笔颜色
gc.SetPenColor(TRgb(255,0,0));
TInt x=aRect.Width()/10;
TInt y=aRect.Height()/4;
TPoint point(x,y);
//获得系统的时间并且显示出来
TTime time;
_LIT(timeformat,"%F%Y-%M-%D %H:%T:%S.%C\t");
time.HomeTime();
TBuf<60> str;
time.FormatL(str,timeformat);
//获得字体
const CFont * normalfont=iCoeEnv->NormalFont();
//设置字体
gc.UseFont(normalfont);
//绘制文字
gc.DrawText(str,point);
//撤销字体
gc.DiscardFont();
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::SizeChanged()
{
DrawNow(); //视图大小发生变化的时候,进行重绘
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs,
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::HandlePointerEventL( const TPointerEvent & aPointerEvent)
{
// Call base class HandlePointerEventL()
CCoeControl::HandlePointerEventL(aPointerEvent);//在支持触摸屏的情况下,调用父类的指针事件响应函数
}
void CHelloGUIAppView::ininMyActiveObject()
{
/**//*
* 此处无需再次创建一个活动规划器,因为GUI框架中已经存在
* */
//CActiveScheduler * scheduler=new (ELeave)CActiveScheduler();
//CleanupStack::PushL(scheduler);
//CActiveScheduler::Install(scheduler);
iMyAO=CAOTest::NewLC(*this);
iMyAO->StartL(0);
CActiveScheduler::Start();
//CleanupStack::PopAndDestroy(scheduler);
}
// End of File
============================================================================
Name : HelloGUIAppView.cpp
Author :
Copyright : Your copyright notice
Description : Application view implementation
============================================================================
*/
// INCLUDE FILES
#include < coemain.h >
#include " HelloGUIAppView.h "
#include < e32math.h >
// ============================ MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CHelloGUIAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView * CHelloGUIAppView::NewL( const TRect & aRect)
{
CHelloGUIAppView* self = CHelloGUIAppView::NewLC(aRect); //使用NewLC进行视图的创建
CleanupStack::Pop(self); //把构造的对象的指针弹出清除栈中
return self;
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView * CHelloGUIAppView::NewLC( const TRect & aRect)
{
CHelloGUIAppView* self = new (ELeave) CHelloGUIAppView; //使用默认的构造函数进行视图的创建
CleanupStack::PushL(self); //把构造的对象的指针压入清除栈中
self->ConstructL(aRect); //使用第2阶段的构造函数进行视图的创建
return self;
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::ConstructL( const TRect & aRect)
{
// Create a window for this application view
CreateWindowL(); //为应用程序的视图创建一个window
// Set the windows size
SetRect(aRect); //设置视图窗口的大小为aRect
// Activate the window, which makes it ready to be drawn
ActivateL(); //激活窗口使得可以在它上面绘图
//初始化自己定义的活动对象
ininMyActiveObject();
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::CHelloGUIAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView::CHelloGUIAppView()
{
// No implementation required
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::~CHelloGUIAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView:: ~ CHelloGUIAppView()
{
// No implementation required
if(iMyAO)
{
iMyAO->Cancel();
delete iMyAO;
iMyAO=NULL;
}
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::Draw( const TRect & aRect) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc(); //获得标准的绘图上下文
// Gets the control's extent //获得当前控件大小的矩形
TRect drawRect(Rect());
// Clears the screen
gc.Clear(drawRect); //进行屏幕清除操作
// 自己写的绘图代码
//设置画刷的风格
//gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
//设置画笔的风格大小
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetPenSize(TSize(1,1));
//设置画笔颜色
gc.SetPenColor(TRgb(255,0,0));
TInt x=aRect.Width()/10;
TInt y=aRect.Height()/4;
TPoint point(x,y);
//获得系统的时间并且显示出来
TTime time;
_LIT(timeformat,"%F%Y-%M-%D %H:%T:%S.%C\t");
time.HomeTime();
TBuf<60> str;
time.FormatL(str,timeformat);
//获得字体
const CFont * normalfont=iCoeEnv->NormalFont();
//设置字体
gc.UseFont(normalfont);
//绘制文字
gc.DrawText(str,point);
//撤销字体
gc.DiscardFont();
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::SizeChanged()
{
DrawNow(); //视图大小发生变化的时候,进行重绘
}
// -----------------------------------------------------------------------------
// CHelloGUIAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs,
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::HandlePointerEventL( const TPointerEvent & aPointerEvent)
{
// Call base class HandlePointerEventL()
CCoeControl::HandlePointerEventL(aPointerEvent);//在支持触摸屏的情况下,调用父类的指针事件响应函数
}
void CHelloGUIAppView::ininMyActiveObject()
{
/**//*
* 此处无需再次创建一个活动规划器,因为GUI框架中已经存在
* */
//CActiveScheduler * scheduler=new (ELeave)CActiveScheduler();
//CleanupStack::PushL(scheduler);
//CActiveScheduler::Install(scheduler);
iMyAO=CAOTest::NewLC(*this);
iMyAO->StartL(0);
CActiveScheduler::Start();
//CleanupStack::PopAndDestroy(scheduler);
}
// End of File
在上面代码中void CHelloGUIAppView::ininMyActiveObject() 处无需再次创建一个活动对象规划器,只有在非GUI程序中需要,因为GUI框架中已经存在,GUI应用程序会自动安装和运行活动调度器,不用手动安装。如果重复创建活动对象规划器那么程序在运行时候提示E32USER-CBase43错误。