1. variables declared in the header file
CAknGrid * iGrid;
CAknGridM * iGridM;
TInt iNumOfRows;
TInt iNumOfColumns;
TSize iCellSize;
TInt iGraphHeight;
2. Code in the source file
Const TInt KNumOfSelectionColumns = 3;
Const TInt KNumOfSelectionRows = 3;
Const TInt KGraphHeight = 32;
_ Partition (KMbmPath, "Z: // system // apps // GirdTest. mbm ");
_ Partition (KItemFormat, "% d/t % S ");
ConstructL ()
IGrid = new (ELeave) CAknGrid;
IGrid-> setcontainer1_wl (* this );
IGridM = new (ELeave) CAknGridM;
IGrid-> SetModel (iGridM );
TInt gridFlags = EAknListBoxSelectionGrid;
IGrid-> ConstructL (this, gridFlags );
CAknGridView: TScrollingType iVerticalScrollingType =
CAknGridView: EScrollIncrementLineAndLoops;
CAknGridView: TScrollingType iHorizontalScrollingType =
CAknGridView: EScrollIncrementLineAndLoops;
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 );
IGrid-> SetPrimaryScrollingType (iVerticalScrollingType );
IGrid-> SetSecondaryScrollingType (iHorizontalScrollingType );
IGrid-> CreateScrollBarFrameL (ETrue );
IGrid-> ScrollBarFrame ()-> SetScrollBarVisibilityL (CEikScrollBarFrame: EOff, CEikScrollBarFrame: EAuto );
AknListBoxLayouts: SetupStandardGrid (* iGrid );
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 <CGulIcon> * icons = new (ELeave) CAknIconArray (7 );
CleanupStack: PushL (icons );
Icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, EMbmGirdtestContact, EMbmGirdtestContact ));
Icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, region, region); icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, EMbmGirdtestConfig, region ));
Icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, EMbmGirdtestCallwall, EMbmGirdtestCallwall ));
Icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, EMbmGirdtestFind, EMbmGirdtestFind ));
Icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, region, region); icons-> AppendL (iEikonEnv-> CreateIconL (KMbmPath, region, region ));
// Sets graphics as ListBox icon. iGrid-> ItemDrawer ()-> FormattedCellData ()-> SetIconArrayL (icons );
CleanupStack: Pop ();
CDesCArray * array = static_cast <CDesCArray *> (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 ();
4 ,~ CGirdTestContainer ()
Delete iGrid;
5. SizeChanged ()
If (iGrid) {iGrid-> SetRect (Rect ());}
6. OfferKeyEventL
TKeyResponse CGirdTestContainer: OfferKeyEventL (const TKeyEvent & aKeyEvent, TEventCode aType)
{
If (aType! = EEventKey)
Return EKeyWasNotConsumed;
Switch (aKeyEvent. iCode)
{
// Forward up and down key press events to the list box
Case EKeyUpArrow:
Case EKeyDownArrow:
Case EKeyLeftArrow:
Case EKeyRightArrow:
Return iGrid-> OfferKeyEventL (aKeyEvent, aType );
Case EKeyOK:
{
TInt nIndex = iGrid-> CurrentItemIndex ();
/*****
.......
*****/
Break;
}
Default:
Break;
}
Return EKeyWasNotConsumed;
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/stonesl/archive/2008/06/23/2577686.aspx
I have found many examples on the Internet, but I have not succeeded. This article is widely used and I hope it will be helpful.