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

📄 gridcontainer.cpp

📁 symbian有关文件操作的一个很好的例子
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CGridContainer from CCoeControl, MCoeControlObserver
*  Part of  : Hello
*  Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES

#include <HelloLdci.rsg>
#include "HelloLdci.hrh"
#include "GridContainer.h"
#include "HelloLdciAppUi.h"

#include <akngrid.h>	//for Grid
#include <e32base.h>		//for CArrayPtr
#include <AknIconArray.h>	//for CAknIconArray
#include <AknLists.h>		//for AknListBoxLayouts

// ================= MEMBER FUNCTIONS =======================

const TInt KNumberOfIcons(7);
const TInt KTextColor = 215;
const TInt KLengthOfStringToShowIndex = 40;
// C++ default constructor can NOT contain any code, that
// might leave.
//
CGridContainer::CGridContainer()
{
	m_pGrid = NULL;
}

// EPOC default constructor can leave.
void CGridContainer::ConstructL(const TRect& aRect)
{
    CreateWindowL();

	m_pGrid = new(ELeave)CAknGrid;
	m_pGrid->SetContainerWindowL(*this);
	m_pGrid->ConstructL(this,EAknListBoxSelectionGrid);
	
	_LIT(KTxtEmpty,"Empty");
	m_pGrid->SetEmptyGridTextL( KTxtEmpty );
	m_pGrid->SetRect( aRect );
	
//	定义网格属性
	const TInt KNumOfSelectionColumns = 3;//列
	const TInt KNumOfSelectionRows = 3;//行
	const TInt KInitialNumOfItemsInSelection = 7;//可用项

	TSize iSizeOfItems;	//网格的大小
	iSizeOfItems.iWidth = ( aRect.iBr.iX - aRect.iTl.iX ) / KNumOfSelectionColumns;
	iSizeOfItems.iHeight = ( aRect.iBr.iY - aRect.iTl.iY ) / KNumOfSelectionRows;
	
	
	// Set initial parameters for the grid
	TBool iVerticalOrientation = EFalse;//ETrue;//填充网格项时,ETrue表示列序,EFalse表示行序
	TBool iTopToBottom = ETrue;//ETrue表示自顶向下,EFalse表示自底向上
	TBool iLeftToRight = ETrue; //ETrue表示自左向右,EFalse表示自右向左
	TInt iNumOfColumns = KNumOfSelectionColumns; //网格的列数
	TInt iNumOfRows = KNumOfSelectionRows;//网格的行数

   
	CAknGridView::TScrollingType iVerticalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
	CAknGridView::TScrollingType iHorizontalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
	
	
//设置网格方向和顺序
	m_pGrid->SetLayoutL( iVerticalOrientation, iLeftToRight,
		iTopToBottom, iNumOfColumns, iNumOfRows,
		iSizeOfItems );
//设置主方向轴上的滚动行为——当焦点水平移动到最右边时,是否自动移动到最左边
	m_pGrid->SetPrimaryScrollingType( iHorizontalScrollingType );
//设置次方向轴上的滚动行为——当焦点垂直移动到最下方时,是否自动移动到最上边
	m_pGrid->SetSecondaryScrollingType( iVerticalScrollingType );
//设置当前选项
	m_pGrid->SetCurrentDataIndex( 0 );
//添加图片
	LoadGraphicsL();
//添加文字
	AddDataL();
//设置格式
	ApplySelGridGraphicStyleL();
	
	SetRect(aRect);
    ActivateL();
}

// Destructor
CGridContainer::~CGridContainer()
{
	if(m_pGrid)
	{
		delete m_pGrid;
		m_pGrid = NULL;
	}
}

// ---------------------------------------------------------
// CGridContainer::SizeChanged()
// Called by framework when the view size is changed
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CGridContainer::SizeChanged()
{
	if (m_pGrid)
	{
		m_pGrid->SetExtent(TPoint(0,0),TSize(Rect().Width(),Rect().Height()));
	}
}

// ---------------------------------------------------------
// CGridContainer::CountComponentControls() const
// (other items were commented in a header).
// ---------------------------------------------------------
//
TInt CGridContainer::CountComponentControls() const
{
	return 1; // return nbr of controls inside this container
}

// ---------------------------------------------------------
// CGridContainer::ComponentControl(TInt aIndex) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
CCoeControl* CGridContainer::ComponentControl(TInt aIndex) const
{
		return m_pGrid;
}

// ---------------------------------------------------------
// CGridContainer::Draw(const TRect& aRect) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CGridContainer::Draw(const TRect& aRect) const
{
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbGray);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);
}

