⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mylistboxappview.cpp

📁 symbian界面设计.关于LISTBOX的经典的例子,适合所有初学者和爱好SYMBIAN的工程师
💻 CPP
字号:
/*
============================================================================
 Name		: MyListBoxAppView.cpp
 Author	  : davey
 Copyright   : It is a software by davey
 Description : Application view implementation
============================================================================
*/

// INCLUDE FILES
#include <eikenv.h>
#include <eikappui.h>
#include <eiktxlbx.h>   // CEikTextListBox
#include <eiktxlbm.h>   // CTestListBoxModel
#include <badesca.h>    // for array of descriptors
#include <Bitmap.mbg>    // for the icon
//#include <QuartzKeys.h> // keys for jog dial


#include <gulicon.h>

#include "MyListBoxAppView.h"
#include "CustomListBox.h"

// CONSTANTS
_LIT(KIconFileName,    "\\Resource\\Bitmap.mbm");
_LIT(KMessage,         "Item #%d is clicked");
_LIT(KItemTextDouble1, "0\tDavey\tMy name");
_LIT(KItemTextDouble2, "1\tMin\tMy elder brother");
_LIT(KItemTextDouble3, "2\tXin\tMy little sister");
_LIT(KItemTextDouble4, "3\tSerena\tMy girl friend");
_LIT(KItemTextDouble5, "0\tWen\tMy teacher");
_LIT(KItemTextDouble6, "1\tYang\tMy little brother");

_LIT(KItemTextSingle1, "0\tFirst item");
_LIT(KItemTextSingle2, "1\tSecond item");
_LIT(KItemTextSingle3, "2\tThird item");
_LIT(KItemTextSingle4, "3\tFourth item");

// MEMBER FUNCTIONS

void CMyListBoxAppView::ConstructL(const TRect& aRect)
	{
	iIsDoubleLine = ETrue;
	CreateWindowL();
	CreateListBoxL();
	SetRect(aRect);
	ActivateL();
	}

CMyListBoxAppView::~CMyListBoxAppView()
	{
	delete iListBox;
	}

CMyListBoxAppView* CMyListBoxAppView::NewL( const TRect& aRect )
    {
    CMyListBoxAppView* self = CMyListBoxAppView::NewLC( aRect );
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// Cmsgpp_proAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CMyListBoxAppView* CMyListBoxAppView::NewLC( const TRect& aRect )
    {
    CMyListBoxAppView* self = new ( ELeave ) CMyListBoxAppView;
    CleanupStack::PushL( self );
    self->ConstructL( aRect );
    return self;
    }

void CMyListBoxAppView::UpdateListBoxL(TBool aIsDoubleLine)
	{
	iIsDoubleLine = aIsDoubleLine;
	CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(6);

	CleanupStack::PushL(array);
	if (aIsDoubleLine)
		{
		array->AppendL(KItemTextDouble1);
		array->AppendL(KItemTextDouble2);
		array->AppendL(KItemTextDouble3);
		array->AppendL(KItemTextDouble4);
		array->AppendL(KItemTextDouble5);
		array->AppendL(KItemTextDouble6);
		}
	else
		{
		array->AppendL(KItemTextSingle1);
		array->AppendL(KItemTextSingle2);
		array->AppendL(KItemTextSingle3);
		array->AppendL(KItemTextSingle4);
		}
	CleanupStack::Pop();

	iListBox->Model()->SetItemTextArray(array);

	// We need to set the current item index and call DrawNow()
	// so that when the user changes the list box to single/double,
	// it will be updated properly.
	iListBox->SetCurrentItemIndex(0);
	iListBox->DrawNow();
	}

void CMyListBoxAppView::SizeChanged()
	{
	if (iListBox)
		{
		iListBox->SetRect(Rect());
		}
	}

TInt CMyListBoxAppView::CountComponentControls() const
	{
	if (iListBox)
		{
		return 1;
		}
	return 0;
	}

CCoeControl* CMyListBoxAppView::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		case 0:
			return iListBox;
		default:
			return NULL;
		}
	}

void CMyListBoxAppView::Draw(const TRect& aRect) const
	{
	CWindowGc& gc = SystemGc();
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.SetBrushColor(KRgbWhite);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(aRect);
	}

TKeyResponse CMyListBoxAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
											   TEventCode aType)
	{
	if (iListBox)
		{
		if ((EEventKeyDown == aType)
			&& (EStdKeyEnter == aKeyEvent.iScanCode))
			{
			HandleListBoxEventL(
				iListBox, MEikListBoxObserver::EEventEnterKeyPressed);
			}
		}
	return EKeyWasNotConsumed;
	}

void CMyListBoxAppView::HandleListBoxEventL(CEikListBox* aListBox,
											TListBoxEvent aEventType)
	{
	if ((MEikListBoxObserver::EEventEnterKeyPressed == aEventType)
		|| (MEikListBoxObserver::EEventItemClicked == aEventType))
        {
		TBuf<30> buffer;
		buffer.Format(KMessage, aListBox->CurrentItemIndex());
		iEikonEnv->InfoMsg(buffer);
		}
	}

void CMyListBoxAppView::CreateListBoxL()
	{
	// Creates a new custom list box.
	iListBox = new (ELeave) CCustomListBox();
	iListBox->ConstructL(this,
		CEikListBox::ENoFirstLetterMatching
		| CEikListBox::ENoExtendedSelection);
	iListBox->SetListBoxObserver(this);

	// Fills the list box with some items.
	UpdateListBoxL(ETrue);

	// Creates a new icon array.
	CArrayPtr<CGulIcon>* iconArray = new (ELeave)
		CArrayPtrFlat<CGulIcon>(4);
	CleanupStack::PushL(iconArray);

	// Loads the icon and sets it as icon array for the list box.
	iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName,
		EMbmBitmapBmp1, EMbmBitmapBmp1_mask));
	iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName, 
		EMbmBitmapBmp2, EMbmBitmapBmp2_mask));
	iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName, 
		EMbmBitmapBmp3, EMbmBitmapBmp3_mask));
	iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName, 
		EMbmBitmapBmp4, EMbmBitmapBmp4_mask));
	CleanupStack::Pop(); // iconArray

	// Sets the icon array.
	CCustomListItemDrawer* itemDrawer = static_cast<CCustomListItemDrawer*>
		(iListBox->View()->ItemDrawer());
	itemDrawer->SetIconArray(iconArray); // transfer ownership

	// Sets the height of the list box item
	// (= 2 * maximum height of the icon).
	TSize size = itemDrawer->MaxIconSize();
	iListBox->SetItemHeightL(2 * size.iHeight);
	}
// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -