博客园丁

我是博客园的一丁,我会永不停顿,不停创新。
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Symbian Dynamic ListBox

Posted on 2006-11-03 16:39  Jason.Jiang  阅读(1615)  评论(1编辑  收藏  举报
Symbian Dynamic ListBox

这些天一直在弄Symbian的ListBox,做个简单的总结——动态列表的建立方法:

1. MMP文件中需要的库:
LIBRARY euser.lib apparc.lib cone.lib eikcore.lib
LIBRARY eikcoctl.lib avkon.lib eikctl.lib bafl.lib
2. 在资源文件中定义ListBox 资源:
   RESOURCE LISTBOX r_notebook_listbox
{
flags = EAknListBoxSelectionList | EAknListBoxLoopScrolling;
}

3. 定义拥有ListBox的控件类,使其从CCoeControl,和MEikListBoxObserver 公有继承,并在类中定义ListBox为数据成员:
    #include <aknlists.h> // CAknSingleStyleListBox
#include <barsread.h> // TResource Reader
#include <e32def.h> // STATIC_CAST
#include <eikclbd.h> // CColumnListBoxData
#include <eikmenub.h> // CEikMenuBarclass
#include <stringloader.h> // StringLoader
#include <uikon.hrh> // TKeyCode #defines

CContainer : public CCoeControl, MEikListBoxObserver
{
// Other Code
private:
  CAknColumnListBox*  iList;
}
CAknColumnListBox 为ListBox的基类。

4. 建立列表:
   void CreateListL()
{
iList = new (ELeave) CAknSingleStyleListBox;
iList->SetContainerWindowL(*this);

TResourceReader reader;
CEikonEnv::Static()->CreateResourceReaderLC(reader, R_NOTEBOOK_LISTBOX);
  iList->ConstructFromResourceL(reader);
CleanupStack::PopAndDestroy();
}

5. 加载列表项:
   void SetupListItemsL()
{
CTextListBoxModel* model = iList->Model();
model->SetOwnershipType (ELbmOwnsItemArray);
  CDesCArray* savedArray = STATIC_CAST(CDesCArray*, model->
ItemTextArray());
LoadSavedL(*savedArray);
}

void LoadSavedL(CDesCArray& aArray)
{
_LIT (KString,"\tSelect%d");
TBuf <16> aString;
for (TInt i = 1; i< 10; i++)
  {
  aString.Format(KString(),i);
  aArray.AppendL (aString);
  }
}
首先取得列表的模型,获取模型中的列表项数组,将列表项逐个添加到列表项数组里。

6. 设置滚动条:
   void SetupScrollBarsL()
{
iList->CreateScrollBarFrameL();
iList->ScrollBarFrame()->SetScrollBarVisibilityL(
  CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
}

7. 其他控件属性或函数的设置:
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (iList)
  return iList->OfferKeyEventL (aKeyEvent, aType);
else
  return EKeyWasNotConsumed;
}

void SizeChanged()
    {
    iList->SetExtent( TPoint(0,0), iList->MinimumSize() );
}

void Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
gc.Clear(aRect);
}