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

📄 worldcontainer.cpp

📁 这是一个关于Symbian Gird控件的程序
💻 CPP
字号:
#include <gulicon.h>
#include <aknutils.h>
#include <avkon.hrh>
#include <eikedwin.h>

#include <akniconarray.h> 
#include <akngrid.h>
#include <aknlists.h> 

#include "WorldContainer.h"
#include "LDK008appui.h"


CWorldContainer::CWorldContainer()
{
	iStatEdit	= NULL;
	iGrid		= NULL;
}

// EPOC default constructor can leave.
void CWorldContainer::ConstructL(const TRect& aRect)
{
    CreateWindowL();	
    SetRect( aRect );
/*	
	//创建edit对象
	iStatEdit = new (ELeave) CEikEdwin;
	//设置Container指针
	iStatEdit->SetContainerWindowL(*this);
	//构造edit
	iStatEdit->ConstructL(EEikEdwinWidthInPixels, 10, 10, 1);
	//设置位置
	iStatEdit->SetExtent(TPoint(0, 0), aRect.Size());
	//设置焦点
	iStatEdit->SetFocus(ETrue);
*/
	//Add something here & Create Grid
	iGrid = new( ELeave ) CAknGrid;	//创建一个Grid控制器实例/对象
	iGrid->SetContainerWindowL( *this );	
	iGrid->ConstructL( this, EAknListBoxSelectionGrid );	//第二阶段构造
	iGrid->SetEmptyGridTextL(_L("empty"));	//设置Grid默认文本:当Grid的Text未设置时,显示默认字符串	
	iGrid->SetRect(aRect.Size());	//设置Grid的矩形显示区域
	
	iVerticalOrientation	= EFalse;	//排列方向:垂直方向为假,则是水平方向
	iTopToBottom		= ETrue;	//排列方式:从上倒下为真
	iLeftToRight			= ETrue;	//排列顺序:从左到有为真
	iNumOfColumns		= 3;		//列数
	iNumOfRows		= 3;		//行数
	iNumOfItems		= 9;		//项的总个数
	//the ordering of the data.
	iVerticalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
	iHorizontalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;

	iSizeOfItems.iWidth = ( aRect.iBr.iX - aRect.iTl.iX ) / iNumOfColumns;
	iSizeOfItems.iHeight = ( aRect.iBr.iY - aRect.iTl.iY ) / iNumOfRows;
	//设置网格布局
	iGrid->SetLayoutL
		( 
		iVerticalOrientation,		//填充网格项时的方向,ETrue表示列序,EFalse表示行序
		iLeftToRight,				//填充网格项时的方向,ETrue表示自顶向下,EFalse表示自底向上
		iTopToBottom, 				//填充网格项时的方向,ETrue表示自左向右,EFalse表示自右向左
		iNumOfColumns,				//网格的列数
		iNumOfRows,					//网格的行数
		iSizeOfItems				//网格的大小
		);	
	//设置主方向轴上的滚动行为——当焦点水平移动到最右边时,是否自动移动到最左边
	iGrid->SetPrimaryScrollingType( iHorizontalScrollingType );	
	//设置次方向轴上的滚动行为——当焦点垂直移动到最下方时,是否自动移动到最上边
	iGrid->SetSecondaryScrollingType( iVerticalScrollingType );	

	//获得显示的图片
	LoadGraphicsL();
	//添加文字信息
	AddDataL();
	//设置布局
	ApplySelGridGraphicStyleL();

    ActivateL();
}

// destructor
CWorldContainer::~CWorldContainer()
{
	//析构edit对象
	if(iStatEdit)
	{
		delete iStatEdit;
		iStatEdit = NULL;
	}
	if(iGrid)
	{
		delete iGrid;
		iGrid = NULL;
	}
}

void CWorldContainer::SizeChanged()
{
	if(iGrid)
	{
		iGrid->SetRect(Rect());
	}
}

// ---------------------------------------------------------
// CWorldContainer::CountComponentControls() const
// return the number of controls
// ---------------------------------------------------------
//
TInt CWorldContainer::CountComponentControls() const
{
	// return number of controls inside this container
	return 1; 
}

// ---------------------------------------------------------
// CWorldContainer::ComponentControl(TInt aIndex) const
// return the pointer to specified control.
// ---------------------------------------------------------
//
CCoeControl* CWorldContainer::ComponentControl( TInt /*aIndex*/ ) const
{
	return iGrid;
}

// ---------------------------------------------------------
// CWorldContainer::Draw(const TRect& aRect) const
// handle the message when client region must be redrawn.
// ---------------------------------------------------------
//
void CWorldContainer::Draw( const TRect& /*aRect*/ ) const
{
	CWindowGc& gc = SystemGc();
// 	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
// 	gc.SetBrushColor(KRgbRed);
// 	gc.DrawRect(Rect());
}

