⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.cpp

📁 symbian的例子程序
💻 CPP
字号:
// Timer.cpp
//
// Copyright (c) 2005 CCNIIT.  All rights reserved.

//Author: Hewei
// version 1.0
//Date: 2005-10-20

// Example shows attempt to use avctive object to display local timer 
// NOTE: the structure of this example is different to standard E32 examples

#include <e32cons.h>
#include <e32std.h>
#include <e32des16.h>

  // All messages written to this
LOCAL_D CConsoleBase* console;

// Function prototypes
 void callExampleL();
 void doExampleL();
 

/////////////////////////////////////////////////////////////////////////////////
// CActiveTimer derives from Cactive, is an active object.
// CactiveConsole definition:
//
////////////////////////////////////////////////////////////////////////////////
class CActiveTimer : public CTimer
 {
 
 public:
	static CActiveTimer* CActiveTimer::NewL(CConsoleBase* aConsole);
	static CActiveTimer* CActiveTimer::NewLC(CConsoleBase* aConsole);
	 ~CActiveTimer();
 private:
	 CActiveTimer();
	 void ConstructL(CConsoleBase* aConsole);
 public:
	 void Start();
 private:	 
	 void RunL();
	 void Cancel();
	 void DoCancel();
	 void Stop();
private:
	 CConsoleBase* iConsole;
	 TTime iTime;
	 RTimer iTimer;
	 TBool iStop;
};
////////////////////////////////////////////////////////////////////////////////
//
//CActiveTimer implement
// 
////////////////////////////////////////////////////////////////////////////////
CActiveTimer* CActiveTimer::NewL(CConsoleBase* aConsole)
{
	CActiveTimer* self=CActiveTimer::NewLC( aConsole);
	CleanupStack::Pop(self);
	return self;
}
CActiveTimer* CActiveTimer::NewLC(CConsoleBase* aConsole)
{
	
	CActiveTimer* self=new (ELeave) CActiveTimer();
	CleanupStack::PushL(self);
	self->ConstructL(aConsole);
	return self;
}
	
CActiveTimer::CActiveTimer()
	 :CTimer(CActive::EPriorityStandard)
 {
	 iStop=FALSE;
	 
 }
void CActiveTimer::ConstructL(CConsoleBase* aConsole)
{
	CTimer::ConstructL();
	iConsole=aConsole;
	iTimer.CreateLocal();
    CActiveScheduler::Add(this);
}
 
 void CActiveTimer::Start()
 {
    if(IsActive())
   		return;
	 iTimer.After(iStatus,1000);
	 SetActive();
	
 }
 
 void CActiveTimer::RunL()
 {
	 if(iStop==FALSE)
	 {

	 iTime.HomeTime();
	 iConsole->ClearScreen();
	 TDay day=iTime.DayNoInWeek();
	 TDayName Dname;
	 Dname.Set(day);
     Dname.ZeroTerminate();
     TDateTime dt;
     dt=iTime.DateTime();
     _LIT(KTIME,"today is:%d-%d-%d\n %s\n%d:%d:%d\n");
     iConsole->Printf(KTIME,dt.Year(),dt.Month()+1,dt.Day()+1,Dname.Ptr(),dt.Hour(),dt.Minute(),dt.Second());
     Start();
	 }
	 else
		 CActiveScheduler::Stop();
   
}
 void CActiveTimer::Stop()
 {
	 iStop=TRUE;
 }
 
 void CActiveTimer::Cancel()
 {
    DoCancel();
 }
 void CActiveTimer::DoCancel()
 {
	 CTimer::DoCancel();
 }
 CActiveTimer::~CActiveTimer()
 {
	 Cancel();
	 iConsole=NULL;
	 iTimer.Close();
 }
 ////////////////////////////////////////////////////////////////////////////////////////
 //
 //CActiveConsole class definition
 //
 /////////////////////////////////////////////////////////////////////////////////////////
 class CActiveConsole : public CActive
 {
 public:
    static CActiveConsole* NewL(CConsoleBase* aConsole);
	static CActiveConsole* NewLC(CConsoleBase* aConsole);
 private:
	 CActiveConsole(CConsoleBase* aConsole);
	 void ConstructL();
	 ~CActiveConsole();
 public:
	 void Start();
	 void ProcessKeyInput(TChar aChar);
 private:	 
	 void RunL();
	 void Cancel();
	 void DoCancel();

 private:
	 CConsoleBase* iConsole;
	
};
////////////////////////////////////////////////////////////////////////////////
//
//CActiveConsole implement
// 
////////////////////////////////////////////////////////////////////////////////
 CActiveConsole* CActiveConsole::NewL(CConsoleBase* aConsole)
 {
  CActiveConsole* self=CActiveConsole::NewL(aConsole);
   CleanupStack::Pop(self);
   return self;
 }
 CActiveConsole* CActiveConsole::NewLC(CConsoleBase* aConsole)
 {
	 
	 CActiveConsole* self=new (ELeave)CActiveConsole(aConsole);
	 CleanupStack::PushL(self);
	 self->ConstructL();
	 return self;
 }
 CActiveConsole::CActiveConsole(CConsoleBase* aConsole)
 :CActive(EPriorityStandard)
 {
	 iConsole=aConsole;

 }
 void CActiveConsole::ConstructL()
 {
	
	 CActiveScheduler::Add(this);
 }
 void CActiveConsole::Start()
 {
     iConsole->Read(iStatus);
	 SetActive();
 }
 void CActiveConsole::ProcessKeyInput(TChar aChar)
 {
     if(aChar==EKeyEscape)
	 {
         CActiveScheduler::Stop();
		 return;
	 }
	
	 iConsole->Read(iStatus);
     SetActive();
 }
 
 void CActiveConsole::RunL()
 {
   
	ProcessKeyInput((TChar)iConsole->KeyCode());
 }

 void CActiveConsole::Cancel()
 {
    DoCancel();
 }
 void CActiveConsole::DoCancel()
 {
	iConsole->ReadCancel();
 }
 CActiveConsole::~CActiveConsole()
 {
	
	 iConsole=NULL;
 }
 ////////////////////////////////////////////////////////////////////////////////
 //
 //use CActiveConsole object
 //
 /////////////////////////////////////////////////////////////////////////////////
GLDEF_C TInt E32Main()
    {
	  // Get cleanup stack
	CTrapCleanup* cleanup=CTrapCleanup::New();

	 // Some more initialization, then do the example
    TRAPD(error,doExampleL());
	// callExampleL() should never leave.
	_LIT(KMsgPanicEpoc32ex,"EPOC32EX");
	__ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
   // destroy the cleanup stack
	delete cleanup;
	// return
	return 0;
    }
//////////////////////////////////////////////////////////////////////////////
//
//doExample() and callExample() impletement
//
//////////////////////////////////////////////////////////////////////////////
void doExampleL()
{
  	//Get Console;
	_LIT(KMsgExampleCode,"Symbian OS Example Code");
	console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
		// Put console onto the cleanup stack.
	CleanupStack::PushL(console);

	int error;
	TRAP(error,callExampleL());
	//TRAPD(error,callExampleL());
    if(error)
	{
		_LIT(KERROR,"error occured!\n");
		console->Printf(KERROR);
	
	}
	else{
      _LIT(KNOLEAVE,"No Leave!\n");
		console->Printf(KNOLEAVE);
	}
	console->Getch();
		// Remove the console object from the cleanupstack
	  // and destroy it. 	
	CleanupStack::PopAndDestroy();
}

void callExampleL() 
    {
     CActiveScheduler* AS=new (ELeave)CActiveScheduler;
	 CleanupStack::PushL(AS);
	 CActiveScheduler::Install(AS);

	 CActiveConsole* con=CActiveConsole::NewLC(console);
	 con->Start();

	
	 CActiveTimer* tm=CActiveTimer::NewLC(console);
	 tm->Start();

	 CActiveScheduler::Start();
	 CleanupStack::PopAndDestroy();
}












⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -