第8天 Symbian学习之开发实践:简单控件和复合控件的使用

Symbian学习笔记
日期:2008/4/3
学习目标: 掌握简单控件与复合控件的显示和操作方法
学习内容: 简单控件和复合控件
练习: 多一个复合控件的的程序,要求复合控件中有三个子控件,且三个子控件用不同的颜色进行填充

一、简单控件
简单控件派生于CCoeControl,主要方法有:
初始化方法;
void setRect(TRect& aRect)/void SizeChanged();//区域大小及遇到变化时处理
void Draw(const TRect& aRect) const;//绘制
void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);//事件处理

二、复合控件
复合控件也成为容器,即多个控件的一个所有者,这些个子控件就摆放在这个复合控件上。复合控件一般只有一个窗口对象,所有的子控件共享这个窗口。
子控件可以是简单控件也可以是复合控件。
子控件相关函数方法:
SetContainerWindowL(constCCoeControl&aContainer)//设置窗口
OfferKeyEventL()//按键消息事件
复合控件相关函数方法:
CreateWindowL()//创建主窗口。
TInt CCoecontrol::CountComponentControls() const//返回子控件的个数
CCoeControl* CCoeControl::ComponentControl(TIntaIndex) const//子控件的一个枚举
三、总结
所有的屏幕操作都在控件上执行,所有的控件又都派生于CCoeControl。
控件包含:初始化,绘画/重新绘制,处理输入 3个过程;
需要的注意的是:初始化时,需要先给控件确定一个窗口,再进行设计大小等操作。
即在子控件的构造方法ConstructL里需要进行SetContainerWindowL。 

四、附件代码

 

/*
* ============================================================================
*  Name     : CMyControlViewsContainer from MyControlViewsContainer.h
*  Part of  : MyControlViews
*  Created  : 03.04.2008 by 
*  Description:
*     Declares container control for application.
*  Version  :
*  Copyright: 
* ============================================================================
*/


#ifndef MYCONTROLVIEWSCONTAINER_H
#define  MYCONTROLVIEWSCONTAINER_H

//  INCLUDES
#include  < coecntrl.h >
#include 
" CFirstControl.h "    
#include 
" CSecondControl.h "  
#include 
" ThirdControl.h "
//  FORWARD DECLARATIONS
class  CEikLabel;         //  for example labels
class  CFirstControl;
class  CSecondControl;
class  CThirdControl;
//  CLASS DECLARATION

/**
*  CMyControlViewsContainer  container control class.
*  
*/

class  CMyControlViewsContainer :  public  CCoeControl, MCoeControlObserver
    
{
    
public// Constructors and destructor
        
        
/**
        * EPOC default constructor.
        * @param aRect Frame rectangle for container.
        
*/

        
void ConstructL(const TRect& aRect);

        
/**
        * Destructor.
        
*/

        
~CMyControlViewsContainer();

    
public// New functions

    
public// Functions from base classes

    
private// Functions from base classes

       
/**
        * From CoeControl,SizeChanged.
        
*/

        
void SizeChanged();

       
/**
        * From CoeControl,CountComponentControls.
        
*/

        TInt CountComponentControls() 
const;

       
/**
        * From CCoeControl,ComponentControl.
        
*/

        CCoeControl
* ComponentControl(TInt aIndex) const;

       
/**
        * From CCoeControl,Draw.
        
*/

        
void Draw(const TRect& aRect) const;

           
        
/**
        * From MCoeControlObserver
        * Acts upon changes in the hosted control's state. 
        *
        * @param    aControl    The control changing its state
        * @param    aEventType    The type of control event 
        
*/

        
void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);
        
    
private//data
        
        CEikLabel
* iLabel;          // example label
        CFirstControl* iFc;
        CEikLabel
* iToDoLabel;      // example label
        CSecondControl* iSc;
        CThirdControl
* iTc;
    }
;

#endif

//  End of File
/*
* ============================================================================
*  Name     : CMyControlViewsContainer from MyControlViewsContainer.h
*  Part of  : MyControlViews
*  Created  : 03.04.2008 by 
*  Implementation notes:
*     Initial content was generated by Series 60 Application Wizard.
*  Version  :
*  Copyright: 
* ============================================================================
*/


//  INCLUDE FILES
#include  " MyControlViewsContainer.h "

#include 
< eiklabel.h >    //  for example label control


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

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

    iLabel 
= new (ELeave) CEikLabel;
    iLabel
->SetContainerWindowL( *this );
    iLabel
->SetTextL( _L("") );
    iFc 
= CFirstControl::NewL(TRect(10,20,30,40),this);
    iSc 
= CSecondControl::NewL(TRect(20,20,30,40),this);
    iTc 
