轉控件

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);
}

發佈了11 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章