一、简单控件 简单控件派生于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);
}
|