symbian自定义闪烁图片控件

来源:互联网 发布:专业mtv制作软件 编辑:程序博客网 时间:2024/05/17 04:58
下面是自定义的一个图片控件,实现的功能是在屏幕上显示一张一闪一闪的图片,并且能够根据用户输入上下左右键进行移动
加载图片,首先要在mmp文件中增添以下代码(红色为增添部分)
/*
============================================================================
 Name        : SelfControl.mmp
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : This is the project specification file for SelfControl.
============================================================================
*/
TARGET                                  SelfControl.app
TARGETTYPE                              app
UID                                     0x100039CE 0x0897af2b
TARGETPATH                              /system/apps/SelfControl
SOURCEPATH                              ../data
RESOURCE                                SelfControl.rss
RESOURCE                                SelfControl_caption.rss
SOURCEPATH                              ../src
SOURCE                                  CBlinkImage.cpp
SOURCE                                  SelfControlApp.cpp
SOURCE                                  SelfControlAppui.cpp
SOURCE                                  SelfControlDocument.cpp
SOURCE                                  SelfControlContainer.cpp
LANG                                    SC
USERINCLUDE                             .
USERINCLUDE                             ../inc
SYSTEMINCLUDE                           .
SYSTEMINCLUDE                           /epoc32/include
/* [+] */ LIBRARY                       fbscli.lib
LIBRARY                                 euser.lib
LIBRARY                                 apparc.lib
LIBRARY                                 cone.lib
LIBRARY                                 eikcore.lib
LIBRARY                                 eikcoctl.lib
LIBRARY                                 avkon.lib
AIF                                     SelfControl.aif ../aif SelfControlaif.rss c8 context_pane_icon.bmp context_pane_icon_mask.bmp list_icon.bmp list_icon_mask.bmp
START BITMAP bmp.mbm
  TARGETPATH                              //                      
   //目标文件夹,相对于手机Z盘来说的
  HEADER                                  
  SOURCEPATH                              ../aif                     //图片所在文件夹,这个路径是相对于该mmp文件所在的路径而言的
  SOURCE                                  c12 image1.bmp       
//图片名为image1.bmp
END 
 
 
编译该工程后会在z盘目录下产生一个bmp.mbm文件以及在epoc32/include目录下产生bmp.mbg文件
 
bmp.mbg文件内容如下:
// bmp.mbg
// Generated by BitmapCompiler
// Copyright (c) 1998-2001 Symbian Ltd.  All rights reserved.
//
enum TMbmBmp
 {
 EMbmBmpImage1//在源程序中访问那张图片要用这个标识
 };
 
 
头文件:
#ifndef CBLINKIMAGE_H
#define CBLINKIMAGE_H
#include<coecntrl.h>
#include "fbs.h"
class CBlinkImage:public CCoeControl
{
  public:
     CBlinkImage(CCoeControl& aParent);
        void ConstructL(CCoeControl& aParent);
     ~CBlinkImage();
  void Draw(const TRect& aRect) const;
  void Start(TTimeIntervalMicroSeconds32 aInterval);
  void Stop();
 private:
   void SizeChanged();
  static TInt Tick(TAny* aThis);
  void DoTick();
  
  private:
        CFbsBitmap* m_pMap;
        CPeriodic* iTicker;
        //CCoeControl* m_pContainer;
        int m_Count;
         bool m_bDrawBitmap;      
};
#endif
 
 
源文件:
#include "CBlinkImage.h"
#include "C:/Symbian/8.0a/S60_2nd_FP2_SC/epoc32/include/bmp.mbg"//为访问图片
#include "fbs.h"
#include "e32math.h"
CBlinkImage::CBlinkImage(CCoeControl& aParent)
:m_bDrawBitmap(false),m_pList(0)
{
   ConstructL(aParent);
}
void CBlinkImage::ConstructL(CCoeControl& aParent)
{
   _LIT(sFilePath,"Z://bmp.mbm"); 
    //编译后产生的bmp.mbm文件所在位置,注意是相对于symbian操作系统的z盘来设置的。这个文件在我的电脑上的路径其实是  
    // C:/Symbian/8.0a/S60_2nd_FP2_SC/epoc32/release/wins/udeb/z/bmp.mbm
   SetContainerWindowL(aParent);//设置图片控件的父亲控件
   m_pMap = new(ELeave) CFbsBitmap();
   User::LeaveIfError(m_pMap->Load(sFilePath,EMbmBmpImage1));//加载图片
   iTicker = CPeriodic::NewL(CActive::EPriorityIdle);
   this->SetRect(TRect( TPoint(20,20),TSize(40,40) ));
   Start(100000);
   ActivateL();
}