// ---------------------------------------------------------
// CGridContainer::OfferKeyEventL(...)
// Notify key events to editors.
// (other items were commented in a header).
// ---------------------------------------------------------
//
TKeyResponse CGridContainer::OfferKeyEventL(
											 const TKeyEvent& aKeyEvent, TEventCode aType)
{
	if (m_pGrid)
	{
		if (aType == EEventKey && aKeyEvent.iScanCode == 167)//== EKeyDevice3)
		{
			_LIT( format,"%d");
			TBuf<KLengthOfStringToShowIndex> number;
			number.Format( format, m_pGrid->CurrentItemIndex()+1);
			iEikonEnv->InfoWinL( number,KNullDesC );
		}
		return m_pGrid->OfferKeyEventL(aKeyEvent,aType);
	}
	return EKeyWasNotConsumed;
}

void CGridContainer::LoadGraphicsL()
{
	CHelloLDCIAppUi* pApp = (CHelloLDCIAppUi*)CEikonEnv::Static()->AppUi();

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

    // Create icon bitmap and mask.
    if ( m_pGrid )
	{
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(0));
	}
    // Set icon array.
    m_pGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
    CleanupStack::Pop(icons); // icons array

}
void CGridContainer::AddDataL()
{
	MDesCArray* array = m_pGrid->Model()->ItemTextArray();
    CDesCArray* cArray = ( CDesCArray* )array;
	TBuf<32> sBuf;
//a
	sBuf.Copy(_L("0\t"));
	sBuf.Append(_L("ColorView"));
	cArray->AppendL(sBuf);
//b	
	sBuf.Copy(_L("1\t"));
	sBuf.Append(_L("SingleList"));
	cArray->AppendL(sBuf);
//c
	sBuf.Copy(_L("2\t"));
	sBuf.Append(_L("c"));
	cArray->AppendL(sBuf);
//d	
	sBuf.Copy(_L("3\t"));
	sBuf.Append(_L("d"));
	cArray->AppendL(sBuf);
//e	
	sBuf.Copy(_L("4\t"));
	sBuf.Append(_L("e"));
	cArray->AppendL(sBuf);
//f	
	sBuf.Copy(_L("5\t"));
	sBuf.Append(_L("f"));
	cArray->AppendL(sBuf);
//g
	sBuf.Copy(_L("6\t"));
	sBuf.Append(_L("g"));
	cArray->AppendL(sBuf);
	
	m_pGrid->HandleItemAdditionL();
}

void CGridContainer::ApplySelGridGraphicStyleL()
{	
   if (!m_pGrid) 
	{
        return;
	}

	// 将网格的文本前景色和背景色设为默认值
	AknListBoxLayouts::SetupStandardGrid(*m_pGrid);

	// 设置文本子单元格
	const CFont* KFont =  ApacPlain12(); 
	
	// 获取绘制器指针	
	CFormattedCellListBoxItemDrawer* itemDrawer = m_pGrid->ItemDrawer();
	//计算单元格的尺寸
	TInt cellWidth = m_pGrid->ColumnWidth();
	TInt cellHeight = m_pGrid->ItemHeight();
	TInt nX = (m_pGrid->ColumnWidth() - 24) / 2;
	TInt nY = (m_pGrid->ItemHeight() - 24 - KFont->HeightInPixels()) / 2;

	// 设置图形子单元格
	AknListBoxLayouts::SetupFormGfxCell(
		*m_pGrid,		//网格实例
		itemDrawer, 		// 网格的绘制器
		0,					// 该单元格在格式化字符串中的索引
		nX,//nX					// 左边位置
		nY,//nY					// 上边位置
		0,					// 右边位置 - 未使用
		0,					// 底边位置 - 未使用
		24,//24		// 图形子单元格的宽度
		24,//24	// 图形子单元格的高度
		TPoint (nX, nY),   //TPoint(nX,nY)                      		// 起始位置
		TPoint (24+nX, 24+nY)); //TPoint(24+nX,24+nY)	// 结束位置
	//获取中文字体
	//const CFont* fontText = ApacPlain12();

	TInt baseline = 24+nY+KFont->HeightInPixels();// cellHeight - KFont->DescentInPixels() - 1;

	AknListBoxLayouts::SetupFormTextCell(
		*m_pGrid,		// 网格实例
		itemDrawer,		// 网格的绘制器
		1,					// 该单元格在格式化字符串中的索引
		KFont,				// 文本字体
		KTextColor,		// 文本的颜色
		0,					// 左边距
		0,					// 右边距 - 未使用
		baseline,//24+nY+fontText->HeightInPixels()			// 基线
		cellWidth,		// 文本宽度 (边距包含在内)
		CGraphicsContext::ECenter,			// 文本对齐方式
		TPoint (0, 24+nY),//TPoint( 0, 24+nY),		// 起始位置
		TPoint(cellWidth, cellHeight));	// 结束位置	
//	DrawNow();
}

TInt CGridContainer::GetCurrentIndex() const
{
    return m_pGrid->CurrentItemIndex();
}
// End of File  

⌨️ 快捷键说明

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