复合控件的练习

复合控件的练习

练习了一下复合控件,其实就是用传统编程模式下添加 CEikLable ,CEikEdwin,CEikTextListBox 控件

对于 CEikLable和CEikTextListBox 没有用到资源文件,CEikEdwin 用的是资源文件,对于读写 CEikTextListBox 是新的知识,及移动

CEikEdwin 与 CEikTextListBox 控件的焦点

代码:头文件

#ifndef STACKCONTAINER_H
#define STACKCONTAINER_H
// INCLUDES
#include <coecntrl.h>
// FORWARD DECLARATIONS
class CEikLabel; // for example labels
class CEikEdwin;
class CEikTextListBox;
// CLASS DECLARATION
/**
* CStackContainer container control class.
*
*/
class CStackContainer : public CCoeControl, MCoeControlObserver
{
public: // Constructors and destructor
/**
* EPOC default constructor.
* @param aRect Frame rectangle for container.
*/
void ConstructL(const TRect& aRect);
/**
* Destructor.
*/
~CStackContainer();
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);
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
public:
void HandleCommandL(TInt aCommand);
private: //data
CEikLabel* iLPop; // example label
CEikLabel* iLPush; // example label
CEikLabel* iLContent;
CEikEdwin* iPushNumber;
CEikTextListBox* iListBox;
};
#endif
// End of File

代码文件:

