今天看看了symbian的活動(dòng)對(duì)象介紹,于是寫一個(gè)有關(guān)活動(dòng)對(duì)象的小程序,該程序是一個(gè)GUI程序,在view動(dòng)態(tài)顯示當(dāng)前的時(shí)間,使用到的活動(dòng)對(duì)象定義如下:

/**//*
============================================================================
Name : AOTest.h
Author : yuankai
Version : 1.0
Copyright : yuankaii@hotmail.com
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;//視圖類對(duì)象的引用
};

#endif // AOTEST_H

對(duì)應(yīng)類中函數(shù)實(shí)現(xiàn):

/**//*
============================================================================
Name : AOTest.cpp
Author : yuankai
Version : 1.0
Copyright : yuankaii@hotmail.com
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();
}
用來調(diào)用View中的Draw函數(shù),使得程序中視圖可以每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);


/**//*
* 進(jìn)行活動(dòng)對(duì)象的初始化工作
*
* */
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;否則編譯器編譯的時(shí)候提示CAOTest未聲明。
下面是View類中實(shí)現(xiàn)代碼:

/**//*
============================================================================
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進(jìn)行視圖的創(chuàng)建
CleanupStack::Pop(self); //把構(gòu)造的對(duì)象的指針彈出清除棧中
return self;
}

// -----------------------------------------------------------------------------
// CHelloGUIAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CHelloGUIAppView* CHelloGUIAppView::NewLC(const TRect& aRect)

{
CHelloGUIAppView* self = new (ELeave) CHelloGUIAppView; //使用默認(rèn)的構(gòu)造函數(shù)進(jìn)行視圖的創(chuàng)建
CleanupStack::PushL(self); //把構(gòu)造的對(duì)象的指針壓入清除棧中
self->ConstructL(aRect); //使用第2階段的構(gòu)造函數(shù)進(jìn)行視圖的創(chuàng)建
return self;
}

// -----------------------------------------------------------------------------
// CHelloGUIAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::ConstructL(const TRect& aRect)

{
// Create a window for this application view
CreateWindowL(); //為應(yīng)用程序的視圖創(chuàng)建一個(gè)window

// Set the windows size
SetRect(aRect); //設(shè)置視圖窗口的大小為aRect

// Activate the window, which makes it ready to be drawn
ActivateL(); //激活窗口使得可以在它上面繪圖
//初始化自己定義的活動(dòng)對(duì)象
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(); //獲得標(biāo)準(zhǔn)的繪圖上下文
// Gets the control's extent //獲得當(dāng)前控件大小的矩形
TRect drawRect(Rect());
// Clears the screen
gc.Clear(drawRect); //進(jìn)行屏幕清除操作

// 自己寫的繪圖代碼

//設(shè)置畫刷的風(fēng)格
//gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
//設(shè)置畫筆的風(fēng)格大小
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetPenSize(TSize(1,1));
//設(shè)置畫筆顏色
gc.SetPenColor(TRgb(255,0,0));

TInt x=aRect.Width()/10;
TInt y=aRect.Height()/4;
TPoint point(x,y);
//獲得系統(tǒng)的時(shí)間并且顯示出來
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();
//設(shè)置字體
gc.UseFont(normalfont);
//繪制文字
gc.DrawText(str,point);
//撤銷字體
gc.DiscardFont();
}

// -----------------------------------------------------------------------------
// CHelloGUIAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CHelloGUIAppView::SizeChanged()

{
DrawNow(); //視圖大小發(fā)生變化的時(shí)候,進(jìn)行重繪
}

// -----------------------------------------------------------------------------
// 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);//在支持觸摸屏的情況下,調(diào)用父類的指針事件響應(yīng)函數(shù)
}

void CHelloGUIAppView::ininMyActiveObject()

{

/**//*
* 此處無需再次創(chuàng)建一個(gè)活動(dòng)規(guī)劃器,因?yàn)镚UI框架中已經(jīng)存在
* */
//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() 處無需再次創(chuàng)建一個(gè)活動(dòng)對(duì)象規(guī)劃器,只有在非GUI程序中需要,因?yàn)镚UI框架中已經(jīng)存在,GUI應(yīng)用程序會(huì)自動(dòng)安裝和運(yùn)行活動(dòng)調(diào)度器,不用手動(dòng)安裝。如果重復(fù)創(chuàng)建活動(dòng)對(duì)象規(guī)劃器那么程序在運(yùn)行時(shí)候提示E32USER-CBase43錯(cuò)誤。