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

📄 mypopmenucontrol.cpp

📁 一个可以加载图片作为下拉菜单的symbian手机示例
💻 CPP
字号:
/*
============================================================================
 Name        : MyPopMenuControl.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CMyPopMenuControl implementation
============================================================================
*/

#include "MyPopMenuControl.h"
//#include "MySelectionControl.h"
#include <eikenv.h>

#define THUMB_SIZE TSize(4,8)

//设置控件的默认大小
void CMyPopMenuControl::SetDefaultSize(TInt aWidth,TInt aHeight)
{
	TSize defalutSize(aWidth,aHeight);
	SetSize(defalutSize);	
}

void CMyPopMenuControl::SetDefaultSize(TSize aSize)
{
	SetSize(aSize);	
}

CMyPopMenuControl::CMyPopMenuControl()
{
	SetDefaultSize(KItemWidth ,KItemHeight);
}


CMyPopMenuControl::~CMyPopMenuControl()
{
	if (iBackgroundImage)
	{
		delete iBackgroundImage;
		iBackgroundImage = NULL; 
	}
}

CMyPopMenuControl* CMyPopMenuControl::NewLC(CDesCArrayFlat* aItemList,
					  CFbsBitmap* aBackgroundImage,const CCoeControl* aParent)
{
	CMyPopMenuControl* self = new (ELeave)CMyPopMenuControl();
	CleanupStack::PushL(self);
	self->ConstructL(aItemList,aBackgroundImage,aParent);
	return self;
}

CMyPopMenuControl* CMyPopMenuControl::NewL(CDesCArrayFlat* aItemList,
		              CFbsBitmap* aBackgroundImage,const CCoeControl* aParent)
{
	CMyPopMenuControl* self=CMyPopMenuControl::NewLC(aItemList ,aBackgroundImage,aParent);
	CleanupStack::Pop(); // self;
	return self;
}

void CMyPopMenuControl::InitControlL(const CCoeControl* aParent)
{
	if (aParent == NULL)
    {
    	CreateWindowL();
     }
    else
    {
        SetContainerWindowL(*aParent);
    }
//    SetRect(aRect);    	
    ActivateL();
}

void CMyPopMenuControl::ConstructL(CDesCArrayFlat* aItemList,CFbsBitmap* aBackgroundImage,const CCoeControl* aParent)
{
//	CControlBase::InitControlL(aParent);
	InitControlL(aParent);
	
	iFont = NULL;
	iItemList = aItemList;
	iNumberOfItems = iItemList->Count();
	iBackgroundImage = aBackgroundImage;
	iItemIndex = 0;
	iTopIndex = 0;
	iNeedScroll = iNumberOfItems > KMaxPopItem ? ETrue : EFalse;
}

void CMyPopMenuControl::SizeChanged()
{
	if (iNeedScroll)
	{
		iMoveSpan = Rect().Height()/iNumberOfItems;
	}
}

void CMyPopMenuControl::Draw(const TRect& aRect) const
{
	CWindowGc& gc = SystemGc();
	gc.Clear(aRect);
	
	TRect controlRect(Rect());
	if (iBackgroundImage)
	{
		gc.DrawBitmap(controlRect,iBackgroundImage);	//���Ʋ˵�����

		TPoint itemTopLeft(controlRect.iTl+TSize(7,8));
		gc.SetPenStyle(CGraphicsContext::ENullPen);
		gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.SetBrushColor(TRgb(170,170,248));
		TPoint currentItemTopLeft(itemTopLeft.iX,itemTopLeft.iY 
								+ KItemHeight*(iItemIndex - iTopIndex));
		//���Ƶ�ǰѡ���ɫ
		gc.DrawRect(TRect(currentItemTopLeft,TSize(KItemWidth,KItemHeight)));
		
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.SetPenColor(KRgbBlack);
		DrawPopMenu(itemTopLeft);	//���Ƶ���˵�ѡ��
	}
	else
	{
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.SetPenColor(TRgb(0x00afff));
		gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.SetBrushColor(TRgb(252,230,195));
		gc.DrawRect(controlRect);	//���Ʋ˵���ɫ
	
		TPoint currentItemTopLeft(controlRect.iTl.iX,controlRect.iTl.iY 
			+ KItemHeight*(iItemIndex - iTopIndex));
		if (iNeedScroll)
		{

			TPoint scrollPoint(controlRect.iBr.iX-5,controlRect.iTl.iY);
			gc.DrawLine(scrollPoint,TPoint(scrollPoint.iX,controlRect.iBr.iY));	
			
			gc.SetPenStyle(CGraphicsContext::ENullPen);
			gc.SetBrushColor(TRgb(0x00afff));
			TRect currentItemRect(currentItemTopLeft,TSize(controlRect.Width()-5,KItemHeight));
			gc.DrawRect(currentItemRect);	//���Ƶ�ǰѡ���ɫ

			TPoint thumbPoint;
			if (iItemIndex == iNumberOfItems - 1)
			{	
				thumbPoint.SetXY(scrollPoint.iX+1,controlRect.iBr.iY-THUMB_SIZE.iHeight);
			}
			else
			{
				thumbPoint.SetXY(scrollPoint.iX+1,scrollPoint.iY+iMoveSpan*iItemIndex);
			}			
				gc.DrawRect(TRect(thumbPoint,THUMB_SIZE));	//���ƹ���
		}
		else
		{
			gc.SetPenStyle(CGraphicsContext::ENullPen);
			gc.SetBrushColor(TRgb(0x00afff));
			//���Ƶ�ǰѡ���ɫ
			gc.DrawRect(TRect(currentItemTopLeft,TSize(controlRect.Width(),KItemHeight)));
		}
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.SetPenColor(KRgbBlack);
		TPoint itemTopLeft(controlRect.iTl+TSize(2,1));
		DrawPopMenu(itemTopLeft);	//���Ƶ���˵�ѡ��
	}	
}

//���Ƶ���˵�
void CMyPopMenuControl::DrawPopMenu(const TPoint& aItemTopLeft) const
{
	CWindowGc& gc = SystemGc();
	if (iFont)
	{
		gc.UseFont(iFont);
	}
	else
	{
		gc.UseFont(CEikonEnv::Static()->SymbolFont());
	}

	//����item
	if (iItemList)
	{
		TPoint textPoint = aItemTopLeft + TSize(3,15);
		TInt itemNum;
		if (iNumberOfItems < KMaxPopItem)
		{
			itemNum = iNumberOfItems;
		}
		else
		{
			itemNum = KMaxPopItem;
		}
		for (TInt i = 0; i < itemNum; i++)
		{
			gc.DrawText(iItemList->MdcaPoint(iTopIndex + i),textPoint);
			textPoint.iY += KItemHeight;
		}	
	}
	gc.DiscardFont();
}

TBool CMyPopMenuControl::IsFocusChangeKey(const TKeyEvent& aKeyEvent,TEventCode aType)
{
	if(aType == EEventKey) 
	{
		return (aKeyEvent.iCode == EKeyDownArrow) || (aKeyEvent.iCode == EKeyUpArrow)
		       || (aKeyEvent.iCode == EKeyLeftArrow) || (aKeyEvent.iCode == EKeyRightArrow) ;
	}
	return false;
	
}

//���?���¼�
TKeyResponse CMyPopMenuControl::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{

	if (aType != EEventKey)
	{
		return EKeyWasNotConsumed;
	}
	if(IsFocusChangeKey(aKeyEvent,aType))
	{
		if(aKeyEvent.iCode == EKeyDownArrow) 
		{
			if(iItemIndex < iNumberOfItems - 1)
			{
				iItemIndex++;
				if (iItemIndex - iTopIndex >= KMaxPopItem)
				{
					iTopIndex = iItemIndex - KMaxPopItem + 1;
				}
			}
		}
		else if(aKeyEvent.iCode == EKeyUpArrow)
		{
			if(iItemIndex > 0)
			{
				iItemIndex--;
				if (iTopIndex > iItemIndex)
				{
					iTopIndex = iItemIndex;
				}
			}
		}
		DrawNow();
		return EKeyWasConsumed;	
	}
	return EKeyWasNotConsumed;
}

//��ȡ��ǰ�˵�ѡ������
TInt CMyPopMenuControl::GetCurrentItemIndex()
{
	return iItemIndex;
}

void CMyPopMenuControl::ResetMenuItem()
{
	iItemIndex = 0;
	iTopIndex = 0;
}

⌨️ 快捷键说明

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