/*
* ============================================================================
* Name : CStackContainer from StackContainer.h
* Part of : Stack
* Created : 11.05.2010 by
* Implementation notes:
* Initial content was generated by Series 60 Application Wizard.
* Version :
* Copyright:
* ============================================================================
*/
// INCLUDE FILES
#include "StackContainer.h"
#include <eiklabel.h> // for example label control
#include <eikedwin.h>
#include <Stack.rsg>
#include <coemain.h>
#include <aknlists.h>
#include <barsread.h>
#include <badesca.h> // CDesCArrayFlat
#include <e32keys.h>
#include <eikmsg.h> // iEikonEnv
#include <Stack.hrh>
#include <eiklbv.h>
#define push_label TPoint(5,5)
#define pop_label TPoint(5,35)
#define push_edit TPoint(88,5)
#define pop_content TPoint(88,35)
#define list_pos TPoint(0,60)
#define text_size TSize(60,20)
#define listbox_size TSize(176,80)
// ================= MEMBER FUNCTIONS =======================
// ---------------------------------------------------------
// CStackContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CStackContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
iLPush = new (ELeave) CEikLabel;
iLPush->SetContainerWindowL( *this );
iLPush->SetTextL( _L("To Push") );
iLPop = new (ELeave) CEikLabel;
iLPop->SetContainerWindowL( *this );
iLPop->SetTextL( _L("Poped") );
iLContent = new (ELeave)CEikLabel;
iLContent->SetContainerWindowL(*this);
iLContent->SetTextL(_L("None"));
TResourceReader aReader;
iCoeEnv->CreateResourceReaderLC(aReader,R_MY_ED);
iPushNumber = new (ELeave) CEikEdwin;
iPushNumber->ConstructFromResourceL(aReader);
CleanupStack::PopAndDestroy();
iPushNumber->SetContainerWindowL(*this);
iPushNumber->SetBorder(TGulBorder::ESingleBlack);
iPushNumber->SetFocus(ETrue);
iListBox = new (ELeave)CAknSingleNumberStyleListBox;
iListBox->SetContainerWindowL(*this);
iListBox->ConstructL(this,0);
CDesCArrayFlat* array = new (ELeave)CDesCArrayFlat(5);
CleanupStack::PushL(array);
array->AppendL(_L("\tBottom of Stack"));
CleanupStack::Pop();
iListBox->Model()->SetItemTextArray(array);
iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
iListBox->SetBorder(TGulBorder::EShallowRaised);
iListBox->ActivateL();
iListBox->CreateScrollBarFrameL(ETrue);
iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn,CEikScrollBarFrame::EAuto);
SetRect(aRect);
ActivateL();
}
// Destructor
CStackContainer::~CStackContainer()
{
delete iLPush;
delete iLPop;
delete iLContent;
delete iPushNumber;
delete iListBox;
}
// ---------------------------------------------------------
// CStackContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CStackContainer::SizeChanged()
{
// TODO: Add here control resize code etc.
//iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() );
//iToDoLabel->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize() );
iLPush->SetExtent(push_label,text_size);
iLPop->SetExtent(pop_label,text_size);
iLContent->SetExtent(pop_content,text_size);
iPushNumber->SetExtent(push_edit,text_size);
iListBox->SetExtent(list_pos,listbox_size);
}
// ---------------------------------------------------------
// CStackContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CStackContainer::CountComponentControls() const
{
  return 5; // return nbr of controls inside this container
}
// ---------------------------------------------------------
// CStackContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CStackContainer::ComponentControl(TInt aIndex) const
{
switch ( aIndex )
{
case 0:
  return iLPush;
case 1:
  return iLPop;
case 2:
  return iLContent;
case 3:
  return iListBox;
case 4:
  return iPushNumber;
default:
  return NULL;
}
}
// ---------------------------------------------------------
// CStackContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CStackContainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
// TODO: Add your drawing code here
// example code...
gc.SetPenStyle( CGraphicsContext::ENullPen );
gc.SetBrushColor( KRgbGray );
gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
gc.DrawRect( aRect );
}
// ---------------------------------------------------------
// CStackContainer::HandleControlEventL(
// CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CStackContainer::HandleControlEventL(
CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
// TODO: Add your control event handler code here
}
TKeyResponse CStackContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (aType != EEventKey )
{
return EKeyWasNotConsumed;
}
if (iPushNumber && iPushNumber->IsFocused())
{
if (aKeyEvent.iCode==EKeyDownArrow)
{
iPushNumber->SetFocus(EFalse);
iListBox->SetFocus(ETrue);
return EKeyWasConsumed;
}
else
{
iPushNumber->OfferKeyEventL(aKeyEvent,aType);
}
}
else if (iListBox && iListBox->IsFocused())
{
if (aKeyEvent.iCode==EKeyUpArrow)
{
iListBox->SetFocus(EFalse);
iPushNumber->SetFocus(ETrue);
return EKeyWasConsumed;
}
else
if (aKeyEvent.iCode==EKeyLeftArrow)
{
iEikonEnv->InfoMsg(_L("left arrow"));
return EKeyWasConsumed;
}
else
if (aKeyEvent.iCode==EKeyRightArrow)
{
iEikonEnv->InfoMsg(_L("right arrow"));
return EKeyWasConsumed;
}
else
{
iPushNumber->OfferKeyEventL(aKeyEvent,aType);
}
}
return EKeyWasNotConsumed;
}
void CStackContainer::HandleCommandL(TInt aCommand)
{
if (aCommand==EStackCmdPush)
{
TBuf<32> buf ;
buf.Zero();
iPushNumber->GetText(buf);
if (buf.Length()==0)
{
iEikonEnv->InfoMsg(_L("can't zero length"));
return;
}
buf.Insert(0,_L("\t"));
CDesCArray* array =
static_cast<CDesCArray*>(iListBox->Model()->ItemTextArray());
array->InsertL(0,buf);
buf.Zero();
iPushNumber->SetTextL(&buf);
iListBox->HandleItemAdditionL();
}
else if (aCommand==EStackCmdPop)
{
CDesCArray* array = static_cast<CDesCArray*>(iListBox->Model()->ItemTextArray());
if (array->Count()==1)
{
iEikonEnv->InfoMsg(_L("stack is empty"));
return;
} else
{
TBuf<32> buf;
TPtrC ptr = (*array)[0];
buf.Copy(ptr);
buf.Delete(0,1);
iEikonEnv->InfoMsg(buf);
iLContent->SetTextL(buf);
array->Delete(0);
iListBox->HandleItemRemovalL();
}
}
}
// End of File
 

其中对于 HandleCommand 是要在 AppUi 中的 handleCommand 来调用的
如下所示:

void CStackAppUi::HandleCommandL(TInt aCommand)
{
switch ( aCommand )
{
case EAknSoftkeyBack:
case EEikCmdExit:
{
Exit();
break;
}
case EStackCmdAppTest:
{
iEikonEnv->InfoMsg(_L("test"));
break;
}
// TODO: Add Your command handling code here
case EStackCmdPush:
case EStackCmdPop:
{
iAppContainer->HandleCommandL(aCommand);
break;
}
default:
break;
}
}
 



安平2009@原创
qi_jianzhou@126.com

原文地址:https://www.cnblogs.com/zziss/p/1732835.html