自定义列表框,可以调整高度。

  [b] 自定义列表框,可以调整高度。 [/b][b]收藏 [/b]
  我是用的自定义列表控件实现的。继承了CAknSingleStyleListBox。
  //------------------------------------------------ ----头文件------------------------------------------------
  #ifndef __CMySingleStyleListBox__
  #define __CMySingleStyleListBox__
  #include
  #include
  class CControl;
  class CFbsBitmap;
  class CMaxIcon; //是我自定义的图片类。
  class CMySingleStyleListBox : public CAknSingleStyleListBox
  {
  public:
  CMySingleStyleListBox(CControl * aControl);
  virtual ~CMySingleStyleListBox();
  protected:
  virtual void CreateItemDrawerL();
  CControl * iControl;
  RArray iSelectRecordID; //在列表中显示的记录ID
  CMaxIcon * iBack; //背景颜色
  CMaxIcon * iSelectedBack; //选中的背景
  };
  class CMyColumnListBoxItemDrawer : public CColumnListBoxItemDrawer
  {
  public:
  CMyColumnListBoxItemDrawer(MTextListBoxModel* aTextListBoxModel,
  const CFont* aFont, CColumnListBoxData* aColumnData,
  CEikListBox* aListBox, CControl * aControl, CMaxIcon * aBack,
  CMaxIcon * aSelBack);
  virtual ~CMyColumnListBoxItemDrawer();
  virtual void DrawActualItem(TInt aItemIndex, const TRect& aActualItemRect,
  TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool,
  TBool aItemIsSelected) const;
  void SetFindBox(CAknSearchField* aFindBox);
  CAknSearchField* iFindBox;
  CEikListBox* iOwner;
  CControl * iControl;
  RArray iSelectRecordID; //在列表中显示的记录ID
  MAknsSkinInstance* skin;
  //图片
  CMaxIcon * iBack; //背景颜色
  CMaxIcon * iSelectedBack; //选中的背景
  };
  #endif
  //------------------------------------------------ ----定义文件------------------------------------------------
  #include
  #include
  //
  #include "manager/Control.h"
  #include "manager/Helper.h"
  #include "manager/MaxIcon.h"
  #include "control/MySingleStyleListBox.h"
  //
  // Construction/Destruction
  //
  #define MEM_FREE(a) if(a){delete a; a=NULL;}
  CMySingleStyleListBox::CMySingleStyleListBox(CCont rol * aControl)
  {
  iControl = aControl;
  iBack = NULL;
  iSelectedBack = NULL;
  }
  void CMySingleStyleListBox::CreateItemDrawerL()
  {
  CColumnListBoxData* columnData = CColumnListBoxData::NewL();
  EnableDragEvents();
  const CFont* myFont;
  myFont = CEikonEnv::Static()->DenseFont();
  CEikListBox *aListBox = this;
  //加载图片
  CMaxIconManager* pIconMgr = iControl->iIconManager;
  //背景
  iBack = pIconMgr->GetMaxIcon(CMaxIconManager::EListBack);
  iSelectedBack = pIconMgr->GetMaxIcon(CMaxIconManager::EListCur);
  iItemDrawer = new (ELeave) CMyColumnListBoxItemDrawer(Model(), myFont,
  columnData, aListBox, iControl, iBack, iSelectedBack);
  //iItemDrawer->SetItemCellSize(TPoint(100,100));
  //iItemDrawer->SetItemCellSize(TSize(360,100));
  iItemDrawer->SetItemCellSize(TSize(360 , 100));
  //View()->SetItemHeight(100) ;
  }
  CMySingleStyleListBox::~CMySingleStyleListBox()
  {
  }
  // MySingleStyleListBox.cpp: implementation of the CMyColumnListBoxItemDrawer class.
  //
  //
  CMyColumnListBoxItemDrawer::CMyColumnListBoxItemDr awer(
  MTextListBoxModel* aTextListBoxModel, const CFont* aFont,
  CColumnListBoxData* aColumnData, CEikListBox* aListBox,
  CControl * aControl, CMaxIcon * aBack, CMaxIcon * aSelBack) :
  CColumnListBoxItemDrawer(aTextListBoxModel, aFont, aColumnData), iOwner(
  aListBox)
  {
  iControl = aControl;
  iBack = aBack;
  iSelectedBack = aSelBack;
  //SetItemCellSize(TSize(360, 150));
  }
  void CMyColumnListBoxItemDrawer::DrawActualItem(TInt aItemIndex,
  const TRect& aActualItemRect, TBool aItemIsCurrent,
  TBool aViewIsEmphasized, TBool, TBool aItemIsSelected) const
  {
  //需要修改 高亮
  /*
  MAknsSkinInstance* skin = AknsUtils::SkinInstance();
  MAknsControlContext* cc = AknsDrawUtils::ControlContext(iOwner);
  if (skin && cc)
  {
  AknsDrawUtils::Background(skin, cc, iOwner, *iGc, aActualItemRect);
  }
  */
  TRect aRect = aActualItemRect;
  aRect.iBr.iX += 30;
  TRgb rSelected(TRgb(228, 100, 41)); //选中的颜色
  TRgb rUnSelected(TRgb(255, 255, 255)); //没有选中的颜色
  TRect outer_rect(aRect);
  outer_rect.Shrink(2, 2);
  TRect inner_rect(outer_rect);
  inner_rect.Shrink(3, 3);
  /*
  //画背景图片
  NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
  */
  /*
  if (aItemIsCurrent)
  {
  NHelper::DrawMaxIcon(*iGc, aActualItemRect, iSelectedBack);
  }
  else
  {
  NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
  }
  */
  if (aItemIsCurrent)
  {
  //NHelper::DrawMaxIcon(*iGc, aActualItemRect, iSelectedBack);
  iGc->DrawBitmap(aRect, iSelectedBack->m_pBitmap);
  }
  else
  {
  //NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
  iGc->DrawBitmap(aRect, iBack->m_pBitmap);
  }
  //初始黑色
  const CFont* font = CEikonEnv::Static()->NormalFont();
  iGc->UseFont(font);
  iGc->SetPenStyle(CGraphicsContext::ESolidPen);
  iGc->SetPenColor(iHighlightedTextColor);
  TPtrC itemText = iModel->ItemText(aItemIndex);
  TInt baseline = (aRect.Height() - font->HeightInPixels()) / 2
  + font->AscentInPixels();
  //计算每段字符串长度
  TInt highlightwidth = font->TextWidthInPixels(itemText);
  TRect highlighted = aRect;
  highlighted.SetWidth(highlightwidth);
  //画每段字符串iTextColor
  iGc->SetPenColor(rUnSelected);
  iGc->SetPenStyle(CGraphicsContext::ESolidPen);
  iGc->DrawText(itemText, highlighted, baseline);
  //释放字体
  iGc->DiscardFont();
  }
  CMyColumnListBoxItemDrawer::~CMyColumnListBoxItemD rawer()
  {
  }
  //------------------------------------------------ ----注意事项------------------------------------------------
  我终于找到解决的办法了。办法如下:
  1.在创建列表元素的时候设置一下大小
  iListShopBox->View()->SetItemHeight(100);
  2.在CreateItemDrawerL()中添加
  iItemDrawer->SetItemCellSize(TSize(360 , 100));
  就OK了……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值