有關ListBox控件的一些總結

終於對Series 60的ListBox控件有了一個比較清晰的瞭解
說真的這幾天看這樣的代碼真有點趕鴨子上架
我在放ListBox控件的容器類中聲明瞭一個CEikColumnListBox* _listbox
在其ConstructL函數中構造一個CEikColumnListBox的實例
_listBox = new(ELeave) CAknSingleStyleListBox;
_listBox->ConstructL(this);

_listBox->SetContainerWindowL(*this);
_listBox->CreateScrollBarFrameL(ETrue);
_listBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn,CEikScrollBarFrame::EAuto);
因爲需要處理listbox的Keypress事件
_listBox->SetListBoxObserver(this);

所以這裏的容器類的實例必須擴展public?MEikListBoxObserver
頭文件必須包含eiklbo.h

當然需要實現MEikListBoxObserver中的虛函數
void CTestListContainer::HandleListBoxEventL(CEikListBox* aListBox,TListBoxEvent aEventType)
這裏處理KeyPress事件
需要aEventType==EEventEnterKeyPressed
如果需要知道選中的是某一個Item
TInt number(_listBox->CurrentItemIndex());
number是從0開始的一直到ListBox的Item總數減一的整數

上下移動選擇的處理是在函數
TKeyResponse CTestListContainer::OfferKeyEventL(
const TKeyEvent& aKeyEvent,TEventCode aType){
if (aType!=EEventKey)
{return EKeyWasNotConsumed;}
f (_listBox){
return _listBox->OfferKeyEventL(aKeyEvent, aType);
}
else {return EKeyWasNotConsumed;}
}
這個函數主要是處理按鍵事件,它並不是專門針對ListBox,當然在這裏是處理ListBox上下移動按鍵。

ListBox的Item可以通過動態添加和資源文件生成
動態添加
TBuf<32>item;

_LIT(KItemName1,"234234234234");
//這裏有個風格格式
item.Format(_L(" %S "),&KItemName1);
listBoxItems->AppendL(item);

_LIT(KItemName2,"rtetertet");
item.Format(_L(" %S "),??&KItemName2);
listBoxItems->AppendL(item);

_LIT(KItemName3,"adsfasfasdf");
item.Format(_L(" %S "),??&KItemName3);
listBoxItems->AppendL(item);

_listBox->HandleItemAdditionL();
_listBox->SetCurrentItemIndexAndDraw(listBoxItems->Count()-1);

_listBox->ActivateL();

這裏是對ListBox框的一些初步瞭解
第一感覺在Symbian下因爲不是可視化編程,所以添加控件比較麻煩
但Symbian的程序結構的確是非常合理
而且我很高興它也是基於MVC的

當然列表框也是可以加圖標的

很簡單
new了一個CAknSingleLargeStyleListBox的列表框
_listBox=new (ELeave)CAknSingleLargeStyleListBox;

建立一個數組來保存ico信息
CAknIconArray* iconList=new (ELeave)CAknIconArray(10);
CleanupStack::PushL(iconList);
把圖標信息添加到數組
iconList->AppendL(iEikonEnv->CreateIconL(KIconsFilename, EMbmTestlistGolgo2,EMbmTestlistGolgo2m));
加完了不要忘了CleanupStack::Pop();

EMbmTestlistGolgo2和EMbmTestlistGolgo2m是在.mbg裏定義的,所以不要忘記include .mbg(在生成.mbm時,該文件在EPOC/include/下產生)文件.
把一些位圖放到一個.mbm文件裏訪問,每個位圖資源都有一個ID
當然把位圖放到.mbm要在.mmp文件裏定義

START BITMAP testlist.mbm
TARGETPATH systemapps estlist
HEADER
SOURCEPATH   ../bmp
SOURCE   C12 golgo2.bmp
SOURCE C12 golgo2m.bmp
END

給listbox加上圖標
我用的是
_listBox->ItemDrawer()->ColumnData()->SetIconArray(iconList);
另外對於CEikFormattedCellListBox擴展的ListBox
_listBox->ItemDrawer()->FormattedCellData()->SetIconArray();
_listBox->ItemDrawer()->FormattedCellData()->SetSubCellAlignmentL(2,CGraphicsContext::ELeft);

當然最後添加item的時候要注意格式
TBuf<32>item;
_LIT(KItemName1,"234234234234");
item.Format(_L("%d/t%S "),0,&KItemName1);
listBoxItems->AppendL(item);


在SymbianOS(series 60平臺) 下有很多格式的ListBox,更詳細的信息可以在

http://perso.wanadoo.fr/klisa/3650/ListBox/page01.html (Series 60 development for Windows programmers)得到。
(The tutorial related to ListBox on the website http://perso.wanadoo.fr has been downloaded, and you can get it at http://blogimg.chinaunix.net/blog/upfile2/080304223156.zip)


補充:
1, 設置ListBox無item時的顯示內容(默認爲"無內容")
_LIT(KTxtEmpty, "Empty");
iListBox->View()->SetListEmptyTextL(KTxtEmpty());

2, 移動ListBox及設置大小造成scroll bar不一致

iListPos = aPoint;
if( iListBox )
{
TRect rect = Rect();
TSize size( Rect().Width(), Rect().iBr.iY - iListPos.iY );
iListBox->SetExtent( iListPos, size );
iListBox->UpdateScrollBarL() ;
CEikScrollBar *scrollBar = iListBox->ScrollBarFrame()->GetScrollBarHandle( CEikScrollBar::EVertical );
CEikScrollBar *scrollBar = iListBox->ScrollBarFrame()->GetScrollBarHandle( CEikScrollBar::EHorizontal );
if( scrollBar )
{
TPoint pos = scrollBar->Position();
pos.iY = iListPos.iY;
scrollBar->SetExtent( pos, scrollBar->MinimumSize() );
}
}
3, Configure propertyse of list box column, such as graphics column, optional column etc.
CColumnListBoxItemDrawer* drawer = iListBox->ItemDrawer();
CColumnListBoxData* colData = drawer->ColumnData();
colData->SetGraphicsColumnL(0, ETrue);
colData->SetOptionalColumnL(1, EFalse);
colData->SetColumnWidthPixelL(0, 20);
colData->SetColumnAlignmentL(1, CGraphicsContext::ECenter);

發佈了3 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章