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

📄 myselectioncontrol.cpp

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

#include "MySelectionControl.h"
#include <fbs.h> //CFbsBitmap
#include <aknutils.h>
#include <CompoundContainerTest_0xE2943051.rsg> 
#include "Biaofeng.hrh"
//#include "ControlFactory.h"
#include "MyPopMenuControl.h"
#include "BiaofengMainWindow.h"

#define SCROLL_IMAGE_SIZE		TSize(11,9)
#define SELECTION_IMAGE_SIZE	TSize(16,16)
const TInt KPopListHeight = 90;
const TInt KNumberOfScrollImage = 5;
const TInt KNumberOfHeadImage = 8;
const TInt KNumberOfControls = 1;

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

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

//根据指定的尺度垂直移动控件
//aSpan > 0 控件向下平移
//aSpan < 0 控件向上平移
void CMySelectionControl::VertMoveControl(TInt aSpan)
{	
		TPoint movePonit(0,aSpan);
		SetExtent(Position()+ movePonit,Size());
}

CMySelectionControl::CMySelectionControl()
{
	SetDefaultSize(240,30);
	iText = NULL;
	iTextList = NULL;
	iCurrentIndex = 0;
	
	iFont = iEikonEnv->DenseFont();
	//iFont = iEikonEnv->AnnotationFont();
	//CEikonEnv::Static()->SymbolFont();
}

CMySelectionControl::~CMySelectionControl()
{
	if(iText) 
	{
		delete iText;
		iText = NULL;
	}
	if(iTextList) 
	{
		delete iTextList;
		iTextList = NULL;
	}
	if (iPopList) 
	{
		delete iPopList;
		iPopList = NULL;
	}
	iBitmapList.ResetAndDestroy();
	iBitmapList.Close();
	iScrollBitmap.ResetAndDestroy();
	iScrollBitmap.Close();
}


void CMySelectionControl::ParseTag(TTag& aTag)
{
	LoadScrollBitmapL(R_SCROLL_FILE_NAME,KNumberOfScrollImage);//"\\system\\apps\\My\\Scroll.mbm"
	for(TInt i=0;i<aTag.GetAttributeList().Count();i++)
	{
		if(aTag.GetAttributeList()[i].iAttributeName.Compare(KSelectName) == 0) //_LIT(KSelectName,"name");
		{
			iSelectName.Copy(aTag.GetAttributeList()[i].iAttributeValue);
			if (iSelectName.Compare(KAreaText)== 0)//_LIT(KAreaText,"area");
			{
				LoadTextListL(R_AREA_SELECT_ITEMS);//"北京"
				iPopList = CMyPopMenuControl::NewL(iTextList,NULL,this);
			}
//			else
//			{
//				if (iSelectName.Compare(KSexText)== 0)//_LIT(KSexText,"sex");
//				{
//					LoadTextListL(R_SEX_SELECT_ITEMS);
//				}
//				else
//				{
//					if (iSelectName.Compare(KHeadImage)== 0) //_LIT(KHeadImage,"head");
//					{
//						CreateBitmapL(R_HEAD_FILE_NAME,KNumberOfHeadImage);
//					}
//					else
//					{
//						LoadTextListL(aTag.GetTagValue());
//						iPopList = CMyPopMenuControl::NewL(iTextList,NULL,this);
//					}
//				}
//
//			}
		}
		else
		{
			if(aTag.GetAttributeList()[i].iAttributeName.Compare(KLabel) == 0) //_LIT(KLabel,"lab");
			{
				SetTextL(aTag.GetAttributeList()[i].iAttributeValue);
			}
		}
	}
}


TInt CMySelectionControl::CountComponentControls() const
{
	if (iPopList)
	{
		return KNumberOfControls;
	}
	return 0;
}


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

TPtrC CMySelectionControl::GetSelectValue()
{
	if (iTextList)
	{
		return iTextList->MdcaPoint(iCurrentIndex);
	}
	else
	{
		iIntToBuf.Num(iCurrentIndex);
		return TPtrC(iIntToBuf);
	}
}

TInt CMySelectionControl::GetIntValue(const TDesC& aValue)
{
	TBufC<10> number(aValue);
	TLex myLexer (number);	
	TInt  intVal= 0;
	User::LeaveIfError (myLexer.Val(intVal));
	return intVal;
}

//返回该控件的标记

TTag CMySelectionControl:: GetTag()
{
	TTag tag ;
	tag.SetTagName(KControlSelection);
	tag.AppendAttribule(KSelectName,iSelectName);
	if (iTextList)
	{
		tag.AppendAttribule(KValue,iTextList->MdcaPoint(iCurrentIndex));
	}
	else
	{
		iIntToBuf.Num(iCurrentIndex);
		tag.AppendAttribule(KValue,iIntToBuf);
	}
	return tag;
}


