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

📄 ndlist.cpp

📁 symbian 二版本的自定义控件
💻 CPP
字号:
#include "NDList.h"

#define KMAXNUM 5

CNDList::CNDList()
{
	iItemArray = NULL;
	iIcon = NULL;
	iIconSelect = NULL;
	iIndex = 0;
	iCount = 0;
	iStart = 0;
}
void CNDList::ContructL(CFbsBitmap* aIcon,CFbsBitmap* aIconSelect)
{
	iIcon = aIcon;
	iIconSelect = aIconSelect;
	iItemArray = new(ELeave) RPointerArray<CNDListItem>(10);
}
// 析构函数
CNDList::~CNDList()
{
	if(iIcon)
	{
		delete iIcon;
		iIcon = NULL;
	}
	if(iIconSelect)
	{
		delete iIconSelect;
		iIconSelect = NULL;
	}
	if(iItemArray)
	{
		iItemArray->ResetAndDestroy();
		delete iItemArray;
		iItemArray = NULL;
	}
}
void CNDList::SetItemArray(CDesC16ArrayFlat* aArray)
{
	iCount = aArray->Count();
	for(TInt i=0;i<iCount;i++)
	{
		CNDListItem* listItem = new CNDListItem;
		listItem->SetContent((*aArray)[i]);
		listItem->SetSelected(EFalse);
		iItemArray->Append(listItem);
	}
}
//按键处理函数
TKeyResponse CNDList::OfferKeyEventL(const TKeyEvent &aKeyEvent, TEventCode aType)
{
	if( (aKeyEvent.iCode == EKeyUpArrow) && (aType == EEventKey))
	{
		if(iIndex == 0)
		{
			if(iStart == 0)
			{
				iStart = iCount-5;	
				if(iStart<0)
				{
					iStart = 0;
					iIndex = iCount - 1;
				}
				else
				{
					iIndex =4;
				}

			}
			else
			{
				iStart--;
			}
		}
		else
		{
			iIndex--;
		}
	}
	else if( (aKeyEvent.iCode == EKeyDownArrow) && (aType == EEventKey))
	{
		if(iIndex == 4)
		{
			if(iStart == iCount-5)
			{
				iStart = iIndex = 0;
			}
			else
			{
				iStart++;
			}
		}
		else
		{
			if(iStart+iIndex+1 == iCount)
			{
				if(iStart>0)
				{
					iStart--;
				}
				else
				{
					iIndex = 0;
				}
			}
			else
			{
				iIndex++;
			}		
		}
	}
	else if(aKeyEvent.iCode == 0XF845 && aType == EEventKey)
	{
		(*iItemArray)[iStart+iIndex]->SetSelected(!(*iItemArray)[iStart+iIndex]->GetSelected());
	}
	return EKeyWasConsumed;
}
//绘制列表到指定GC上
void CNDList::Draw(CWindowGc &aGc)
{

	TRect rect;

	rect.SetRect(1,5+28*iIndex,174,28+28*iIndex);
	
	aGc.DrawRect(rect);	
	
	for(TInt i = 0;i<KMAXNUM && (iStart+i)<iCount;i++)
	{
		rect.SetRect(6,5+i*28,36,29+i*28);
		aGc.DrawBitmap(rect,iIcon);//速度慢
// 		aGc.BitBlt();
// 		aGc.BitBltMasked();

		aGc.DrawText((*iItemArray)[i+iStart]->GetContent()->Des(),TPoint(50,21+i*28));
		if((*iItemArray)[i+iStart]->GetSelected())
		{
			rect.SetRect(136,6+i*28,155,26+i*28);
			aGc.DrawBitmap(rect,iIconSelect);
		}
	}
	aGc.SetBrushColor(KRgbWhite);
	rect.SetRect(170,0,176,144);
	aGc.DrawRect(rect);
	if(iCount>5)
	{
		aGc.SetBrushColor(KRgbGray);
		TInt height = (5*144)/iCount;
		TInt start = 0;
		if(iStart+iIndex>4)
		{
			start = (iStart+iIndex-4)*(144-height)/(iCount-5);
		}
		rect.SetRect(170,start,174,start+height);
		aGc.DrawRect(rect);
	}
	
}
void CNDList::SetRect(TRect& aRect)
{
	iRect = aRect;
}
//获取当前选中项索引
TInt CNDList::Selected()
{
	return iIndex+iStart;
}
//设置指定索引为选中项
void CNDList::SetSelected(TInt aIndex)
{
	iIndex = 0;
	if(aIndex>iCount-1)
	{
		iStart = iCount-1;
	}
	else
	{
		iStart = aIndex;
	}
}
//添加条目到列表项结尾处
void CNDList::AppendAnItem( const TDesC& aDes )
{
	CNDListItem* listItem = new(ELeave) CNDListItem;
	listItem->SetContent(aDes);
	listItem->SetSelected(EFalse);
	iItemArray->Append(listItem);
	iCount++;
}
void CNDList::AppendAnItem( CNDListItem *anItem)
{
	CNDListItem* listItem = new(ELeave) CNDListItem;
	listItem->SetContent(anItem->GetContent()->Des());
	listItem->SetSelected(anItem->GetSelected());
	iItemArray->Append(listItem);
	iCount++;
}

⌨️ 快捷键说明

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