timercontainer.cpp

来自「《基于Symbian OS的手机开发与应用实践》这本书的配套源码。」· C++ 代码 · 共 121 行

CPP
121
字号
/*
* ============================================================================
*  Name     : CTimerContainer from TimerContainer.h
*  Part of  : Timer
*  Created  : 05.02.2006 by ToBeReplacedByAuthor
*  Implementation notes:
*     Initial content was generated by Series 60 Application Wizard.
*  Version  :
*  Copyright: ToBeReplacedByCopyright
* ============================================================================
*/

// INCLUDE FILES
#include <eiklabel.h>   // for CEikLabel

#include "TimerContainer.h"
#include "TimerModel.h"

// CONSTANTS
const TInt CTimerContainer::KTimeStringLength = 7;

// ================= MEMBER FUNCTIONS =======================

CTimerContainer* CTimerContainer::NewL(const TRect& aRect, const CTimerModel& aModel)
    {
    CTimerContainer* self = new (ELeave) CTimerContainer(aModel);
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    CleanupStack::Pop();
    return self;
    }

// Destructor
CTimerContainer::~CTimerContainer()
    {
    delete iLabel;
    }

// public new functions
void CTimerContainer::RefreshL()
    {
    TBuf<KTimeStringLength> timeString;
    const TTime& time = iModel.Time();
    time.FormatL(timeString, KTimeFormat);
    iLabel->SetTextL(timeString);
    DrawDeferred();
    }


// ---------------------------------------------------------
// CTimerContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CTimerContainer::CountComponentControls() const
    {
    return 1; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CTimerContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CTimerContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
            return iLabel;
        default:
            return NULL;
        }
    }

// ---------------------------------------------------------
// CTimerContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CTimerContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    // TODO: Add your drawing code here
    // example code...
    gc.SetPenStyle( CGraphicsContext::ENullPen );
    gc.SetBrushColor( KRgbGray );
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    gc.DrawRect( aRect );
    }


CTimerContainer::CTimerContainer(const CTimerModel& aModel)
 : iModel(aModel)
    {
    // No Implementation Required
    }


// ---------------------------------------------------------
// CTimerContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CTimerContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

    SetRect(aRect);

    iLabel = new(ELeave) CEikLabel;
    TBuf<KTimeStringLength> timeString;
    const TTime& time = iModel.Time();
    time.FormatL(timeString, KTimeFormat);
    iLabel->SetTextL(timeString);
    TSize size(iLabel->MinimumSize());
    TPoint position((aRect.Width()-size.iWidth)/2, (aRect.Height()-size.iHeight)/2);
    iLabel->SetPosition(position);

    ActivateL();
    }

// End of File  

⌨️ 快捷键说明

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