//创建选择图片
void CMySelectionControl::CreateBitmapL(TInt aBitmapResource, TInt aNumberOfBitmap)
{
	
	TFileName bitmapFile;
	iCoeEnv->ReadResource( bitmapFile, aBitmapResource );
	User::LeaveIfError (CompleteWithAppPath (bitmapFile));
	for (TInt index = 0;index < aNumberOfBitmap; index++)
	{
		CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
		User::LeaveIfError(bitmap->Load(bitmapFile, index));
		iBitmapList.AppendL(bitmap);
	}
	iNumberOfItems = aNumberOfBitmap;
}

//加载选择文本
void CMySelectionControl::LoadTextListL(TInt aTextResource)
{
	iTextList = iCoeEnv->ReadDesCArrayResourceL( aTextResource );
	iNumberOfItems = iTextList->Count();
}

void CMySelectionControl::LoadTextListL(const TDesC& aText)
{
	iTextList = new (ELeave) CDesCArrayFlat(10); 
	TPtrC ptr = aText;
	TInt offset = ptr.Locate('|');
	while(offset != KErrNotFound)
	{
		iTextList->AppendL(ptr.Left(offset));
		if (ptr.Length() > offset)
		{
			ptr.Set(ptr.Mid(offset+1));
			offset = ptr.Locate('|');
		}
		else
		{
			ptr.Set(KNullDesC);
			break;
		}
	}
	if (ptr != KNullDesC)
	{
		iTextList->AppendL(ptr);
	}
	iNumberOfItems = iTextList->Count();
}


void CMySelectionControl::LoadScrollBitmapL(TInt aBitmapResource, TInt aNumberOfBitmap)
{
	TFileName bitmapFile;
	iCoeEnv->ReadResource( bitmapFile, aBitmapResource );
	User::LeaveIfError (CompleteWithAppPath (bitmapFile));
	for (TInt index = 0;index < aNumberOfBitmap; index++)
	{
		CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
		User::LeaveIfError(bitmap->Load(bitmapFile, index));
		iScrollBitmap.AppendL(bitmap);
	}
}

void CMySelectionControl::SizeChanged()
{
	CCoeControl::SizeChanged();
	TRect controlRect(Rect());
	iTextTopLeft.SetXY(controlRect.iTl.iX+8,controlRect.iTl.iY+16);
	iSelectionRect.SetRect(controlRect.iTl.iX+66,controlRect.iTl.iY+1,
						   controlRect.iBr.iX-8,controlRect.iBr.iY-1);
	if (iPopList)
	{
		if(iNumberOfItems < KMaxPopItem)
		{
			iPopList->SetExtent(TPoint(iSelectionRect.iTl.iX,iSelectionRect.iBr.iY),
				                TSize(iSelectionRect.Width(),KItemHeight * iNumberOfItems));
		}
		else
		{
			iPopList->SetExtent(TPoint(iSelectionRect.iTl.iX,iSelectionRect.iBr.iY),
				                TSize(iSelectionRect.Width(),KPopListHeight));
		}
	}
}

void CMySelectionControl::FocusChanged(TDrawNow aDrawNow)
{
	if (EDrawNow == aDrawNow)
	{
		DrawNow();
	}
}


void CMySelectionControl::InitControlL(const CCoeControl* aParent)
{
	if (aParent == NULL)
	{
		CreateWindowL();
		iParent = NULL;
	}
	else
	{
		iParent = aParent;
		SetContainerWindowL(*aParent);
//		SetParent(const_cast<CCoeControl*>(aParent));
	}
	if (iPopList)
	{
		iPopList->SetContainerWindowL(*this);
		iPopList->MakeVisible(EFalse);
		iPopList->SetFocus(EFalse);
	}
	SetRect(Rect());   
	ActivateL();    
}


//设置链接内容
void CMySelectionControl::SetTextL(const TDesC& aText)
{
	if(iText)
	{
		delete iText;
	}
	iText = aText.AllocL();
}

void CMySelectionControl::SetColor(const TRgb& aColor)
{
	iColor = aColor;
}

void CMySelectionControl::SetFont(const CFont* aFont)
{
	iFont = aFont;
}

//显示文本信息
void CMySelectionControl::OutputText(CWindowGc & aGc,const TRgb &aColor ) const
{
	aGc.SetPenColor(aColor);
	//aGc.SetUnderlineStyle(EUnderlineOn);
	if(iFont) 
	{
		aGc.UseFont(iFont);	
	} 
	else 
	{
		aGc.UseFont(iEikonEnv->DenseFont()/*CEikonEnv::Static()->SymbolFont()*/);
	}
	if(iText)
	{
		aGc.DrawText(iText->Des(), iTextTopLeft);
	}	
    if (iTextList && iPopList)
	{
		aGc.DrawText(iTextList->MdcaPoint(iCurrentIndex),iSelectionRect.iTl+TPoint(3,18));
    }
	else
	{
		if (iTextList) 
		{
			aGc.DrawText(iTextList->MdcaPoint(iCurrentIndex),iSelectionRect.iTl+TPoint(3,18));
		}
	}
	aGc.DiscardFont();
}