= CThirdControl::NewL(TRect(20,20,30,40),this);
    iToDoLabel 
= new (ELeave) CEikLabel;
    iToDoLabel
->SetContainerWindowL( *this );
    iToDoLabel
->SetTextL( _L("iToDoLabel:     here") );
    
    SetRect(aRect);
    ActivateL();
    }


//  Destructor
CMyControlViewsContainer:: ~ CMyControlViewsContainer()
    
{
    delete iLabel;
    delete iFc;
    delete iToDoLabel;
    delete iSc;
    delete iTc;
    }


//  ---------------------------------------------------------
//  CMyControlViewsContainer::SizeChanged()
//  Called by framework when the view size is changed
//  ---------------------------------------------------------
//
void  CMyControlViewsContainer::SizeChanged()
    
{
    
// TODO: Add here control resize code etc.
    iLabel->SetExtent( TPoint(10,10), TSize(100,10)/*iLabel->MinimumSize() */);
    iFc
->SetExtent( TPoint(10,30), TSize(200,200) );
    iToDoLabel
->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize());
    }


//  ---------------------------------------------------------
//  CMyControlViewsContainer::CountComponentControls() const
//  ---------------------------------------------------------
//
TInt CMyControlViewsContainer::CountComponentControls()  const
    
{
    
return 5// return nbr of controls inside this container
    }


//  ---------------------------------------------------------
//  CMyControlViewsContainer::ComponentControl(TInt aIndex) const
//  ---------------------------------------------------------
//
CCoeControl *  CMyControlViewsContainer::ComponentControl(TInt aIndex)  const
    
{
    
switch ( aIndex )
        
{
        
case 0:
            
return iLabel;
        
case 1:
            
return iFc;
        
case 2:
            
return iToDoLabel;
        
case 3:
            
return iSc;
        
case 4:
            
return iTc;
        
default:
            
return NULL;
        }

    }


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


//  ---------------------------------------------------------
//  CMyControlViewsContainer::HandleControlEventL(
//      CCoeControl* aControl,TCoeEvent aEventType)
//  ---------------------------------------------------------
//
void  CMyControlViewsContainer::HandleControlEventL(
    CCoeControl
*   /*aControl*/ ,TCoeEvent  /*aEventType*/ )
    
{
    
// TODO: Add your control event handler code here
    }



//  End of File  
//  CFirstControl.h: interface for the CFirstControl class.
//
//

#if  !defined(AFX_CFIRSTCONTROL_H__2533836F_568A_4F35_824A_11B2D8EB84DC__INCLUDED_)
#define  AFX_CFIRSTCONTROL_H__2533836F_568A_4F35_824A_11B2D8EB84DC__INCLUDED_

#if  _MSC_VER > 1000
#pragma  once
#endif   //  _MSC_VER > 1000
#include 
< coecntrl.h >

class  CFirstControl :  public  CCoeControl  
{
public:
    CFirstControl();
    
static CFirstControl* NewL(const TRect& aRect,CCoeControl* aParrent);
    
virtual ~CFirstControl();
    
void ContructL(const TRect& aRect,CCoeControl* aParrent);
    
void SizeChanged();
    
void Draw(const TRect& aRect) const;
}
;

#endif   //  !defined(AFX_CFIRSTCONTROL_H__2533836F_568A_4F35_824A_11B2D8EB84DC__INCLUDED_)
//  CFirstControl.cpp: implementation of the CFirstControl class.
//
//

#include 
" CFirstControl.h "

//
//  Construction/Destruction
//

CFirstControl::CFirstControl()
{
    
//ContructL(TRect(30,30,50,50));
}

CFirstControl
*  CFirstControl::NewL( const  TRect &  aRect,CCoeControl *  aParrent)
{
    CFirstControl
* self = new (ELeave) CFirstControl();
    CleanupStack::PushL(self);
    self
->ContructL(aRect,aParrent);
    CleanupStack::Pop(self);    
    
return self;
}

CFirstControl::
~ CFirstControl()
{
    
}

void  CFirstControl::ContructL( const  TRect &  aRect,CCoeControl *  aParrent)
{
    
//CreateWindowL();
    SetContainerWindowL(*aParrent);
    SetRect(aRect);
    ActivateL();
}

void  CFirstControl::SizeChanged()
{
    
//this->SetRect()
}

void  CFirstControl::Draw( const  TRect &   /*aRect*/ ) const
{
    CWindowGc
& gc = SystemGc();
    
//gc.Clear(aRect);
    gc.SetPenStyle( CGraphicsContext::ESolidPen);
    gc.SetPenColor(KRgbGreen);
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush);
    gc.SetBrushColor(KRgbGreen);
    TRect t;
    t.SetRect(
20,20,100,300) ;
    gc.DrawEllipse(t);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值