// ---------------------------------------------------------
// CAknExGridContainer::OfferKeyEventL(
//           const TKeyEvent& aKeyEvent, TEventCode aType )
// Handles the key events.
// ---------------------------------------------------------
//
TKeyResponse CWorldContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType )
{
	//将按键设置到edit中
	if(iGrid)
	{
		return iGrid->OfferKeyEventL(aKeyEvent, aType);
	}
	return EKeyWasNotConsumed;
}

// End of File  

void CWorldContainer::SetTextL(const TDesC& aBuf)
{
	if(iStatEdit)
	{
		iStatEdit->SetTextL(&aBuf);
	}
}

void CWorldContainer::GetText(TDes& aBuf)
{
	if(iStatEdit)
	{
		iStatEdit->GetText(aBuf);
	}	
}

void CWorldContainer::LoadGraphicsL()
{
	CLDK008AppUi* pApp = (CLDK008AppUi*)CEikonEnv::Static()->AppUi();

	CArrayPtr< CGulIcon >* icons = new(ELeave) CAknIconArray(iNumOfItems);
	CleanupStack::PushL( icons );

	icons->AppendL(pApp->LoadGraphicsL(0));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));
	icons->AppendL(pApp->LoadGraphicsL(1));

	// 设置图片数组
	iGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
	CleanupStack::Pop(); // icons array
}

void CWorldContainer::AddDataL()
{
	MDesCArray* array = iGrid->Model()->ItemTextArray();	//获取模型对象
	CDesCArray* cArray = ( CDesCArray* )array;				//获取描述符数组

	TBuf<32> sBuf;
	//a
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("notes"));
	cArray->AppendL(sBuf);
	//b	
	sBuf.Copy(_L("1\t"));
	sBuf.Append(_L("friends"));
	cArray->AppendL(sBuf);
	//c
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("map"));
	cArray->AppendL(sBuf);
	//d	
	sBuf.Copy(_L("1\t"));
	sBuf.Append(_L("music"));
	cArray->AppendL(sBuf);
	//e	
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("video"));
	cArray->AppendL(sBuf);
	//f	
	sBuf.Copy(_L("1\t"));
	sBuf.Append(_L("search"));
	cArray->AppendL(sBuf);
	//g
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("dictionary"));
	cArray->AppendL(sBuf);
	//h
	sBuf.Copy(_L("1\t"));
	sBuf.Append(_L("forum"));
	cArray->AppendL(sBuf);
	//i
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("blog"));
	cArray->AppendL(sBuf);
	//为网格添加数据
	iGrid->HandleItemAdditionL();
}

void CWorldContainer::ApplySelGridGraphicStyleL()
{
	if (!iGrid) 
	{
		return;
	}
	//设置网格默认的前景色和背景色
	AknListBoxLayouts::SetupStandardGrid( *iGrid );

	TInt nImgWidth = 48;
	//获取中文字体
	const CFont* fontText = ApacPlain12();
	//或者const CFont* fontText = LatinBold12()
	TInt nX = (iGrid->ColumnWidth() - nImgWidth) / 2;
	TInt nY = (iGrid->ItemHeight() - nImgWidth - fontText->HeightInPixels()) / 2;

	//设置图标的布局
	AknListBoxLayouts::SetupFormGfxCell( 
		*iGrid, 
		iGrid->ItemDrawer(),	
		0,								//图标在网格项格式串中的位置,表示第一项
		nX,								//左边位置
		nY,								//右边位置
		0,								//该参数无效
		0,								//该参数无效							
		nImgWidth,				//图标宽度
		nImgWidth,				//图标高度
		TPoint( nX, nY ),				//图标左顶点的相对坐标
		TPoint( nImgWidth+nX, nImgWidth+nY) );	//图标右底点的相对坐标
	//设置文本的布局
	AknListBoxLayouts::SetupFormAntiFlickerTextCell( 
		*iGrid, 
		iGrid->ItemDrawer(),
		1,								//文本在网格项格式串中的位置
		fontText,						//字体
		215,							//文本颜色(是系统调色板中黑色对应的索引值)
		0,								//左边空白
		0,								//该参数无效
		nImgWidth+nY+fontText->HeightInPixels(),		//基线
		iGrid->ColumnWidth(),			//文本的宽度
		CGraphicsContext::ECenter,		//文本对齐方式,居中
		TPoint( 0, nImgWidth+nY), //文本左顶点的相对坐标
		TPoint( iGrid->ColumnWidth(), iGrid->ItemHeight()) );	//文本右底点的相对坐标

	DrawNow();
}


⌨️ 快捷键说明

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