void CMySelectionControl::Draw(const TRect& aRect) const
{
	CWindowGc& gc = SystemGc();
	gc.Clear(aRect);
	gc.SetPenStyle(CGraphicsContext::ESolidPen);
	if(IsFocused()) 
	{
		gc.SetPenColor(KRgbRed);
	}
	else
	{
		gc.SetPenColor(KRgbGray);
	}
	gc.DrawRect(iSelectionRect);

	OutputText(gc,KRgbBlack);
	if(iBitmapList.Count() > 0)
	{	
		// create TPoint object for top left of bitmap
		TPoint bitmapTopLeft(iSelectionRect.iTl.iX+35,iSelectionRect.iTl.iY+2); 
		//gc.BitBltMasked(bitmapTopLeft, iBitmap, TRect(TPoint(0,0), TSize(10, 10)), iMask, ETrue);
		gc.DrawBitmap(TRect(bitmapTopLeft, SELECTION_IMAGE_SIZE), iBitmapList[iCurrentIndex]);	
		//gc.BitBlt(bitmapTopLeft, iBitmap); 
	}

    if (iScrollBitmap.Count() > 0) 
	{
		TPoint ScrollTopLeft(iSelectionRect.iBr.iX-15,iSelectionRect.iTl.iY+6); 
		//gc.BitBltMasked(bitmapTopLeft, iBitmap, TRect(TPoint(0,0), TSize(10, 10)), iMask, ETrue);
		gc.DrawBitmap(TRect(ScrollTopLeft, SCROLL_IMAGE_SIZE), iScrollBitmap[GetScrollIndex()]);
    }
}

//处理按键事件
TKeyResponse CMySelectionControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
	if (aType != EEventKey)
		return EKeyWasNotConsumed;

		TInt code = aKeyEvent.iCode;
		
		if (iPopList)
		{
			if (iPopList->IsFocused())
			{
				if (EKeyEnter == code || EKeyOK == code) 
				{
					iCurrentIndex = iPopList->GetCurrentItemIndex();
					
//					if(iPopList->IsVisible())
						iPopList->MakeVisible(EFalse);	//隐藏弹出列表
//					else
//						iPopList->MakeVisible(ETrue);
					
					iPopList->SetFocus(EFalse);
					
					DrawNow();
					return EKeyWasConsumed;
				}
				else
				{
					if (EKeyUpArrow == code || EKeyDownArrow == code) 
					{
						return iPopList->OfferKeyEventL(aKeyEvent,aType);
					}
				}
			}
			else
			{
				if (EKeyEnter == code || EKeyOK == code) 
				{
//					CCoeControl * parent = GetParent();
					TRect parentRect = iParent->Rect();
					TPoint popListPoint = iPopList->Rect().iBr;
					if (!parentRect.Contains(popListPoint)) 
					{
						TInt moveSpan = popListPoint.iY - parentRect.iBr.iY;
						((CMainWinControl*)iParent)->VertMoveControl(-moveSpan);
						iParent->DrawNow();
					}
					iPopList->MakeVisible(ETrue);	//显示弹出列表
					iPopList->DrawNow();
					iPopList->SetFocus(ETrue);
					return EKeyWasConsumed;
				}	
			}
		}
		if (code == EKeyRightArrow)
		{
			iCurrentIndex = (iCurrentIndex+1) % iNumberOfItems;
			DrawNow();	
			return EKeyWasConsumed;
		}
		else
		{
			if(code == EKeyLeftArrow)
			{
				if (iCurrentIndex > 0)
				{
					iCurrentIndex--;
				}
				else
				{
					iCurrentIndex = iNumberOfItems-1;
				}
				DrawNow();
				return EKeyWasConsumed;
			}
		}	
	return EKeyWasNotConsumed;
}

TInt CMySelectionControl::GetScrollIndex() const
{
	TInt scrollIndex;
	if (iTextList && iPopList)
	{
		 scrollIndex = 4;
	}
	else
	{
		if (iCurrentIndex ==0)
		{
			scrollIndex = 0;
		}
		else
		{
			if (iCurrentIndex == iNumberOfItems - 1)
			{
				scrollIndex = 2;
			}
			else
			{
				scrollIndex = 1;
			}
		}
	}
	return scrollIndex;
}

⌨️ 快捷键说明

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