CBlinkImage::~CBlinkImage()
{
   if(iTicker)
   iTicker->Cancel();
   if(m_pMap)
   delete m_pMap;
}

void CBlinkImage::SizeChanged()
{
}
void CBlinkImage::Draw(const TRect& aRect) const
{////根据m_bDrawBitmap的值来决定怎样画这个控件
  if (m_bDrawBitmap)
 {
  TRect rt = Rect();
  SystemGc().BitBlt(rt.iTl, m_pMap);//画图
 }
 else
 {
    SystemGc().Clear();//清空屏幕
 }
}
void CBlinkImage::Start(TTimeIntervalMicroSeconds32 aInterval)
{
 iTicker->Start(aInterval, aInterval, TCallBack(Tick, this));
}
void CBlinkImage::Stop()
{
   iTicker->Cancel();
}
TInt CBlinkImage::Tick(TAny* aThis)
{
 static_cast<CBlinkImage*>(aThis)->DoTick();
 return 0;
}
void CBlinkImage::DoTick()
{
  //每次调用DoTick()函数都会改变当前m_bDrawBitmap的值,然后调用DrawNow()函数重画图片,就能出现一闪一闪的效果
     if(m_pMap)
 {
  if (m_bDrawBitmap==false)
  {
   m_bDrawBitmap = true;
  }
  else
  {
   m_bDrawBitmap = false;
  }
    DrawNow();//每次调用DrawNow()函数,框架会自动调用Draw()函数。
 }
 }
 
 
 
在类container中调用创建这个自定义的闪烁图标控件:
SelfControlContainer.cpp文件:
/*
============================================================================
Name        : CSelfControlContainer from SelfControlContainer.h
Author      :
Version     :
Copyright   : Your copyright notice
Description : Container control implementation
============================================================================
*/
// INCLUDE FILES
#include "SelfControlContainer.h"
#include "CBlinkImage.h"
#include <eiklabel.h>  // for example label control

// ================= MEMBER FUNCTIONS =======================
// ---------------------------------------------------------
// CSelfControlContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CSelfControlContainer::ConstructL(const TRect& aRect)
{
 CreateWindowL();
 //x=10;
 iLabel = new (ELeave) CEikLabel;
 iLabel->SetContainerWindowL( *this );
 iLabel->SetTextL( _L("Example View") );
 iToDoLabel = new (ELeave) CEikLabel;
 iToDoLabel->SetContainerWindowL( *this );
 iToDoLabel->SetTextL( _L("Add Your controls/n here") );
 m_pBmp = new (ELeave) CBlinkImage( *this );

 SetRect(aRect);
 ActivateL();
}
// Destructor
CSelfControlContainer::~CSelfControlContainer()
{
 delete iLabel;
 delete iToDoLabel;
 delete m_pBmp;
}
// ---------------------------------------------------------
// CSelfControlContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CSelfControlContainer::SizeChanged()
{
 // TODO: Add here control resize code etc.
 iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() );
 iToDoLabel->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize() );
}
// ---------------------------------------------------------
// CSelfControlContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CSelfControlContainer::CountComponentControls() const
{
 return 3; // return nbr of controls inside this container
}
// ---------------------------------------------------------
// CSelfControlContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CSelfControlContainer::ComponentControl(TInt aIndex) const
{
 switch ( aIndex )
 {
 case 0:
  return iLabel;
 case 1:
  return iToDoLabel;
 case 2:
  return m_pBmp;
 default:
  return NULL;
 }
}
// ---------------------------------------------------------
// CSelfControlContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CSelfControlContainer::Draw(const TRect& aRect) const
{
 CWindowGc& gc = SystemGc();
 gc.Clear();
}
// ---------------------------------------------------------
// CSelfControlContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CSelfControlContainer::HandleControlEventL(
 CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
 // TODO: Add your control event handler code here
}
 
//响应按键事件
TKeyResponse CSelfControlContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
 TRect rt = m_pBmp->Rect();
 switch(aKeyEvent.iCode)
 {
 case EKeyDownArrow:
  rt.Move(0,3);
  m_pBmp->SetRect(rt);
  DrawNow();
  return EKeyWasConsumed;
  break;
 case EKeyLeftArrow:
  rt.Move(-3,0);
  m_pBmp->SetRect(rt);
  DrawNow();
  return EKeyWasConsumed;
  break;
 case EKeyUpArrow:
  rt.Move(0,-3);
  m_pBmp->SetRect(rt);
  DrawNow();
  return EKeyWasConsumed;
  break;
 case EKeyRightArrow:
  rt.Move(3,0);
  m_pBmp->SetRect(rt);
  DrawNow();
  return EKeyWasConsumed;
  break;
 default:
  break;
 }
 return EKeyWasNotConsumed;
}
原创粉丝点击