Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2028483
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类:

2008-03-04 18:08:52

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,更详细的信息可以在

(
Series 60 development for Windows programmers)得到。
(The tutorial related to ListBox on the website 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);


Related resource:
1, How to create mbm file:
2, Customize ListBox:

阅读(2081) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~