📄 listexamappview.cpp
字号:
/*
============================================================================
Name : ListExamAppView.cpp
Author : hou maoqing
Copyright : Copyright (c) Hou maoqing 2008
Description : Application view implementation
============================================================================
*/
// INCLUDE FILES
#include <coemain.h>
#include <badesca.h> // CDesCArray
#include <coecntrl.h> // CCoeControl
#include <eiklbo.h> // MEikListBoxObserver
#include <akniconarray.h> // CAknIcon
#include <aknlists.h> // CAknSingleStyleListBox
#include <barsread.h> // TResource Reader
#include <e32def.h> // STATIC_CAST
#include <eikclbd.h> // CColumnListBoxData
#include <ListExamIcon.mbg>
#include <ListExam_0xE82FFBEE.rsg>
#include "ListExamAppView.h"
// ============================ MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CListExamAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CListExamAppView* CListExamAppView::NewL(const TRect& aRect)
{
CListExamAppView* self = CListExamAppView::NewLC(aRect);
CleanupStack::Pop(self);
return self;
}
// -----------------------------------------------------------------------------
// CListExamAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CListExamAppView* CListExamAppView::NewLC(const TRect& aRect)
{
CListExamAppView* self = new ( ELeave ) CListExamAppView;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
// -----------------------------------------------------------------------------
// CListExamAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CListExamAppView::ConstructL(const TRect& aRect)
{
// Create a window for this application view
CreateWindowL();
CreateListL();
LoadListIconsL();
LoadListItemsL();
// Set the windows size
SetRect(aRect);
// Activate the window, which makes it ready to be drawn
ActivateL();
}
// -----------------------------------------------------------------------------
// CListExamAppView::CListExamAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CListExamAppView::CListExamAppView()
{
// No implementation required
}
// -----------------------------------------------------------------------------
// CListExamAppView::~CListExamAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CListExamAppView::~CListExamAppView()
{
delete iMainItemsListBox;
}
// -----------------------------------------------------------------------------
// CListExamAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CListExamAppView::Draw(const TRect& /*aRect*/) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
// Gets the control's extent
TRect drawRect(Rect());
// Clears the screen
gc.Clear(drawRect);
}
// -----------------------------------------------------------------------------
// CListExamAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CListExamAppView::SizeChanged()
{
TRect rect=Rect();
iMainItemsListBox->SetExtent (rect.iTl, rect.Size());
}
TInt CListExamAppView::CountComponentControls() const
{
return 1; // return nbr of controls inside this container
}
CCoeControl* CListExamAppView::ComponentControl( TInt aIndex ) const
{
switch (aIndex)
{
case 0:
return iMainItemsListBox;
default:
return NULL;
}
}
TKeyResponse CListExamAppView::OfferKeyEventL(
const TKeyEvent& aKeyEvent, TEventCode aType )
{
if (iMainItemsListBox)
return iMainItemsListBox->OfferKeyEventL (aKeyEvent, aType);
else
return EKeyWasNotConsumed;
}
void CListExamAppView::CreateListL()
{
// 创建列表实例
iMainItemsListBox = new (ELeave) CAknSingleGraphicStyleListBox;
iMainItemsListBox->SetContainerWindowL(*this);
// 从资源构造列表
TResourceReader reader;
CEikonEnv::Static()->CreateResourceReaderLC(reader, R_LISTBOX_EXAM);
iMainItemsListBox->ConstructFromResourceL(reader);
CleanupStack::PopAndDestroy(); // reader
// 给列表创建滚动条
iMainItemsListBox->CreateScrollBarFrameL();
iMainItemsListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
}
void CListExamAppView::LoadListIconsL()
{
// Get the name of the file containing the icons
TFileName fnIconFileName;
fnIconFileName.Append(_L("\\resource\\apps\\ListExamIcon.mbm"));
// Create an array of icons, reading them from the file
CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(2);
CleanupStack::PushL(icons);
icons->AppendL(iEikonEnv->CreateIconL(fnIconFileName, EMbmListexamiconIcon1, EMbmListexamiconIcon1_mask));
icons->AppendL(iEikonEnv->CreateIconL(fnIconFileName, EMbmListexamiconIcon2, EMbmListexamiconIcon2_mask));
CleanupStack::Pop(icons);
iMainItemsListBox->ItemDrawer()->ColumnData()->SetIconArray(icons); // passing ownership of icons
}
void CListExamAppView::LoadListItemsL()
{
CTextListBoxModel* model = iMainItemsListBox->Model(); // not taking ownership
model->SetOwnershipType (ELbmOwnsItemArray);
CDesCArray* mainItemsArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
//装入列表项
TBuf<32> bufItem;
bufItem.Append(_L("0\tFirst Item"));
mainItemsArray->AppendL (bufItem);
bufItem.Zero();
bufItem.Append(_L("1\tSecond Item"));
mainItemsArray->AppendL (bufItem);
}
void CListExamAppView::HandleListBoxEventL(CEikListBox* /*aListBox*/, TListBoxEvent aListBoxEvent)
{
// if the Select Key has been pressed
if ((aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
(aListBoxEvent == MEikListBoxObserver::EEventItemClicked))
{
}
}
void CListExamAppView::AddListitemL()
{
if (iMainItemsListBox)
{
CTextListBoxModel* model = iMainItemsListBox->Model();
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt nCurItem = iMainItemsListBox->CurrentItemIndex();
TBuf<32> bufItem;
bufItem.Append(_L("1\tthird Item"));
itemArray->AppendL (bufItem);
iMainItemsListBox->HandleItemAdditionL();
}
}
void CListExamAppView::DeleteSelectedL()
{
if (iMainItemsListBox)
{
CTextListBoxModel* model = iMainItemsListBox->Model();
if (model->NumberOfItems() > 0)
{
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt nCurItem = iMainItemsListBox->CurrentItemIndex();
itemArray->Delete(nCurItem);
AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
iMainItemsListBox,nCurItem,ETrue);
}
}
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -