//首先先创建一个 GRID 对象
iGrid = new( ELeave ) CAknGrid;
iGrid->SetContainerWindowL( *this );
TInt gridFlags = EAknListBoxSelectionGrid;
iGrid->ConstructL( this, gridFlags);
//第二,创建 GRID MODE 对象
iGridM = new( ELeave ) CAknGridM;
iGrid->SetModel( iGridM );
//第三, 设置GRID层
TBool iVerticalOrientation = EFalse;
TBool iTopToBottom = ETrue;
TBool iLeftToRight = ETrue;
iNumOfColumns = KNumOfSelectionColumns;
iNumOfRows = KNumOfSelectionRows;
iGraphHeight=KGraphHeight;
iCellSize.iWidth = ( aRect.iBr.iX - aRect.iTl.iX ) / iNumOfColumns;
iCellSize.iHeight = ( aRect.iBr.iY - aRect.iTl.iY ) / iNumOfRows;
iGrid->SetLayoutL( iVerticalOrientation,iLeftToRight, iTopToBottom,
iNumOfRows, iNumOfColumns,iCellSize );
//第四,设置滚动条
CAknGridView::TScrollingType
iVerticalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
CAknGridView::TScrollingType
iHorizontalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
iGrid->SetPrimaryScrollingType( iVerticalScrollingType );
iGrid->SetSecondaryScrollingType( iHorizontalScrollingType );
iGrid->CreateScrollBarFrameL(ETrue);
iGrid->ScrollBarFrame()
->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff,CEikScrollBarFrame::EAuto);
AknListBoxLayouts::SetupStandardGrid( *iGrid );
//设置GRID元素的图像信息和文本信息
TInt grWidth = 0;
TInt txWidth = 0;
grWidth = iGrid->ColumnWidth();
txWidth = iGrid->ColumnWidth();
const CFont* fontText = CCoeEnv::Static()->NormalFont();
AknListBoxLayouts::SetupFormGfxCell( *iGrid, iGrid->ItemDrawer(),
0, 0, 0, 0, 0, grWidth, iGraphHeight,
TPoint( 0, 0 ), TPoint( grWidth, iGraphHeight) );
AknListBoxLayouts::SetupFormTextCell( *iGrid, iGrid->ItemDrawer(),
1, fontText,
215,
0, 0,
iGrid->ItemHeight()-3,
txWidth,
CGraphicsContext::ECenter,
TPoint( 0, iGrid->ItemHeight()-iGraphHeight),
TPoint( iGrid->ColumnWidth(), iGrid->ItemHeight() )
);
//创建图片数组,并将它放到数据层中
CArrayPtr* icons =new ( ELeave ) CAknIconArray(7);
CleanupStack::PushL( icons );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestContact,EMbmGirdtestContact) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestCalllog,EMbmGirdtestCalllog) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestConfig,EMbmGirdtestConfig) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestCallwall,EMbmGirdtestCallwall) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestFind,EMbmGirdtestFind) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestBlackcode,EMbmGirdtestBlackcode) );
icons->AppendL(
iEikonEnv->CreateIconL(
KMbmPath,EMbmGirdtestTodo,EMbmGirdtestTodo) );
// Sets graphics as ListBox icon.
iGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
CleanupStack::Pop();
//从视图层得到数据层对象
CDesCArray* array = static_cast
(iGrid->Model()->ItemTextArray());
array->Reset();
//从资源文件中得到字符串,添加到数据层中
TBuf<255> item;
HBufC* textResource = StringLoader::LoadLC(QTN_CONTACT);
item.Format(KItemFormat,0,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_CALLLOG);
item.Format(KItemFormat,1,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_CONFIG);
item.Format(KItemFormat,2,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_CALLWALL);
item.Format(KItemFormat,3,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_FIND);
item.Format(KItemFormat,4,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_BLACKCODE);
item.Format(KItemFormat,5,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
textResource = StringLoader::LoadLC(QTN_TODO);
item.Format(KItemFormat,6,textResource);
array->AppendL(item);
CleanupStack::PopAndDestroy();
//将数据变化显示到视图上
iGrid->HandleItemAdditionL();