SymbianER

symbian application developers' zone

时间条控件制作简明教程 Bookmark this page on deli.cio.us

    易智游戏中的时间条,格斗游戏中的血条,相信是大家在制作游戏当中经常遇到的元素. 现在用一简单的教程介绍如何用S60自定义控件完成它,该控件可以随意控制大小,控制时间长度.

    该控件将用到,窗口,如何制作自定义,时间周期类等知识.

    制作之前首先请大家看看 Nokia Forum 关于 自定义控件的原理和教程
    下载页面>>

   CPeriodic 的类的使用可以参考sdk中
    Developer Library >> API Reference >> C++ API reference >> Timers and Timing Services >> CPeriodic

1.类的定义:



Code:
class CMyTimerContainer : public CCoeControl
{
 public:
   
    void ConstructL(TInt aSecond ,TPoint aPoint, TSize aSize);// aSecond 初始的秒数,TPoint 控件所在位置; TSize控件大小
    ~CMyTimerContainer();
    void Draw(const TRect& aRect) const;

    static TInt Start__(TAny* aObject);    //周期函数
    void Start_();//周期函数
private: //data

  CPeriodic* iPeriodic;
  TInt iLeft; //剩余的时间
  TInt iTotal;//总共的时间
  TPoint iPoint; //控件所在位置,
  TSize iSize; //控件的大小
}
2.周期函数:


Code:
TInt CMyTimerContainer::Start__(TAny* aObject)
{
  ((CMyTimerContainer*)aObject)->Start_(); // cast, and call non-static function
    return 1;
}

void CMyTimerContainer::Start_()
{
  iLeft--;
  if(iLeft==0)
  {
    DrawNow();
    iPeriodic->Cancel();
    return;
  }
  DrawNow();
}
3.绘图函数
Code:

void CMyTimerContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();

    /*画背景*************************/
    gc.SetBrushColor( KRgbBlack );
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    gc.DrawRect(aRect);

    /*画进度条*************************/
     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
     gc.SetBrushColor( KRgbRed );
     gc.SetPenStyle( CGraphicsContext::ENullPen );
     gc.DrawRect( TRect(TSize(iLeft*Size().iWidth/iTotal,iSize.iHeight)) );
    }

4.控件的构造


Code:
void CMyTimerContainer::ConstructL(TInt aSecond ,TPoint aPoint, TSize aSize)
    {
    CreateWindowL();
    iTotal = aSecond;
    iLeft = iTotal;
    iPoint = aPoint;
    iSize = aSize;
    iPeriodic = CPeriodic::NewL(CActive::EPriorityIdle);
    iPeriodic->Start(0/*8000000*/, 1000000/*35714*/, TCallBack(Start__, this));
    SetExtent(aPoint,aSize);
    ActivateL();
    }

5.如何调用该控件


Code:
a,在XXXContainer.h中加入该控件
   private: //data
     CMyTimerContainer* iTimer;

b,在构造函数中构造其,秒数,位置,以及大小
void CXXXXContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

    iTimer =  new (ELeave) CMyTimerContainer;
    iTimer->ConstructL( 60/*init seconds*/,TPoint(0,100),TSize(176,15),this);

    SetRect(aRect);
    ActivateL();
    }

English Version: http://www.symbianer.com/post/custom-control-timeline-en.html
Chinese Version: http://www.symbianer.com/post/custom-control-timeline.html

至此一个时间控件就做好了,大家可以按照以上的步骤做一下.
本文源代码下载地址: 下载>> 
可以发 邮件: N-office@163.com
website: http://www.symbianer.com
QQ symbian 讨论群:623041

交流心得.

 

If you like this post then please consider subscribing to my full feed RSS. You can also subscribe by Email and have new posts sent directly to your inbox.

Post A Comment:

Recent Entries

Site Search

Recent Entries

Subscribe

    Enter your email address:

    Delivered by FeedBurner

Feeds

    Subscribe in Rojo
    Add to Google

    Subscribe in Bloglines
    Subscribe in NewsGator Online
    Add to Technorati Favorites
    Add symbianer-Symbian S60 Uiq Mobile Examples Codes to Newsburst from CNET News.com

    Add to My AOL
    Subscribe in FeedLounge
    Add to netvibes
    Add to The Free Dictionary
    Add to Bitty Browser
    Add to Plusmo
    Subscribe in NewsAlloy
    Add symbianer-Symbian S60 Uiq Mobile Examples Codes to ODEO
    Subscribe in podnova
    Add to Pageflakes

    Technology Blogs - Blog Top Sites

Communities


English Version Mobile Symbianer PowerBy SymbianER
Copyright 2006-2008 www.symbianer.com All Rights Reserved.